照着题解打的,检查了好多遍没看出问题
样例输出 20 18
请大佬帮忙看看(如果是低级错误那不好意思www
)
十分谢谢
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e5+7;
ll b[MAXN];
ll n,m,mod;
struct node{
ll l,r;
ll sum;
ll mul;
ll add;
}a[MAXN*4];
inline ll read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
x=x*10+ch-'0',ch=getchar();
return x*f;
}
void update(ll k){
a[k].sum=(a[k*2].sum+a[k*2+1].sum)%mod;
return;
}
void pushdown(ll k){
// if(a[k].l==a[k].r){
// a[k].add=0;
// a[k].mul=1;
// return;
// }
a[k*2].sum=(a[k*2].sum*a[k].mul+(a[k*2].r-a[k*2].l+1)*a[k].add)%mod;
a[k*2+1].sum=(a[k*2+1].sum*a[k].mul+(a[k*2+1].r-a[k*2+1].l+1)*a[k].add)%mod;
a[k*2].mul=(a[k*2].mul*a[k].mul)%mod;
a[k*2+1].mul=(a[k*2+1].mul*a[k].mul)%mod;
a[k*2].add=(a[k*2].add*a[k].mul+a[k].add)%mod;
a[k*2+1].add=(a[k*2+1].add*a[k].mul+a[k].add)%mod;
a[k].add=0;
a[k].mul=1;
}
void build(ll p,ll l,ll r){
a[p].l=l;a[p].r=r;
a[p].mul=1;
a[p].add=0;
if(l==r){
a[p].sum=b[l]%mod;
return;
}
ll mid=(l+r)/2;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
update(p);
}
void add(ll p,ll x,ll y,ll k){//区间加
if(a[p].l>=x&&a[p].r<=y){
a[p].sum=(a[p].sum+(y-x+1)*k)%mod;
a[p].add=(a[p].add+k)%mod;
return;
}
pushdown(p);
ll mid=(a[p].l+a[p].r)/2;
if(x<=mid) add(p*2,x,y,k);
if(y>mid) add(p*2+1,x,y,k);
update(p);
return;
// else{
// add(p*2,x,mid,k);
// add(p*2+1,mid+1,y,k);
// }
}
void mul(ll p,ll x,ll y,ll k){
if(a[p].l>=x&&a[p].r<=y){
a[p].add=(a[p].add*k)%mod;
a[p].mul=(a[p].mul*k)%mod;
a[p].sum=(a[p].sum*k)%mod;
return;
}
pushdown(p);
ll mid=(a[p].l+a[p].r)/2;
if(x<=mid) mul(p*2,x,y,k);
if(y>mid) mul(p*2+1,x,y,k);
update(p);
return;
// else{
// mul(p*2,x,mid,k);
// mul(p*2+1,mid+1,y,k);
// }
}
ll ask(ll p,ll x,ll y){
if(a[p].l>=x&&a[p].r<=y){
return a[p].sum;
}
pushdown(p);
ll mid=(a[p].l+a[p].r)/2;
ll ans=0;
if(x<=mid) ans=(ans+ask(p*2,x,y))%mod;
if(y>mid) ans=(ans+ask(p*2+1,x,y))%mod;
return ans;
// else{
// return (ask(p*2,x,mid)+ask(p*2+1,mid+1,y))%mod;
// }
}
int main(){
n=read();
m=read();
mod=read();
for(ll i=1;i<=n;i++){
b[i]=read();
}
build(1,1,n);
for(ll i=1;i<=m;i++){
ll s,x,y,k;
s=read();
if(s==1){
x=read();y=read();k=read();
mul(1,x,y,k);
}
if(s==2){
x=read();y=read();k=read();
add(1,x,y,k);
}
if(s==3){
x=read();y=read();
cout<<ask(1,x,y)<<endl;
}
}
return 0;
}