-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGeneral Node Lazy SGT.CPP
151 lines (138 loc) · 3.57 KB
/
General Node Lazy SGT.CPP
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
struct Data { // change this
ll val;
Data(ll x) {
val = x;
}
};
class SegTree {
public:
vector<Data> st;
vector<Data> lazy;
vector<bool> isLazy;
Data ID = Data(0); // change this
int n = 0;
SegTree(int s) {
n = s;
st.assign(4*n+5, ID);
lazy.assign(4*n+5, ID);
isLazy.assign(4*n+5, false);
}
void merge(Data& curr, Data& a, Data& b) { // change this
curr.val = a.val + b.val;
}
void propogate(int node, int L, int R) { // change this
if(L!=R) {
isLazy[2*node] = 1;
isLazy[2*node + 1] = 1;
lazy[2*node] = lazy[node];
lazy[2*node + 1] = lazy[node];
}
st[node].val = (R-L+1)*lazy[node].val;
lazy[node] = ID;
isLazy[node] = 0;
}
void Build(int node, int L, int R, vector<ll>& arr) {
if(L==R) {
st[node].val = arr[L];
return;
}
int M = (L + R) / 2;
Build(2*node, L, M, arr);
Build(2*node+1, M+1, R, arr);
merge(st[node], st[2*node], st[2*node+1]);
}
void pUpdate(int node, int L, int R, int pos, ll val) {
if(L==R) {
st[node].val = val;
return;
}
int M = (L + R) / 2;
if(isLazy[2*node]) {
propogate(2*node, L, M);
}
if(isLazy[2*node+1]) {
propogate(2*node+1, M+1, R);
}
if(pos<=M) {
pUpdate(2*node, L, M, pos, val);
}
else {
pUpdate(2*node+1, M+1, R, pos, val);
}
merge(st[node], st[2*node], st[2*node+1]);
}
void Update(int node, int L, int R, int lo, int hi, ll val) {
if(R<lo || L>hi) {
return;
}
if(lo<=L && R<=hi) {
isLazy[node] = 1;
lazy[node].val = val;
propogate(node, L, R);
return;
}
int M = (L + R) / 2;
if(isLazy[2*node]) {
propogate(2*node, L, M);
}
if(isLazy[2*node+1]) {
propogate(2*node+1, M+1, R);
}
Update(2*node, L, M, lo, hi, val);
Update(2*node+1, M+1, R, lo, hi, val);
merge(st[node], st[2*node], st[2*node+1]);
}
Data Query(int node, int L, int R, int lo, int hi) {
if(R<lo || L>hi) {
return ID;
}
if(lo<=L && R<=hi) {
return st[node];
}
int M = (L + R) / 2;
if(isLazy[2*node]) {
propogate(2*node, L, M);
}
if(isLazy[2*node+1]) {
propogate(2*node+1, M+1, R);
}
Data lft = Query(2*node, L, M, lo, hi);
Data rght = Query(2*node+1, M+1, R, lo, hi);
Data curr = ID;
merge(curr, lft, rght);
return curr;
}
Data pQuery(int node, int L, int R, int pos) {
if(L==R) {
return st[node];
}
int M = (L + R) / 2;
if(isLazy[2*node]) {
propogate(2*node, L, M);
}
if(isLazy[2*node+1]) {
propogate(2*node+1, M+1, R);
}
if(pos<=M) {
return pQuery(2*node, L, M, pos);
}
else {
return pQuery(2*node+1, M+1, R, pos);
}
}
void build(vector<ll>& arr) {
Build(1, 1, n, arr);
}
void update(int pos, ll val) {
pUpdate(1, 1, n, pos, val);
}
void update(int l, int r, ll val) {
Update(1, 1, n, l, r, val);
}
Data query(int lo, int hi) {
return Query(1, 1, n, lo, hi);
}
Data query(int pos) {
return pQuery(1, 1, n, pos);
}
};