-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplicit_SplayTree.cpp
More file actions
292 lines (250 loc) · 8.1 KB
/
Implicit_SplayTree.cpp
File metadata and controls
292 lines (250 loc) · 8.1 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <bits/stdc++.h>
using namespace std;
#define fixed(n) fixed << setprecision(n)
#define ceil(n, m) (((n) + (m) - 1) / (m))
#define add_mod(a, b, m) (((a % m) + (b % m)) % m)
#define sub_mod(a, b, m) (((a % m) - (b % m) + m) % m)
#define mul_mod(a, b, m) (((a % m) * (b % m)) % m)
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
#define sz(x) int(x.size())
#define debug(x) cout << #x << ": " << (x) << "\n";
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define EPS 1e-9
constexpr int INF = 1 << 30, Mod = 1e9 + 7;
constexpr ll LINF = 1LL << 62;
#define PI acos(-1)
template < typename T = int > using Pair = pair < T, T >;
vector < string > RET = {"NO", "YES"};
template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
for (auto &x : v) in >> x;
return in;
}
template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
for (const T &x : v) out << x << ' ';
return out;
}
struct Data {
ll val, sum, pref, suff, max_seg;
Data() : val(0), sum(0), pref(-LINF), suff(-LINF), max_seg(-LINF) {}
Data(ll v) : val(v), sum(val), pref(val), suff(val), max_seg(val) {}
};
Data combine(const Data& a, const Data& b){
Data res;
res.sum = a.sum + b.sum;
res.pref = max(a.pref, a.sum + b.pref);
res.suff = max(b.suff, b.sum + a.suff);
res.max_seg = max({a.max_seg, b.max_seg, a.suff + b.pref});
return res;
}
template < typename T = int > struct SplayTree { // 0-indexed
struct Node {
Node *ch[2], *par;
T val, update;
int subSz;
bool is_lazy;
Node() : subSz(0), update(0), is_lazy(false) {
par = ch[0] = ch[1] = this;
}
Node(T V) : val(V), subSz(1), update(0), is_lazy(false) {
par = ch[0] = ch[1] = EMPTY;
}
// update node values
void update() {
subSz = ch[0] -> subSz + ch[1] -> subSz + 1;
auto v = val.val;
val = combine(ch[0] -> val, combine(Data(v), ch[1] -> val));
val.val = v;
}
// push down lazy propagation
void push_down(){
if(this == EMPTY || !is_lazy) return;
val = Data(update * subSz);
ch[0] -> lazy_update(update);
ch[1] -> lazy_update(update);
is_lazy = false;
}
// lazy change
void lazy_update(ll c){
if(this == EMPTY) return;
update = c;
is_lazy = true;
}
};
static Node* EMPTY;
Node *root;
enum dir {LEFT, RIGHT};
#define returnType ll
SplayTree(){
root = EMPTY;
}
// Link two nodes with direction d
void link(Node *p, Node *c, int d){
if(p != EMPTY) p -> ch[d] = c, p -> update();
if(c != EMPTY) c -> par = p;
}
// 0 for LEFT, 1 for RIGHT
int get_dir(Node *p, Node *c){
return p -> ch[RIGHT] == c;
}
/*
gp gp
| gd | gd
p q
/ \d !d/ \
a q -> <- p c
!d/ \ / \d
b c a b
*/
// rotate node p with direction d
void rotate(Node *p, int d){
Node *q = p -> ch[d];
Node *gp = p -> par;
int gd = get_dir(gp, p);
link(p, q -> ch[!d], d);
link(q, p, !d);
link(gp, q, gd);
}
// splay node p to the root of the tree it belongs to
void splay(Node *q){
// splay p until it becomes the root of the tree
while(q -> par != EMPTY){
Node *p = q -> par;
Node *gp = p -> par;
int d1 = get_dir(p, q);
int d2 = get_dir(gp, p);
if(gp == EMPTY){ // direct parent
rotate(p, d1);
}else if(d1 == d2){ // zig-zig
rotate(gp, d2);
rotate(p, d1);
}else { // zig-zag
rotate(p, d1);
rotate(gp, d2);
}
}
root = q;
}
// split tree into two trees, one with values less than val and the other with values greater than or equal val
void split(Node *p, int idx, Node* &ls, Node * &ge){
if(idx >= p -> subSz) {
ls = p, ge = EMPTY;
return;
}
p = splay_by_idx(p, idx);
ls = p -> ch[LEFT];
ge = p;
link(ge, EMPTY, LEFT);
link(EMPTY, ls, RIGHT);
}
// make the node with index idx the root of the tree
Node* splay_by_idx(Node* p, int idx){
p = at(p, idx);
splay(p);
return p;
}
// merge two trees into one tree
Node* merge(Node *ls, Node *ge){
if(ls == EMPTY) return ge;
if(ge == EMPTY) return ls;
ge = splay_by_idx(ge, 0);
link(ge, ls, LEFT);
return ge;
}
// merge two trees into one tree
void merge(Node* p){
root = merge(root, p);
}
// find node with index idx or the node that should be the parent of the node with index idx
Node* at(Node *p, int k){
if(p == EMPTY) return EMPTY;
p -> push_down();
if(k > p -> subSz) return EMPTY;
int sz = p -> ch[LEFT] -> subSz;
if(sz > k) return at(p -> ch[LEFT], k);
if(sz + 1 <= k) return at(p -> ch[RIGHT], k - sz - 1);
return p;
}
// find node with index idx or the node that should be the parent of the node with index idx
returnType at(int k){
auto p = splay_by_idx(root, k);
return p -> val;
}
// insert node with index idx to the tree
void insert(int idx, int val){
Node *before, *after;
split(root, idx, before, after);
Node* between = new Node(val);
root = merge(merge(before, between), after);
}
// erase node with index idx from the tree
void erase(int idx){
Node *before, *after, *between;
split(root, idx + 1, before, after);
split(before, idx, before, between);
delete between;
root = merge(before, after);
}
// replace node with index idx with node with value val
void replace(int idx, int val){
Node *before, *after, *between;
split(root, idx + 1, before, after);
split(before, idx, before, between);
// change the value of the node
between -> val = val;
root = merge(merge(before, between), after);
}
// get query result for the range [s, e]
returnType query(int s, int e){
Node *before, *after, *between;
split(root, e + 1, before, after);
split(before, s, before, between);
returnType ans = between -> val.max_seg;
root = merge(merge(before, between), after);
return ans;
}
// propagate lazy updates from node q to the root of the tree
void push_down_to_root(Node* p){
if(p == EMPTY) return;
push_down_to_root(p -> par);
p -> push_down();
}
// get the index of node q
int get_idx(Node* p){
push_down_to_root(p);
splay(p);
return p -> ch[LEFT] -> subSz;
}
// get the size of the subtree rooted at node p
int get_size(){
return root -> subSz;
}
// print the tree for debugging purposes
void print(Node* p, int depth){
if(p == EMPTY) return;
print(p -> ch[LEFT], depth + 1);
cout << string(2 * depth, ' ') << setw(2) << p -> val.val << "\n";
print(p -> ch[RIGHT], depth + 1);
}
// print the tree for debugging purposes
void print(){
print(root, 0);
cout << "-----------------------------------\n";
}
};
template < typename T > typename SplayTree < T > :: Node* SplayTree < T > :: EMPTY = new typename SplayTree < T > :: Node();
void Solve(){
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int test_cases = 1;
// cin >> test_cases;
for(int tc = 1; tc <= test_cases; tc++){
// cout << "Case #" << tc << ": ";
Solve();
}
return 0;
}