差分数组

#include
using namespace std;
#define ll long long
const int M=1e6+50;
ll n,op,mi,mx,mod,fina,d[M],pre[M];
int main(){
    scanf("%lld%lld%lld%lld%lld",&n,&op,&mod,&mi,&mx);

    char s[5];int x,y,c;

    for(int i=1;i<=op;i++){
        scanf("%s",s);
        scanf("%d%d",&x,&y);
        if(s[0]=='A'){
            scanf("%d",&c);
            d[x]+=c;
            d[y+1]-=c;
        }else{
            ll res=0,ans=0;
            for(long long i=1;i<=y;i++){
                ans+=d[i];
                if(i>=x&&mi<=(ans*i)%mod&&(ans*i)%mod<=mx)res++;
            }
            printf("%lld\n",res);
        }
    }
    scanf("%lld",&fina);
    long long now=0;
    for(long long i=1;i<=n;i++){
        now+=d[i];
        if(mi<=(now*i)%mod&&(now*i)%mod<=mx)pre[i]++;
        pre[i]+=pre[i-1];
    }
    while(fina--){
        scanf("%d%d",&x,&y);
        printf("%lld\n",pre[y]-pre[x-1]);
    }
    return 0;
}

P3948 数据结构

你可能感兴趣的:(c++,算法)