-
Notifications
You must be signed in to change notification settings - Fork 34
/
randomized-binary-search-tree-lazy.hpp
194 lines (164 loc) · 4.66 KB
/
randomized-binary-search-tree-lazy.hpp
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
template <class Monoid, class OperatorMonoid = Monoid>
struct RandomizedBinarySearchTree {
using F = function<Monoid(Monoid, Monoid)>;
using G = function<Monoid(Monoid, OperatorMonoid)>;
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
using P = function<OperatorMonoid(OperatorMonoid, int)>;
inline int xor128() {
static int x = 123456789;
static int y = 362436069;
static int z = 521288629;
static int w = 88675123;
int t;
t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
struct Node {
Node *l, *r;
int cnt;
Monoid key, sum;
OperatorMonoid lazy;
Node() = default;
Node(const Monoid &k, const OperatorMonoid &p)
: cnt(1), key(k), sum(k), lazy(p), l(nullptr), r(nullptr) {}
};
vector<Node> pool;
int ptr;
const Monoid M1;
const OperatorMonoid OM0;
const F f;
const G g;
const H h;
const P p;
RandomizedBinarySearchTree(int sz, const F &f, const Monoid &M1)
: pool(sz),
ptr(0),
f(f),
g(G()),
h(H()),
p(P()),
M1(M1),
OM0(OperatorMonoid()) {}
RandomizedBinarySearchTree(int sz, const F &f, const G &g, const H &h,
const P &p, const Monoid &M1,
const OperatorMonoid &OM0)
: pool(sz), ptr(0), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {}
inline Node *alloc(const Monoid &key) {
return &(pool[ptr++] = Node(key, OM0));
}
virtual Node *clone(Node *t) { return t; }
inline int count(const Node *t) { return t ? t->cnt : 0; }
inline Monoid sum(const Node *t) { return t ? t->sum : M1; }
inline Node *update(Node *t) {
t->cnt = count(t->l) + count(t->r) + 1;
t->sum = f(f(sum(t->l), t->key), sum(t->r));
return t;
}
Node *propagate(Node *t) {
t = clone(t);
if (t->lazy != OM0) {
t->key = g(t->key, p(t->lazy, 1));
if (t->l) {
t->l = clone(t->l);
t->l->lazy = h(t->l->lazy, t->lazy);
t->l->sum = g(t->l->sum, p(t->lazy, count(t->l)));
}
if (t->r) {
t->r = clone(t->r);
t->r->lazy = h(t->r->lazy, t->lazy);
t->r->sum = g(t->r->sum, p(t->lazy, count(t->r)));
}
t->lazy = OM0;
}
return update(t);
}
Node *merge(Node *l, Node *r) {
if (!l || !r) return l ? l : r;
if (xor128() % (l->cnt + r->cnt) < l->cnt) {
l = propagate(l);
l->r = merge(l->r, r);
return update(l);
} else {
r = propagate(r);
r->l = merge(l, r->l);
return update(r);
}
}
pair<Node *, Node *> split(Node *t, int k) {
if (!t) return {t, t};
t = propagate(t);
if (k <= count(t->l)) {
auto s = split(t->l, k);
t->l = s.second;
return {s.first, update(t)};
} else {
auto s = split(t->r, k - count(t->l) - 1);
t->r = s.first;
return {update(t), s.second};
}
}
Node *build(int l, int r, const vector<Monoid> &v) {
if (l + 1 >= r) return alloc(v[l]);
return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));
}
Node *build(const vector<Monoid> &v) {
ptr = 0;
return build(0, (int)v.size(), v);
}
void dump(Node *r, typename vector<Monoid>::iterator &it) {
if (!r) return;
r = propagate(r);
dump(r->l, it);
*it = r->key;
dump(r->r, ++it);
}
vector<Monoid> dump(Node *r) {
vector<Monoid> v((size_t)count(r));
auto it = begin(v);
dump(r, it);
return v;
}
string to_string(Node *r) {
auto s = dump(r);
string ret;
for (int i = 0; i < s.size(); i++) ret += ", ";
return (ret);
}
void insert(Node *&t, int k, const Monoid &v) {
auto x = split(t, k);
t = merge(merge(x.first, alloc(v)), x.second);
}
void erase(Node *&t, int k) {
auto x = split(t, k);
t = merge(x.first, split(x.second, 1).second);
}
Monoid query(Node *&t, int a, int b) {
auto x = split(t, a);
auto y = split(x.second, b - a);
auto ret = sum(y.first);
t = merge(x.first, merge(y.first, y.second));
return ret;
}
void set_propagate(Node *&t, int a, int b, const OperatorMonoid &p) {
auto x = split(t, a);
auto y = split(x.second, b - a);
y.first->lazy = h(y.first->lazy, p);
t = merge(x.first, merge(propagate(y.first), y.second));
}
void set_element(Node *&t, int k, const Monoid &x) {
t = propagate(t);
if (k < count(t->l))
set_element(t->l, k, x);
else if (k == count(t->l))
t->key = t->sum = x;
else
set_element(t->r, k - count(t->l) - 1, x);
t = update(t);
}
int size(Node *t) { return count(t); }
bool empty(Node *t) { return !t; }
Node *makeset() { return nullptr; }
};