-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegment Tree.cpp
More file actions
150 lines (150 loc) · 3.07 KB
/
Copy pathSegment Tree.cpp
File metadata and controls
150 lines (150 loc) · 3.07 KB
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
#include <iostream>
using namespace std;
const int inf = 1e9;
#define lson p << 1
#define rson p << 1 | 1
struct SegNode
{
int sum;
int l, r;
int add, mul, tag;
} tree[40005];
void pushup(int p)
{
tree[p].sum = tree[lson].sum + tree[rson].sum;
}
void pushdown(int p)
{
if(tree[p].tag != inf)
{
tree[lson].sum = (tree[lson].r - tree[lson].l + 1) * tree[p].tag;
tree[rson].sum = (tree[rson].r - tree[rson].l + 1) * tree[p].tag;
tree[lson].add = tree[rson].add = 0;
tree[lson].mul = tree[rson].mul = 1;
tree[lson].tag = tree[rson].tag = tree[p].tag;
}
else
{
tree[lson].sum = tree[lson].sum * tree[p].mul + (tree[lson].r - tree[lson].l + 1) * tree[p].add;
tree[rson].sum = tree[rson].sum * tree[p].mul + (tree[rson].r - tree[rson].l + 1) * tree[p].add;
tree[lson].add = tree[p].add + tree[lson].add * tree[p].mul;
tree[rson].add = tree[p].add + tree[rson].add * tree[p].mul;
tree[lson].mul *= tree[p].mul;
tree[rson].mul *= tree[p].mul;
}
tree[p].add = 0;
tree[p].mul = 1;
tree[p].tag = inf;
}
void build(int p, int l, int r)
{
tree[p].l = l;
tree[p].r = r;
tree[p].add = 0;
tree[p].mul = 1;
tree[p].tag = inf;
if(l == r)
{
cin >> tree[p].sum;
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
pushup(p);
}
void modifyAdd(int p, int L, int R, int k)
{
if(L <= tree[p].l && tree[p].r <= R)
{
tree[p].sum += (tree[p].r - tree[p].l + 1) * k;
tree[p].add += k;
return;
}
pushdown(p);
int mid = (tree[p].l + tree[p].r) >> 1;
if(L <= mid)
modifyAdd(lson, L, R, k);
if(mid < R)
modifyAdd(rson, L, R, k);
pushup(p);
}
void modifyMul(int p, int L, int R, int k)
{
if(L <= tree[p].l && tree[p].r <= R)
{
tree[p].sum *= k;
tree[p].add *= k;
tree[p].mul *= k;
return;
}
pushdown(p);
int mid = (tree[p].l + tree[p].r) >> 1;
if(L <= mid)
modifyMul(lson, L, R, k);
if(mid < R)
modifyMul(rson, L, R, k);
pushup(p);
}
void modifyAssign(int p, int L, int R, int k)
{
if(L <= tree[p].l && tree[p].r <= R)
{
tree[p].sum = (tree[p].r - tree[p].l + 1) * k;
tree[p].add = 0;
tree[p].mul = 1;
tree[p].tag = k;
return;
}
pushdown(p);
int mid = (tree[p].l + tree[p].r) >> 1;
if(L <= mid)
modifyAssign(lson, L, R, k);
if(mid < R)
modifyAssign(rson, L, R, k);
pushup(p);
}
int querySum(int p, int L, int R)
{
if(L <= tree[p].l && tree[p].r <= R)
return tree[p].sum;
pushdown(p);
int mid = (tree[p].l + tree[p].r) >> 1, ans = 0;
if(L <= mid)
ans += querySum(lson, L, R);
if(mid < R)
ans += querySum(rson, L, R);
return ans;
}
int main()
{
int n, q;
cin >> n >> q;
build(1, 1, n);
while(q--)
{
int op, l, r, k;
cin >> op >> l >> r;
if(op == 1)
{
cin >> k;
modifyAdd(1, l, r, k);
}
if(op == 2)
{
cin >> k;
modifyMul(1, l, r, k);
}
if(op == 3)
{
cin >> k;
modifyAssign(1, l, r, k);
}
if(op == 4)
cout << querySum(1, l, r) << endl;
for(int i = 1; i <= n; i++)
cout << querySum(1, i, i) << ' ';
cout << endl;
}
return 0;
}