牛客15949 are you ok?

链接

点击跳转

题解

查询的时候二分+线段树即可

代码

#include 
#include 
#include 
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct SegmentTree
{
    ll mx[maxn<<2], add[maxn<<2], L[maxn<<2], R[maxn<<2];
    void maketag_set(ll o, ll v)
    {
        add[o]=0;
        mx[o]=v;
    }
    void maketag_add(ll o, ll v)
    {
        add[o]+=v;
        mx[o]+=v;
    }
    void pushdown(ll o)
    {
        if(L[o]==R[o])return;
        if(add[o])
        {
            maketag_add(o<<1,add[o]);
            maketag_add(o<<1|1,add[o]);
            add[o]=0;
        }
    }
    void pushup(ll o)
    {
        mx[o]=max(mx[o<<1],mx[o<<1|1]);
    }
    void build(ll o, ll l, ll r, ll* array=NULL)
    {
        ll mid(l+r>>1);
        L[o]=l, R[o]=r;
        add[o]=0;
        if(l==r)
        {
            mx[o]=array[l];
            return;
        }
        build(o<<1,l,mid,array);
        build(o<<1|1,mid+1,r,array);
        pushup(o);
    }
    void Add(ll o, ll l, ll r, ll v)
    {
        ll mid(L[o]+R[o]>>1);
        if(l<=L[o] and r>=R[o]){maketag_add(o,v);return;}
        pushdown(o);
        if(l<=mid)Add(o<<1,l,r,v);
        if(r>mid)Add(o<<1|1,l,r,v);
        pushup(o);
    }
    ll Max(ll o, ll l, ll r)
    {
        ll mid(L[o]+R[o]>>1), ans(-linf);
        if(l<=L[o] and r>=R[o])return mx[o];
        pushdown(o);
        if(l<=mid)ans=max(ans,Max(o<<1,l,r));
        if(r>mid)ans=max(ans,Max(o<<1|1,l,r));
        return ans;
    }
}segtree;
ll a[maxn];
int main()
{
    de((sizeof(segtree)>>20));
    ll n, q;
    while(~scanf("%lld%lld",&n,&q))
    {
        ll i;
        rep(i,1,n)a[i]=read();
        segtree.build(1,1,n,a);
        while(q--)
        {
            ll k=read();
            if(k>segtree.Max(1,1,n))
            {
                printf("are you ok\n");
                continue;
            }
            ll l=1, r=n;
            while(l<r)
            {
                ll mid(l+r>>1);
                if(segtree.Max(1,1,mid)>=k)r=mid;
                else l=mid+1;
            }
            printf("%lld\n",l-1);
            if(l>1)segtree.Add(1,l-1,l-1,+1);
        }
    }
    return 0;
}

你可能感兴趣的:(#,线段树)