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
142
143
144
145
146
147
148
149
150
151
152
| #include <bits/stdc++.h>
#define int long long
#define maxn 400010
using namespace std;
int n, m, p;
struct a
{
int v, mul, add;
} xds[maxn];
int num[maxn];
inline void build(int root, int l, int r)
{
xds[root].add = 0;
xds[root].mul = 1;
if (l == r)
{
xds[root].v = num[l] % p;
return;
}
int m = (l + r) / 2;
build(root * 2, l, m);
build(root * 2 + 1, m + 1, r);
xds[root].v = (xds[root * 2].v + xds[root * 2 + 1].v) % p;
}
inline void pushdown(int root, int l, int r)
{
int m = (l + r) / 2;
//value
xds[root * 2].v *= xds[root].mul;
xds[root * 2].v %= p;
xds[root * 2].v += xds[root].add * (m - l + 1);
xds[root * 2].v %= p;
xds[root * 2 + 1].v *= xds[root].mul;
xds[root * 2 + 1].v %= p;
xds[root * 2 + 1].v += xds[root].add * (r - m);
xds[root * 2 + 1].v %= p;
//lazytag
xds[root * 2].mul *= xds[root].mul;
xds[root * 2].mul %= p;
xds[root * 2].add *= xds[root].mul;
xds[root * 2].add %= p;
xds[root * 2].add += xds[root].add;
xds[root * 2].add %= p;
xds[root * 2 + 1].mul *= xds[root].mul;
xds[root * 2 + 1].mul %= p;
xds[root * 2 + 1].add *= xds[root].mul;
xds[root * 2 + 1].add %= p;
xds[root * 2 + 1].add += xds[root].add;
xds[root * 2 + 1].add %= p;
//init
xds[root].mul = 1;
xds[root].add = 0;
}
inline void mul(int root, int l, int r, int f, int t, int k)
{
if (r < f || t < l)
{
return;
}
if (f <= l && r <= t)
{
xds[root].mul *= k;
xds[root].mul %= p;
xds[root].add *= k;
xds[root].add %= p;
xds[root].v *= k;
xds[root].v %= p;
return;
}
int m = (l + r) / 2;
pushdown(root, l, r);
mul(root * 2, l, m, f, t, k);
mul(root * 2 + 1, m + 1, r, f, t, k);
xds[root].v = (xds[root * 2].v + xds[root * 2 + 1].v) % p;
}
inline void add(int root, int l, int r, int f, int t, int k)
{
if (r < f || t < l)
{
return;
}
if (f <= l && r <= t)
{
xds[root].add += k;
xds[root].add %= p;
xds[root].v += k * (r - l + 1);
xds[root].v %= p;
return;
}
int m = (l + r) / 2;
pushdown(root, l, r);
add(root * 2, l, m, f, t, k);
add(root * 2 + 1, m + 1, r, f, t, k);
xds[root].v = (xds[root * 2].v + xds[root * 2 + 1].v) % p;
}
inline int query(int root, int l, int r, int f, int t)
{
if (r < f || t < l)
{
return 0;
}
if (f <= l && r <= t)
{
return xds[root].v % p;
}
pushdown(root, l, r);
int m = (l + r) / 2;
return (query(root * 2, l, m, f, t) + query(root * 2 + 1, m + 1, r, f, t)) % p;
}
signed main()
{
int tp1, tp2, tp3, tp4;
scanf("%lld%lld%lld", &n, &m, &p);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &num[i]);
}
build(1, 1, n);
while (m--)
{
scanf("%lld", &tp1);
switch (tp1)
{
case 1:
scanf("%lld%lld%lld", &tp2, &tp3, &tp4);
mul(1, 1, n, tp2, tp3, tp4);
break;
case 2:
scanf("%lld%lld%lld", &tp2, &tp3, &tp4);
add(1, 1, n, tp2, tp3, tp4);
break;
case 3:
scanf("%lld%lld", &tp2, &tp3);
cout << query(1, 1, n, tp2, tp3) << endl;
break;
}
}
return 0;
}
|