- 【模板】线段树 2
to yzz
- 2024-12-20 13:05:41 @
#include<iostream>
#define int long long
using namespace std;
int a[1145141],f[1145141],d[1145141],f2[1145141];
//a表示原数组,f表示懒标记加,f2表示懒标记乘,d表示线段树
int n,q,mod;
void pushdamn(int s,int t,int p){
int mid=(s+t)>>1;
d[p*2]=(d[p*2]*f2[p]+f[p]*(mid-s+1))%mod;
d[p*2+1]=(d[p*2+1]*f2[p]+f[p]*(t-mid))%mod;
f2[p*2]=(f2[p]*f2[p*2])%mod;
f2[p*2+1]=(f2[p]*f2[p*2+1])%mod;
f[p*2]=(f[p*2]*f2[p]+f[p])%mod;
f[p*2+1]=(f[p*2+1]*f2[p]+f[p])%mod;
f2[p]=1;
f[p]=0;
return;
}
void build(int s,int t,int p){
f2[p]=1;
f[p]=0;
if (s==t){
d[p]=a[s];
d[p]%=mod;
return;
}
int mid=(s+t)>>1;
build(s,mid,p*2);
build(mid+1,t,p*2+1);
d[p]=(d[p*2+1]+d[p*2])%mod;
return;
}
void update(int l,int r,int s,int t,int c,int p){ //加法更新
if (l<=s && t<=r){
d[p]+=(t-s+1)*c;
f[p]+=c;
d[p]%=mod;
f[p]%=mod;
return;
}
int mid=(s+t)>>1;
pushdamn(s,t,p);
if (l<=mid)update(l,r,s,mid,c,p*2);
if (mid<r)update(l,r,mid+1,t,c,p*2+1);
d[p]=(d[p*2]+d[p*2+1])%mod;
return;
}
void update2(int l,int r,int s,int t,int c,int p){ //乘法更新
if (l<=s && t<=r){
d[p]*=c;
f2[p]*=c;
f[p]*=c;
d[p]%=mod;
f2[p]%=mod;
f[p]%=mod;
return;
}
int mid=(s+t)>>1;
pushdamn(s,t,p);
if (l<=mid)update2(l,r,s,mid,c,p*2);//update改为 update2
if (mid<r)update2(l,r,mid+1,t,c,p*2+1);//update改为 update2
d[p]=(d[p*2]+d[p*2+1])%mod;
return;
}
int getsum(int l,int r,int s,int t,int p){
if (l<=s && t<=r)return d[p];
pushdamn(s,t,p);
int mid=(s+t)>>1;
int sum=0;
if (l<=mid)sum+=getsum(l,r,s,mid,p*2);//l<=s改为 l<=mid
if (r>mid)sum+=getsum(l,r,mid+1,t,p*2+1);//t<r改为 r>mid
return sum;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q >> mod;
for (int i=1;i<=n;i++){
cin >> a[i];
}
build(1,n,1);
while(q--){
int ope,x,y;
cin >> ope >> x >> y;
if (ope==1){
int k;
cin >> k;
update2(x,y,1,n,k,1);
}else if(ope==2){
int k;
cin >> k;
update(x,y,1,n,k,1);
}else{
cout << getsum(x,y,1,n,1)%mod << "\n";
}
}
return 0;
}
0 comments
No comments so far...
Information
- ID
- 287
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- # Submissions
- 69
- Accepted
- 12
- Uploaded By