#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lc p<<1
#define rc p<<1|1
const int N=100005;
struct node{
int sum,add,mul,l,r;
}tree[N*4];
int a[N];
int n,m,mod;
void push_up(int p){
tree[p].sum=(tree[lc].sum+tree[rc].sum)%mod;
}
void build(int l,int r,int p){
tree[p].l=l,tree[p].r=r,tree[p].mul=1;
if(l==r){
tree[p].sum=a[l];
return ;
}
int mid=(l+r)/2;
build(l,mid,lc);
build(mid+1,r,rc);
push_up(p);
}
void push_down(int p){
if(tree[p].mul!=1){
tree[lc].mul=(tree[lc].mul*tree[p].mul)%mod;
tree[rc].mul=(tree[rc].mul*tree[p].mul)%mod;
tree[lc].sum=(tree[lc].sum*tree[p].mul)%mod;
tree[rc].sum=(tree[rc].sum*tree[p].mul)%mod;
tree[lc].add=(tree[lc].add*tree[p].mul)%mod;
tree[rc].add=(tree[rc].add*tree[p].mul)%mod;
tree[p].mul=1;
}
if(tree[p].add!=0){
int len1=tree[lc].r-tree[lc].l+1;
int len2=tree[rc].r-tree[rc].l+1;
tree[lc].sum=(tree[lc].sum+tree[p].add*len1)%mod;
tree[rc].sum=(tree[rc].sum+tree[p].add*len2)%mod;
tree[lc].add=(tree[lc].add+tree[p].add)%mod;
tree[lc].add=(tree[lc].add+tree[p].add)%mod;
tree[p].add=0;
}
}
void update1(int l,int r,int k,int p){//加法
if(l<=tree[p].l&&r>=tree[p].r){
int len=tree[p].r-tree[p].l+1;
tree[p].sum=(tree[p].sum+k*len)%mod;
tree[p].add=(tree[p].add+k)%mod;
return ;
}
push_down(p);
int mid=(tree[p].l+tree[p].r)/2;
if(l<=mid)
update1(l,r,k,lc);
if(r>mid)
update1(l,r,k,rc);
push_up(p);
}
void update2(int l,int r,int k,int p){//乘法
if(l<=tree[p].l&&r>=tree[p].r){
tree[p].sum=(tree[p].sum*k)%mod;
tree[p].add=(tree[p].add*k)%mod;
tree[p].mul=(tree[p].mul*k)%mod;
return ;
}
push_down(p);
int mid=(tree[p].l+tree[p].r)/2;
if(l<=mid)
update2(l,r,k,lc);
if(r>mid)
update2(l,r,k,rc);
push_up(p);
}
int query(int l,int r,int p){
if(l<=tree[p].l&&r>=tree[p].r)
return tree[p].sum;
push_down(p);
int res=0;
int mid=(tree[p].l+tree[p].r)/2;
if(l<=mid)
res=(res+query(l,r,lc))%mod;
if(r>mid)
res=(res+query(l,r,rc))%mod;
return res;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m>>mod;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,n,1);
while(m--){
int op;
cin>>op;
if(op==1){
int x,y,k;
cin>>x>>y>>k;
update2(x,y,k%mod,1);
}
if(op==2){
int x,y,k;
cin>>x>>y>>k;
update1(x,y,k%mod,1);
}
if(op==3){
int x,y;
cin>>x>>y;
cout<<query(x,y,1)%mod<<'\n';
}
for(int i=1;i<=n;i++)
cout<<query(i,i,1)<<" ";
cout<<endl;
}
return 0;
}
样例都没过,题面: P3373 【模板】线段树 2 - 洛谷