-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathTreap struct (with lazy prop).cpp
243 lines (214 loc) · 6.2 KB
/
Treap struct (with lazy prop).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
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
/* Treap (with lazy prop) */
/*
Blog:
https://codeforces.com/blog/entry/84017
SecondThread:
https://www.youtube.com/watch?v=6x0UlIBLRsc
CP algorithms:
https://cp-algorithms.com/data_structures/treap.html
Hello world:
https://codeforces.com/contest/863/problem/D
Problems:
https://codeforces.com/gym/102787
Examples:
https://codeforces.com/gym/102787/problem/B
http://p.ip.fi/rW9o
https://codeforces.com/gym/102787/problem/C
http://p.ip.fi/-MjA
*/
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
struct Treap {
T *root;
using key_t = T::key_t;
operator T*() const { return root; }
explicit Treap(T *t = nullptr): root(t) {}
Treap(const auto& a): root(build(a, 0, a.size())) {}
template<class... Args>
Treap(int N, Args&&... args): Treap(vector<T>(N, T(forward<Args>(args)...))) {}
void heapify(T *t) {
if(!t) return;
T *mx = t;
if(t->l and t->l->prio > mx->prio)
mx = t->l;
if(t->r and t->r->prio > mx->prio)
mx = t->r;
if(mx != t) {
swap(t->prio, mx->prio);
heapify(mx);
}
}
// [l, r)
T *build(const auto& a, int l, int r) {
if(r <= l) return nullptr;
int mid = l + r >> 1;
T *t = new T(a[mid]);
t->l = build(a, l, mid);
t->r = build(a, mid + 1, r);
t->pull();
return heapify(t), t;
}
// l has subtree size sz
static void split_by_sz(T *t, int sz, T* &l, T* &r) {
if(!t) return l = r = nullptr, void();
t->prop();
if(sz <= (t->l? t->l->sz : 0))
split_by_sz(t->l, sz, l, t->l), r = t;
else
split_by_sz(t->r, sz - (t->l? t->l->sz : 0) - 1, t->r, r), l = t;
t->pull();
}
// r starts with key
static void split_by_key(T *t, key_t key, T* &l, T* &r) {
if(!t) return l = r = nullptr, void();
t->prop();
if(key <= t->key)
split_by_key(t->l, key, l, t->l), r = t;
else
split_by_key(t->r, key, t->r, r), l = t;
t->pull();
}
static void meld(T* &t, T *l, T *r) {
if(l) l->prop();
if(r) r->prop();
if(!l or !r) t = l? l : r;
else if(l->prio > r->prio)
meld(l->r, l->r, r), t = l;
else
meld(r->l, l, r->l), t = r;
if(t) t->pull();
}
// pos(i.e. sz) -> 0-based
static void insert_at_pos(T* &t, int sz, T *nd) {
if(!t) t = nd;
else if(nd->prio > t->prio)
split_by_sz(t, sz, nd->l, nd->r), t = nd;
else {
t->prop();
int lsz = t->l? t->l->sz : 0;
if(sz <= lsz)
insert_at_pos(t->l, sz, nd);
else
insert_at_pos(t->r, sz - lsz - 1, nd);
}
t->pull();
}
// returns pos inserted (0-based)
static int insert_key(T* &t, T *nd) {
int res = 0;
if(!t) t = nd;
else if(nd->prio > t->prio) {
split_by_key(t, nd->key, nd->l, nd->r);
t = nd; res += (t->l? t->l->sz : 0);
} else {
t->prop();
if(nd->key > t->key)
res += 1 + (t->l? t->l->sz : 0);
insert_key(nd->key < t->key? t->l : t->r, nd);
}
t->pull();
return res;
}
// pos(i.e. sz) -> 0-based
static void erase_at_pos(T* &t, int sz) {
if(!t) return;
t->prop();
int lsz = t->l? t->l->sz : 0;
if(sz == lsz)
meld(t, t->l, t->r);
else if(sz < lsz)
erase_at_pos(t->l, sz);
else
erase_at_pos(t->r, sz - lsz - 1);
if(t) t->pull();
}
// returns pos erased (0-based)
static int erase_key(T* &t, key_t key) {
if(!t) return 0;
int res = 0;
t->prop();
if(key == t->key)
res += (t->l? t->l->sz : 0), meld(t, t->l, t->r);
else {
if(key > t->key)
res += 1 + (t->l? t->l->sz : 0);
erase_key(key < t->key ? t->l : t->r, key);
}
if(t) t->pull();
return res;
}
// 0-based [lpos, rpos)
void erase_interval(int lpos, int rpos) {
T *x{nullptr}, *t{root}, *y{nullptr};
if(rpos < root->sz)
split_by_sz(root, rpos, t, y);
if(lpos > 0)
split_by_sz(t, lpos, x, t);
meld(root, x, y);
}
// pos -> 0-based
void insert_at_pos(int pos, T *nd) { insert_at_pos(root, pos, nd); }
void erase_at_pos(int pos) { erase_at_pos(root, pos); }
void insert_key(key_t key) { insert_key(root, new T(key)); }
void erase_key(key_t key) { erase_key(root, key); }
// 0-based [lpos, rpos)
T query(int lpos, int rpos) {
T *x{nullptr}, *t{root}, *y{nullptr};
if(rpos < root->sz)
split_by_sz(root, rpos, t, y);
if(lpos > 0)
split_by_sz(t, lpos, x, t);
T res = *t;
meld(t, x, t);
meld(root, t, y);
return res;
}
// 0-based [lpos, rpos)
template<class... Args>
void update(int lpos, int rpos, Args&&... args) {
T *x{nullptr}, *t{root}, *y{nullptr};
if(rpos < root->sz)
split_by_sz(root, rpos, t, y);
if(lpos > 0)
split_by_sz(t, lpos, x, t);
t->update(forward<Args>(args)...);
meld(t, x, t);
meld(root, t, y);
}
void inorder(T *t, vector<T*>& res) {
if(!t) return;
t->prop();
inorder(t->l, res);
res.push_back(t);
inorder(t->r, res);
t->pull();
}
vector<T*> to_vector() {
if(!root) return {};
vector<T*> res;
res.reserve(root->sz);
inorder(root, res);
return res;
}
};
struct treap_node {
int sz{1};
mt19937::result_type prio{rng()};
treap_node *l{nullptr}, *r{nullptr};
using key_t = int;
key_t key;
treap_node(key_t key): key(key) {}
void prop() {}
void pull() {
sz = 1;
if(l) {
l->prop();
sz += l->sz;
}
if(r) {
r->prop();
sz += r->sz;
}
}
void update() {}
};