90pts求调

P2801 教主的魔法

hmy521 @ 2023-11-15 20:50:40

#include<bits/stdc++.h>
using namespace std;
template<typename T>inline void read(T &cn){
    cn=0;T w=1;
    char c=getchar();
    while(!isdigit(c)){if(c=='-')w=-1;c=getchar();}
    while(isdigit(c)){cn=cn*10+c-48;c=getchar();}
    cn*=w;
}
int a[1000005],b[1000005];
int add[1005];
int tot,st[1000005],ed[1000005],pos[1000005];
inline void change(int l,int r,int d){
    int p=pos[l],q=pos[r];
    if(p==q){
        for(int i=l;i<=r;i++){
            a[i]+=d;
            b[i]=a[i];
        }
        sort(b+st[p],b+ed[p]+1);
    }else{
        for(int i=p+1;i<=q-1;i++)add[i]+=d;
        for(int i=l;i<=ed[p];i++){
            a[i]+=d;b[i]=a[i];
        }
        sort(b+st[p],b+ed[p]+1);
        for(int i=st[q];i<=r;i++){
            a[i]+=d;b[i]=a[i];
        }
        sort(b+st[q],b+ed[q]+1);
    }
}
inline int ask(int l,int r,int c){
    int p=pos[l],q=pos[r];
    int ans=0;
    if(p==q){
        for(int i=l;i<=r;i++){
            if(a[i]+add[p]>=c){
                ans++;
            }
        }
        return ans;
    }else{
        for(int i=p+1;i<=q-1;i++){
            int x=lower_bound(b+st[i],b+ed[i]+1,c-add[i])-b;
            ans+=ed[i]-x+1;
        }
        for(int i=l;i<=ed[p];i++){
            if(a[i]+add[p]>=c)ans++;
        }
        for(int i=st[q];i<=r;i++){
            if(a[i]+add[q]>=c)ans++;
        }
        return ans;
    }
}
int main(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    int n,q;
    read(n),read(q);
    for(int i=1;i<=n;i++){
        read(a[i]);b[i]=a[i];
    }
    int block=sqrt(n);
    tot=n/block;
    if((block*block)!=n)tot++;
    for(int i=1;i<=tot;i++){
        st[i]=(i-1)*block+1;
        ed[i]=i*block;
        if(i!=tot)sort(b+st[i],b+ed[i]+1);
    }
    ed[tot]=min(n,tot*block);
    sort(b+st[tot],b+ed[tot]+1);
    for(int i=1;i<=n;i++){
        pos[i]=(i-1)/block+1;
    }
    while(q--){
        char op;
        cin>>op;
        int l,r,w;
        read(l),read(r),read(w);
        if(op=='M'){
            change(l,r,w);
        }else{
            printf("%d\n",ask(l,r,w));
        }
    }
    return 0;
}

by immortal_ @ 2023-11-15 21:02:32

@hmy521 你开long long试一下呢


by hmy521 @ 2023-11-15 21:07:14

@immortal_ 还是90


by hmy521 @ 2023-11-15 21:07:49

@hmy521 这个应该跟long long没关系吧,数据范围并不大


by hmy521 @ 2023-11-15 22:02:14

@immortal_ 知道了,修改a数组后b数组必须在整个块范围内重新赋值,因为编号不一样


by immortal_ @ 2023-11-15 22:12:03

@hmy521 OK


|