求教dalao

P2801 教主的魔法

BigOldDriver @ 2017-07-26 17:01:45

#include<bits/stdc++.h>
#define ll int
#define MAXN 1005
using namespace std;
ll N,Val[MAXN*MAXN],Q;
vector<ll>Block[MAXN];ll Bcnt;
ll Belong[MAXN*MAXN],Lazy[MAXN];
inline ll Get()
{
    ll num=0,bj=1;char x=getchar();
    while(!isdigit(x))
    {
        if(x=='-')bj=-1;
        x=getchar();
    }
    while(isdigit(x))
    {
        num=num*10+x-'0';
        x=getchar();
    }
    return num*bj;
}
inline void Rec(ll Now)
{
    ll i;
    Block[Now].clear();
    for(i=(Now-1)*Bcnt+1;i<=min(Now*Bcnt,N);++i)
        Block[Now].push_back(Val[i]);
    sort(Block[Now].begin(),Block[Now].end());
}
inline void Add(ll L,ll R,ll d)
{
    ll i,j;
    for(i=L;i<=min(Belong[L]*Bcnt,R);++i)Val[i]+=d;
    Rec(Belong[L]);
    if(Belong[L]!=Belong[R])
    {
        for(i=(Belong[R]-1)*Bcnt+1;i<=R;++i)Val[i]+=d;
        Rec(Belong[R]);
    }
    for(i=Belong[L]+1;i<=Belong[R]-1;++i)Lazy[i]+=d;
}
inline ll Query(ll L,ll R,ll z)
{
    ll Ans=0,i;
    for(i=L;i<=min(Belong[L]*Bcnt,R);++i)
        if(Val[i]+Lazy[Belong[L]]>=z)Ans++;
    if(Belong[L]!=Belong[R])
    {
        for(i=(Belong[R]-1)*Bcnt+1;i<=R;++i)
        if(Val[i]+Lazy[Belong[R]]>=z)Ans++;    
    }
    for(i=Belong[L]+1;i<=Belong[R]-1;++i)
    {
        ll x=z-Lazy[i];
        ll Now=lower_bound(Block[i].begin(),Block[i].end(),x)-Block[i].begin();
        Ans+=Block[i].size()-Now;
    }
    return Ans; 
}
int main()
{
    ll i,x,y,z;char cmd;
    N=Get();Q=Get();Bcnt=(ll)sqrt(N);
    for(i=1;i<=N;++i)
    {
        Belong[i]=(i-1)/Bcnt+1;Val[i]=Get();
        Block[Belong[i]].push_back(Val[i]);
    }
    for(i=1;i<=Belong[N];++i)
        sort(Block[i].begin(),Block[i].end());
    while(Q--)
    {
        while(cmd=getchar())if(cmd=='M'||cmd=='A')break;
        if(cmd=='M')
        {
            x=Get();y=Get();z=Get();
            Add(x,y,z);
        }
        else
        {
            x=Get();y=Get();z=Get();
            printf("%d\n",Query(x,y,z));
        }
    }    
    return 0;
}

|