求助wa#1本地通过洛谷WA

P2801 教主的魔法

Eason_wu @ 2024-02-05 17:32:07

#include<bits/stdc++.h>
#define endl "\n"
#define fr(ii,ss,tt) for(int ii=ss;ii<=tt;ii++)
#define rf(ii,ss,tt) for(int ii=ss;ii>=tt;ii--)
#define pii pair<int,int>
#define pt printf
#define fi first
#define eb emplace_back
#define se second
#define int long long
using namespace std;
inline void rd(){}
template<typename T, typename... args>inline void rd(T&x, args&...y) {char ch;bool f = 0;for (ch = getchar(); ch < '0' || '9' < ch; ch = getchar())if (ch == '-')f = 1;for (x = 0; '0' <= ch && ch <= '9'; ch = getchar())x = (x << 1) + (x << 3) + ch - '0';if (f)x = -x;rd(y...);}
template<typename IT>void shw(const char* name,IT s,IT t){cout<<name<<"={";for(IT now=s;now!=t;now++){cout<<*now<<",";}cout<<"}\n";}
template<typename T>void toMax(T&x,const T&y){if(x<y)x=y;}
template<typename T>void toMin(T&x,const T&y){if(x>y)x=y;}
const int N = 1e6 + 33,inf=2e9;
int n,m,a[N],bel[N],bl,bc,lb[N],rb[N];int btag[N],b[N];
int q_b(int bi,int w){//q cnt(i in bi:a[i]>=w)
    auto it=lower_bound(b+lb[bi],b+rb[bi]+1,w-btag[bi])-b;//find first b[it] >=w
    return (rb[bi]-it+1);
}
void a_b(int bi,int w){
    btag[bi]+=w;
}
void a_a(int l,int r,int w){
    int li=bel[l];
    int ri=bel[r];
    assert(bel[r]-bel[l]==0);
    fr(i,l,r){
        b[i]=(a[i]+=w);
    }
    sort(b+lb[li],b+rb[li]+1);
//  if(ri!=li)sort(b+lb[ri],b+rb[ri]+1);
}
signed main() {
    //freopen("o.out","w",stdout);
    rd(n,m);
    bl=sqrt(n);
    fr(i,1,n)rd(a[i]);
    fr(i,1,n){
        bel[i]=(i+bl-1)/bl;
        int bi=bel[i];
        if(!lb[bi])lb[bi]=i;
        rb[bi]=i;

        b[i]=a[i];
    }
    bc=bel[n];
    fr(bi,1,bc){
        sort(b+lb[bi],b+rb[bi]+1);
    }
    fr(_,1,m){
        char op;
        int ll,rr,c;
        cin>>op;
        if(op == 'M') {
            int l,r;
            rd(l,r,c);
            int li=bel[l];
            int ri=bel[r];
            if(li!=ri){
                int L=lb[li+1];
                int R=rb[ri-1];
                a_a(l,L-1,c);
                a_a(R+1,r,c);
                fr(bi,li+1,ri-1){
                    a_b(bi,c);
                }
            }else{
                a_a(l,r,c);
            }
        }
        if(op == 'A') {
            rd(ll,rr,c);
            int ans=0;
            int aa=bel[ll];
            int bb=bel[rr];
            int L=bel[rb[aa]+1];
            int R=bel[lb[bb]-1];
            fr(bi,L,R){
                ans+=q_b(bi,c);
            }
            //q_left a
            if(bel[ll]!=bel[rr]){
                fr(i,ll,rb[aa]){
                    ans+=(a[i]+btag[i]>=c);
                }
                fr(i,lb[bb],rr){
                    ans+=(a[i]+btag[i]>=c);
                }
            }else{
                fr(i,ll,rr){
                    ans+=(a[i]+btag[i]>=c);
                }
            }

            pt("%lld\n",ans);
        }
    }

    return 0;
}

|