题目链接

P3373 【模板】线段树 2

代码

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
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int sz=1e5+10;

inline void re(int &x) {
x=0;char ch=getchar();bool nf=0;
while(ch<'0' || ch>'9') nf|=(ch=='-'),ch=getchar();
while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
if(nf) x=-x;
}

int n,m,p,a[sz],t[sz<<2],lazy1[sz<<2],lazy2[sz<<2];

inline void pushup(int o,int l,int r) {
t[o]=(t[o<<1]+t[o<<1|1])%p;
}

inline void pushdown(int o,int l,int r) {
int mid=(l+r)>>1;
(t[o<<1]*=lazy2[o])%=p,(t[o<<1|1]*=lazy2[o])%=p;
(lazy2[o<<1]*=lazy2[o])%=p,(lazy2[o<<1|1]*=lazy2[o])%=p;
lazy1[o<<1]=lazy1[o<<1]*lazy2[o]%p,lazy1[o<<1|1]=lazy1[o<<1|1]*lazy2[o]%p;
lazy2[o]=1;
t[o<<1]=(t[o<<1]+lazy1[o]*(mid-l+1))%p;t[o<<1|1]=(t[o<<1|1]+lazy1[o]*(r-mid))%p;
lazy1[o<<1]+=lazy1[o];lazy1[o<<1]%=p;lazy1[o<<1|1]+=lazy1[o];lazy1[o<<1|1]%=p;
lazy1[o]=0;
}

void build(int o,int l,int r) {
lazy1[o]=0,lazy2[o]=1;
if(l==r) {
t[o]=a[l];
return;
}
int mid=(l+r)>>1;
build(o<<1,l,mid),build(o<<1|1,mid+1,r);
pushup(o,l,r);
}

void update1(int o,int l,int r,int li,int ri,int k) {
if(l>=li && r<=ri) {
(t[o]+=(r-l+1)*k)%=p;
(lazy1[o]+=k)%=p;
return;
}
pushdown(o,l,r);
int mid=(l+r)>>1;
if(li<=mid) update1(o<<1,l,mid,li,ri,k);
if(ri>mid) update1(o<<1|1,mid+1,r,li,ri,k);
pushup(o,l,r);
}

void update2(int o,int l,int r,int li,int ri,int k) {
if(l>=li && r<=ri) {
(t[o]*=k)%=p;
(lazy2[o]*=k)%=p;
(lazy1[o]*=k)%=p;
return;
}
pushdown(o,l,r);
int mid=(l+r)>>1;
if(li<=mid) update2(o<<1,l,mid,li,ri,k);
if(ri>mid) update2(o<<1|1,mid+1,r,li,ri,k);
pushup(o,l,r);
}

int query(int o,int l,int r,int li,int ri) {
if(l>=li && r<=ri) return t[o];
pushdown(o,l,r);
int mid=(l+r)>>1,ret=0;
if(li<=mid) (ret+=query(o<<1,l,mid,li,ri))%=p;
if(ri>mid) (ret+=query(o<<1|1,mid+1,r,li,ri))%=p;
return ret;
}

signed main() {
// freopen("testdata.in","r",stdin);
re(n),re(m),re(p);
for(int i=1;i<=n;++i) {
re(a[i]);
a[i]%=p;
}
build(1,1,n);
while(m--) {
int typ;re(typ);
if(typ==1) {
int x,y,k;
re(x),re(y),re(k);
k%=p;
update2(1,1,n,x,y,k);
} else if(typ==2) {
int x,y,k;
re(x),re(y),re(k);
k%=p;
update1(1,1,n,x,y,k);
} else if(typ==3) {
int x,y;
re(x),re(y);
printf("%lld\n",query(1,1,n,x,y));
}
}
return 0;
}

 Comments