区间修改+区间查询+双lazy标记
维护两个懒标记,注意优先级。没啥难度,就是细节要注意。
#include<cstdio>
#include<cstring>
#define ll long long
int n,m,mod;
ll a[100010];
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;char ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
struct node{
ll sum,add,mul;//add 加,mul乘
}tree[400040];
inline void build(int p,int l,int r){
tree[p].add=0;tree[p].mul=1;
if(l==r){
tree[p].sum=a[l];return;
}
int mid=l+r>>1;
build(p<<1,l,mid);build(p<<1|1,mid+1,r);
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
}
inline void pushdown(int p,int l,int r){//add*mul first;
if(tree[p].mul==1&&tree[p].add==0) return;
int mid=l+r>>1;
tree[p<<1].mul=tree[p<<1].mul*tree[p].mul%mod;
tree[p<<1].add=(tree[p<<1].add*tree[p].mul+tree[p].add)%mod;
tree[p<<1].sum=(tree[p<<1].sum*tree[p].mul+tree[p].add*(mid-l+1))%mod;
tree[p<<1|1].mul=tree[p<<1|1].mul*tree[p].mul%mod;
tree[p<<1|1].add=(tree[p<<1|1].add*tree[p].mul+tree[p].add)%mod;
tree[p<<1|1].sum=(tree[p<<1|1].sum*tree[p].mul+tree[p].add*(r-mid))%mod;
tree[p].add=0;tree[p].mul=1;
}
inline void change(int p,int l,int r,int x,int y,int op,ll c){
if(x<=l&&r<=y){
if(op==1){
tree[p].mul=tree[p].mul*c%mod;// tips:*=优先级低。。dont't use that!
tree[p].add=tree[p].add*c%mod;
tree[p].sum=tree[p].sum*c%mod;
}
else{
tree[p].add=(tree[p].add+c)%mod;
tree[p].sum=(tree[p].sum+c*(r-l+1))%mod;
}
return;
}
pushdown(p,l,r);
int mid=l+r>>1;
if(x<=mid) change(p<<1,l,mid,x,y,op,c);
if(y>mid) change(p<<1|1,mid+1,r,x,y,op,c);
tree[p].sum=(tree[p<<1].sum+tree[p<<1|1].sum)%mod; //注意维护sum
}
inline ll query(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return tree[p].sum;
pushdown(p,l,r);
int mid=l+r>>1;ll res=0;
if(x<=mid) res+=query(p<<1,l,mid,x,y);
if (y>mid) res+=query(p<<1|1,mid+1,r,x,y);
return res%mod;
}
int main(){
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
n=read();mod=read();
for(int i=1;i<=n;++i) a[i]=read(),a[i]%=mod;
build(1,1,n);m=read();
while(m--){
int op=read(),x=read(),y=read();
if(op==3){
printf("%lld\n",query(1,1,n,x,y));
}
else{
ll c=read();
change(1,1,n,x,y,op,c%mod);
}
}
return 0;
}