萌新刚学线段树 自己看了五遍代码都没找到问题 求大佬帮忙看一下
#include<cstdio>
using namespace std;
int n,tree[100001*4],add[100001*4],mul[100001*4];
int m,p,x[100001];
int lson(int pos)
{
return pos<<1;
}
int rson(int pos)
{
return pos<<1|1;
}
void push_up(int pos)
{
tree[pos]=tree[lson(pos)]+tree[rson(pos)];
}
void build(int pos,int l,int r)
{
add[pos]=0;
mul[pos]=1;
if(l==r)
{
tree[pos]=x[l];
return;
}
int m=(l+r)>>1;
build(lson(pos),l,m);
build(rson(pos),m+1,r);
push_up(pos);
}
void Add(int &x,int y)
{
x=(x+y)%p;
}
void Mul(int &x,int y)
{
x=x*y%p;
}
void push_down(int pos,int l,int r)
{
int m=(l+r)>>1;
Mul(mul[lson(pos)],mul[pos]);
Mul(add[lson(pos)],mul[pos]);
Add(add[lson(pos)],add[pos]);
Mul(tree[lson(pos)],mul[pos]);
Add(tree[lson(pos)],add[pos]*(m-l+1));
Mul(mul[rson(pos)],mul[pos]);
Mul(add[rson(pos)],mul[pos]);
Add(add[rson(pos)],add[pos]);
Mul(tree[rson(pos)],mul[pos]);
Add(tree[rson(pos)],add[pos]*(r-m));
add[pos]=0;
mul[pos]=1;
}
void update_add(int x,int y,int pos,int l,int r,int k)
{
if(x<=l&&r<=y)
{
Add(add[pos],k);
Add(tree[pos],k*(r-l+1));
return;
}
push_down(pos,l,r);
int m=(l+r)>>1;
if(x<=m)
update_add(x,y,lson(pos),l,m,k);
if(y>m)
update_add(x,y,rson(pos),m+1,r,k);
push_up(pos);
}
void update_mul(int x,int y,int pos,int l,int r,int k)
{
if(x<=l&&r<=y)
{
Mul(add[pos],k);
Mul(mul[pos],k);
Mul(tree[pos],k);
return;
}
push_down(pos,l,r);
int m=(l+r)>>1;
if(x<=m)
update_mul(x,y,lson(pos),l,m,k);
if(y>m)
update_mul(x,y,rson(pos),m+1,r,k);
push_up(pos);
}
int query(int x,int y,int pos,int l,int r)
{
if(x<=l&&r<=y)
return tree[pos];
int m=(l+r)>>1,ans=0;
if(x<=m)
Add(ans,query(x,y,lson(pos),l,m));
if(y>m)
Add(ans,query(x,y,rson(pos),m+1,r));
return ans;
}
void build()
{
build(1,1,n);
}
void update_add(int x,int y,int k)
{
update_add(x,y,1,1,n,k);
}
void update_mul(int x,int y,int k)
{
update_mul(x,y,1,1,n,k);
}
int query(int x,int y)
{
return query(x,y,1,1,n);
}
int main()
{
scanf("%d %d %d",&n,&m,&p);
for(int i=1;i<=n;i++)
scanf("%d",&x[i]);
build();
for(int i=1;i<=m;i++)
{
int op;
scanf("%d",&op);
if(op==1)
{
int x,y,k;
scanf("%d %d %d",&x,&y,&k);
update_mul(x,y,k);
}
if(op==2)
{
int x,y,k;
scanf("%d %d %d",&x,&y,&k);
update_add(x,y,k);
}
if(op==3)
{
int x,y;
scanf("%d %d",&x,&y);
printf("%d\n",query(x,y));
}
}
}