1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #include <bits/stdc++.h> #define int long long #define l(x) tree[x].l #define r(x) tree[x].r #define sum(x) tree[x].sum #define add(x) tree[x].add #define mul(x) tree[x].mul using namespace std;
struct SegmentTree{ int l,r; int sum,add,mul; }tree[100010*4];
int a[100010],n,m,mod;
int read(){ int x=0; char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return x; }
void build(int p,int l,int r){ l(p)=l,r(p)=r; mul(p)=1; if(l==r){ sum(p)=a[l]%mod; return; } int mid=(l+r)/2; build(p*2,l,mid); build(p*2+1,mid+1,r); sum(p)=(sum(p*2)+sum(p*2+1))%mod; }
void spread(int p){ sum(p*2)=(int)(mul(p)*sum(p*2)+((r(p*2)-l(p*2)+1)*add(p))%mod)%mod; sum(p*2+1)=(int)(mul(p)*sum(p*2+1)+(add(p)*(r(p*2+1)-l(p*2+1)+1))%mod)%mod; mul(p*2)=(int)(mul(p*2)*mul(p))%mod; mul(p*2+1)=(int)(mul(p*2+1)*mul(p))%mod; add(p*2)=(int)(add(p*2)*mul(p)+add(p))%mod; add(p*2+1)=(int)(add(p*2+1)*mul(p)+add(p))%mod; mul(p)=1; add(p)=0; }
void change_add(int p,int l,int r,int d){ if(l<=l(p)&&r>=r(p)){ sum(p)=(int)(sum(p)+d*(r(p)-l(p)+1))%mod; add(p)=(add(p)+d)%mod; return; } spread(p); sum(p)=(sum(p*2)+sum(p*2+1))%mod; int mid=(l(p)+r(p))/2; if(l<=mid) change_add(p*2,l,r,d); if(r>mid) change_add(p*2+1,l,r,d); sum(p)=(sum(p*2)+sum(p*2+1))%mod; }
void change_mul(int p,int l,int r,int d){ if(l<=l(p)&&r>=r(p)){ mul(p)=(mul(p)*d)%mod; sum(p)=(sum(p)*d)%mod; add(p)=(add(p)*d)%mod; return; } spread(p); sum(p)=sum(p*2)+sum(p*2+1); int mid=(l(p)+r(p))/2; if(l<=mid) change_mul(p*2,l,r,d); if(r>mid) change_mul(p*2+1,l,r,d); sum(p)=(sum(p*2)+sum(p*2+1))%mod; }
int ask(int p,int l,int r){ if(l<=l(p)&&r>=r(p)) return sum(p); spread(p); int mid=(l(p)+r(p))/2; int val=0; if(l<=mid) val=(val+ask(p*2,l,r))%mod; if(r>mid) val=(val+ask(p*2+1,l,r))%mod; return val; }
signed main(){ cin>>n>>m>>mod; for(int i=1;i<=n;i++) a[i]=read(); build(1,1,n); while(m--){ int op; int l,r,d; scanf("%lld%lld%lld",&op,&l,&r); if(op==1){ scanf("%lld",&d); change_mul(1,l,r,d); } else if(op==2){ scanf("%lld",&d); change_add(1,l,r,d); } else{ cout<<ask(1,l,r)<<"\n"; } } return 0; }
|