# LOJ#6280. 数列分块入门 4

2018-02-13 10:06:13来源:cnblogs.com作者:自为风月马前卒人点击

#### 样例输入

``41 2 2 30 1 3 11 1 4 40 1 2 21 1 2 4``

#### 样例输出

``14``

#### 数据范围与提示

`#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#define int long long using namespace std;const int MAXN=2*1e6+10;const int INF=1e8+10;inline char nc(){    static char buf[MAXN],*p1=buf,*p2=buf;    return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++;}inline int read(){    char c=nc();int x=0,f=1;    while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}    while(c>='0'&&c<='9'){x=x*10+c-'0';c=nc();}    return x*f;}int a[MAXN],belong[MAXN],L[MAXN],R[MAXN],sum[MAXN],tag[MAXN],block;void IntervalAdd(int l,int r,int val){    for(int i=l;i<=min(R[l],r);i++) a[i]+=val,sum[belong[i]]+=val;    if(belong[l]!=belong[r])        for(int i=L[r];i<=r;i++)            a[i]+=val,sum[belong[i]]+=val;    for(int i=belong[l]+1;i<=belong[r]-1;i++)        tag[i]+=val;}int Query(int l,int r,int mod){    int ans=0;    for(int i=l;i<=min(R[l],r);i++)         ans+=a[i]+tag[belong[i]],ans=ans%mod;        if(belong[l]!=belong[r])        for(int i=L[r];i<=r;i++)            ans+=a[i]+tag[belong[i]];    for(int i=belong[l]+1;i<=belong[r]-1;i++)        ans+=sum[i]+tag[i]*block,ans=ans%mod;    return ans%mod;}main(){    #ifdef WIN32    freopen("a.in","r",stdin);   // freopen("b.out","w",stdout);    #else    #endif    int N=read();block=sqrt(N);    for(int i=1;i<=N;i++) belong[i]=(i-1)/block+1,L[i]=(belong[i]-1)*block+1,R[i]=belong[i]*block;    for(int i=1;i<=N;i++) a[i]=read();    for(int i=1;i<=N;i++) sum[belong[i]]+=a[i];    for(int i=1;i<=N;i++)    {        int opt=read(),l=read(),r=read(),c=read();        if(opt==0) IntervalAdd(l,r,c);        else                    printf("%lld/n",Query(l,r,c+1));     }     return 0;}`