forked from t3nsor/codebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treap.cpp
203 lines (179 loc) · 4.33 KB
/
treap.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
/* Implicit treap implementation
* Dynamic array that supports following operations:
* - Inserting an element in a given position
* - Deleting an element in a given position
* - Reversing elements in a given interval
* - Range queries and updates
* - Splitting array in a given position in two subarrays
* - Merging two arrays into a single array
* All operations are (randomized) O(log n)
* Warning: high costant factor, use other data structures,
* e.g. segment tree, if treap is not strictly required
*/
using ll = long long;
const int MAXN = 1e6; // max number of nodes to be allocated
const int SEED = 42;
struct Treap {
struct node {
ll val, q, lazy = 0LL;
int pr, sz = 1;
bool rev = false;
node *l = nullptr, *r = nullptr;
};
node *root;
mt19937 rng;
node stock[MAXN];
int si = 0;
node *newnode(ll v) {
stock[si].val = stock[si].q = v;
stock[si].pr = rng();
return &(stock[si++]);
}
Treap(int seed = SEED): root(nullptr), rng(seed) {}
Treap(vector<ll> &v, int seed = SEED): rng(seed) {
root = build(v, 0, v.size() - 1);
}
node *build(vector<ll> &v, int l, int r) {
if(l > r) return nullptr;
int m = (l + r) / 2;
node *n = newnode(v[m]);
n->l = build(v, l, m - 1);
n->r = build(v, m + 1, r);
fixPr(n);
updateNode(n);
return n;
}
void fixPr(node *n) {
if(n) {
node *t = n;
if(n->l && n->l->pr > t->pr) t = n->l;
if(n->r && n->r->pr > t->pr) t = n->r;
if(n != t) {
swap(n->pr, t->pr);
fixPr(t);
}
}
}
inline int getSize(node *n) {
if(n) return n->sz;
else return 0;
}
inline ll query(node *n) { // adjust as needed
if(n) return n->q + n->lazy * n->sz;
else return 0;
}
inline void addLazy(node *n, ll add) {
if(n) n->lazy += add; // adjust as needed
}
inline void toggleRev(node *n) {
if(n) n->rev = !n->rev;
}
void pushLazy(node *n) {
if(n) {
if(n->lazy) {
n->val += n->lazy;
addLazy(n->l, n->lazy);
addLazy(n->r, n->lazy);
n->lazy = 0LL;
}
if(n->rev) {
swap(n->l, n->r);
toggleRev(n->l);
toggleRev(n->r);
n->rev = false;
}
}
}
void updateNode(node *n) {
if(n) {
n->sz = 1 + getSize(n->l) + getSize(n->r);
n->q = n->val + query(n->l) + query(n->r); // adjust as needed
}
}
void merge(node *&d, node *sl, node *sr) {
if(!sl || !sr) {
if(sl) d = sl;
else d = sr;
} else if(sl->pr > sr->pr) {
pushLazy(sl);
merge(sl->r, sl->r, sr);
d = sl;
} else {
pushLazy(sr);
merge(sr->l, sl, sr->l);
d = sr;
}
updateNode(d);
}
// pos is the first index of the right-side tree
void split(node *s, node *&dl, node *&dr, int pos, int add = 0) {
pushLazy(s);
if(!s) {
dl = dr = nullptr;
return;
}
int curPos = add + getSize(s->l);
if(pos <= curPos) {
split(s->l, dl, s->l, pos, add);
dr = s;
updateNode(dr);
} else {
split(s->r, s->r, dr, pos, curPos + 1);
dl = s;
updateNode(dl);
}
}
node *insert(ll val, int pos) { // indeces are 0-based
node *n = newnode(val);
node *tl, *tr;
split(root, tl, tr, pos);
merge(root, tl, n);
merge(root, root, tr);
return n;
}
node *erase(int pos) {
node *tl, *tr, *n;
split(root, tl, tr, pos);
split(tr, n, tr, 1);
merge(root, tl, tr);
return n;
}
ll find(node *n, int pos, int add = 0) {
pushLazy(n);
int curPos = add + getSize(n->l);
ll ans;
if(pos == curPos) ans = n->val;
else if(pos < curPos) ans = find(n->l, pos, add);
else ans = find(n->r, pos, curPos + 1);
updateNode(n);
return ans;
}
ll find(int pos) {
return find(root, pos);
}
void add(int l, int r, ll add) {
node *tl, *tr, *t;
split(root, tl, tr, l);
split(tr, t, tr, r - l + 1);
addLazy(t, add);
merge(tl, tl, t);
merge(root, tl, tr);
}
void reverse(int l, int r) {
node *tl, *tr, *t;
split(root, tl, tr, l);
split(tr, t, tr, r - l + 1);
toggleRev(t);
merge(tl, tl, t);
merge(root, tl, tr);
}
ll query(int l, int r) {
node *tl, *tr, *t;
split(root, tl, tr, l);
split(tr, t, tr, r - l + 1);
ll ans = query(t);
merge(tl, tl, t);
merge(root, tl, tr);
return ans;
}
};