蒟蒻90分求调QAQ—orz

P2801 教主的魔法

yingxilin @ 2024-08-02 16:37:34

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define FOR(i,_l,_r) for(int i=_l;i<=_r;i++)
int n,q;
const int N=1e6+5;
const int M=1000+5;
LL b[M][M],a[N],add[M],pos[N],l[M],r[M];
void pre()
{
    int len,tot;
    len=tot=sqrt(n);
    FOR(i,1,tot){
        l[i]=(i-1)*len+1;
        r[i]=i*len;
        FOR(j,l[i],r[i]) pos[j]=i,b[i][j-l[i]+1]=a[j];
        sort(b[i]+1,b[i]+len+1);
        //FOR(j,l[i],r[i]) cout<<b[i][j]<<" ";cout<<endl;
    }
    if(r[tot]<n)
    {
        tot++;
        l[tot]=r[tot-1]+1;
        r[tot]=n;
        FOR(j,l[tot],r[tot]) pos[j]=tot,b[tot][j-l[tot]+1]=a[j];
        sort(b[tot]+1,b[tot]+(r[tot]-l[tot]+1)+1);
    }
}
void change(int ll,int rr,int w)
{
    int L=pos[ll];
    int R=pos[rr];
    if(L==R)
    {
        FOR(i,ll,rr) a[i]+=w;
        sort(b[L]+1,b[L]+(r[L]-l[L]+1)+1);
    }
    else{
        FOR(i,L+1,R-1) add[i]+=w;
        FOR(i,ll,l[L+1]-1) a[i]+=w,b[L][i-l[L]+1]=a[i];
        sort(b[L]+1,b[L]+(r[L]-l[L]+1)+1);
        FOR(i,r[R-1]+1,rr) a[i]+=w,b[R][i-l[R]+1]=a[i];
        sort(b[R]+1,b[R]+(r[R]-l[R]+1)+1);
    }
}
int query(int ll,int rr,int c)
{
    int ans=0;
    int L=pos[ll];
    int R=pos[rr];
    if(L==R)
    {
        FOR(i,ll,rr) if(a[i]+add[L]>=c) ans++;
        return ans;
    }
    else{
        FOR(i,L+1,R-1) 
        {
            int k=lower_bound(b[i]+1,b[i]+(r[i]-l[i]+1)+1,c-add[i])-b[i];
            if(1<=k&&k<=r[i]-l[i]+1)
                ans+=(r[i]-l[i]+1)-k+1;
        }
        FOR(i,ll,l[L+1]-1) if(a[i]+add[L]>=c) ans++;//cout<<ans<<" ";
        FOR(i,r[R-1]+1,rr) if(a[i]+add[L]>=c) ans++;//cout<<ans<<" ";
        return ans;
    }
}
int main()
{
    freopen("P2801.in","r",stdin);
    freopen("P2801.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>n>>q;
    FOR(i,1,n) cin>>a[i];
    pre();
    while(q--)
    {
        char opt;
        cin>>opt;
        if(opt=='M')
        {
            int ll,rr,w;
            cin>>ll>>rr>>w;
            change(ll,rr,w);
        }
        else{
            int ll,rr,c;
            cin>>ll>>rr>>c;
            cout<<query(ll,rr,c)<<endl;
        }
    }
    return 0;
}

|