分块炸了

P2801 教主的魔法

sogk @ 2021-07-22 17:48:13

忽略丑陋的快读快写

#include <bits/stdc++.h>
#define AK 1
#define ll long long

namespace FastIOstream{
    template <typename T> 
    inline void read(T &x){//Fast read
        x=0;
        int f=1;
        char c=getchar();
        for(;!isdigit(c);){
            if(c=='-')
                f=-1;
            c=getchar();
        }
        for(;isdigit(c);){
            x=(x<<1)+(x<<3)+c-48;
            c=getchar();
        }
        x*=f;
        return;
    }
    template <typename T> 
    void print(T x){//Fast print
        if(x<0){
            x=-x;
            putchar('-');
        }
        if(x>9){
            print((x>>1)/5);
        }
        putchar(x%10+48);
        return;
    }
}

using namespace std;
using namespace FastIOstream;

ll n,t,a[1000005],d[1005],p[100005],l[1005],r[1005],s[1000005],w,num;

inline void init(){
    scanf("%lld%lld",&n,&t);
    for(int i=1;i<=n;++i){
        scanf("%lld",&a[i]);
        s[i]=a[i];
    }
    w=sqrt(n);
    num=n/w;
    for(int i=1;i<=num;++i){
        l[i]=(i-1)*w+1;
        r[i]=i*w;
    }
    if(num*w<n){
        ++num;
        l[num]=(num-1)*w+1;
        r[num]=n;
    }
    for(int i=1;i<=num;++i){
        for(int j=l[i];j<=r[i];++j){
            p[j]=i;
            sort(s+l[i],s+r[i]+1);
        }
    }
    return;
}

inline void Plus(int x,int y,int k){
    if(p[x]==p[y]){
        int q=p[x];
        for(int i=x;i<=y;++i){
            a[i]+=k;
        }
        for(int i=l[q];i<=r[q];++i){
            s[i]=a[i];
        }
        sort(s+l[q],s+r[q]+1);
    }
    else{
        int qr=p[x]+1,qf=p[y]-1;
        for(int i=qr;i<=qf;++i){
            d[i]+=k;
        }
        qr=x;qf=r[p[x]];
        for(int i=qr;i<=qf;++i){
            a[i]+=k;
        }
        for(int i=l[p[x]];i<=r[p[x]];++i){
            s[i]=a[i];
        }
        sort(s+l[p[x]],s+r[p[x]]+1);
        qr=l[p[y]];qf=y;
        for(int i=qr;i<=qf;++i){
            a[i]+=k;
        }
        for(int i=l[p[y]];i<=r[p[y]];++i){
            s[i]=a[i];
        }
        sort(s+l[p[y]],s+r[p[y]]+1);
    }
    return;
}

inline ll cut(ll x,ll y,ll c){
    for(;x<y;){
        ll mid=(x+y)>>1;
        if(s[mid]+d[p[x]]>=c)y=mid;
        else x=mid+1;
    }
    return y;
}

inline int ask(int x,int y,int c){
    if(p[x]==p[y]){
        int ans=0;
        int q=p[x];
        for(int i=l[q];i<=r[q];++i){
            if(a[i]+d[q]>=c)ans++;
        }
        return ans;
    }
    else{
        int ans=0;
        int qr=p[x]+1,qf=p[y]-1;
        for(int i=qr;i<=qf;++i){
            int k=c;
            ans+=(r[i]-cut(l[i],r[i],k)+1);
        }
        qr=x,qf=r[p[x]];
        for(int i=qr;i<=qf;++i){
            if((a[i]+d[p[x]])>=c)ans++;
        }
        qr=l[p[y]],qf=y;
        for(int i=qr;i<=qf;++i){
            if((a[i]+d[p[y]])>=c)ans++;
        }
        return ans;
    }
}

signed main(){
    init();
    for(int i=1;i<=t;i++){
        char op;
        int x,y,k;
        cin >> op;
        read(x);
        read(y);
        read(k);
        if(op=='M'){
            Plus(x,y,k);
        }
        else if(op=='A'){
            print(ask(x,y,k));
            puts(" ");
        }
    }
    return 0;
}

|