差分

#include<iostream>
#include<cstdio>
using namespace std;
long long a[80010], b[80010], sum[80010], ans, now;
long long n, opt, mod, minn, maxx, l, r, x, f;
char ch[5];
int main(){
    scanf("%lld%lld%lld%lld%lld", &n, &opt, &mod, &minn, &maxx);
    for(long long j=1; j<=opt; j++){
        scanf("%s%lld%lld", &ch, &l, &r);									//注意:ch要是定义成字符,会有潜在的风险,读入空格? 
        if(ch[0]=='A'){
            scanf("%lld", &x);
            b[l]+=x, b[r+1]-=x;												//差分修改区间,O(1) 
        }
        else{
            ans=0, now=0;
            for(long long i=1; i<=r; i++){
                now+=b[i];													//now为差分数组b的前缀和 
                if(i>=l && (now*i)%mod>=minn && (now*i)%mod<=maxx) ans++;
            }
            printf("%lld
",ans);
        }
    }
    scanf("%lld",&f);
    for(long long i=1; i<=n; i++){
        a[i]=a[i-1]+b[i];
        if((a[i]*i)%mod>=minn && (a[i]*i)%mod<=maxx)	sum[i]=sum[i-1]+1;	//sum[i]统计从开始到位置i满足条件的个数 
        else 											sum[i]=sum[i-1];
    }
    for(long long j=1; j<=f; j++){
        scanf("%lld%lld", &l, &r);
        printf("%lld
", sum[r]-sum[l-1]);									//sum[l, r]=sum[1, r]-sum[1, l-1]; 
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lfyzoi/p/10572917.html