-
Notifications
You must be signed in to change notification settings - Fork 522
/
TreapIndexed.java
252 lines (224 loc) · 7.55 KB
/
TreapIndexed.java
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
package structures;
import java.util.*;
import java.util.function.Predicate;
// https://cp-algorithms.com/data_structures/treap.html
public class TreapIndexed {
static Random random = new Random();
public static class Node {
long nodeValue;
long mx;
long sum;
long add;
int size;
long prio;
Node left;
Node right;
Node(long value) {
nodeValue = value;
mx = value;
sum = value;
add = 0;
size = 1;
prio = random.nextLong();
}
void apply(long v) {
nodeValue += v;
mx += v;
sum += v * size;
add += v;
}
void push() {
if (add != 0) {
if (left != null)
left.apply(add);
if (right != null)
right.apply(add);
add = 0;
}
}
void pull() {
mx = Math.max(nodeValue, Math.max(getMx(left), getMx(right)));
sum = nodeValue + getSum(left) + getSum(right);
size = 1 + getSize(left) + getSize(right);
}
static long getMx(Node root) {
return root == null ? Long.MIN_VALUE : root.mx;
}
static long getSum(Node root) {
return root == null ? 0 : root.sum;
}
static int getSize(Node root) {
return root == null ? 0 : root.size;
}
}
public static class TreapPair {
Node left;
Node right;
TreapPair(Node left, Node right) {
this.left = left;
this.right = right;
}
}
public static TreapPair split(Node root, int minRight) {
if (root == null)
return new TreapPair(null, null);
root.push();
if (Node.getSize(root.left) >= minRight) {
TreapPair sub = split(root.left, minRight);
root.left = sub.right;
root.pull();
sub.right = root;
return sub;
} else {
TreapPair sub = split(root.right, minRight - Node.getSize(root.left) - 1);
root.right = sub.left;
root.pull();
sub.left = root;
return sub;
}
}
public static Node merge(Node left, Node right) {
if (left == null)
return right;
if (right == null)
return left;
left.push();
right.push();
if (left.prio > right.prio) {
left.right = merge(left.right, right);
left.pull();
return left;
} else {
right.left = merge(left, right.left);
right.pull();
return right;
}
}
public static Node insert(Node root, int index, long value) {
TreapPair t = split(root, index);
return merge(merge(t.left, new Node(value)), t.right);
}
public static Node remove(Node root, int index) {
TreapPair t = split(root, index);
return merge(t.left, split(t.right, index + 1 - Node.getSize(t.left)).right);
}
public static Node modify(Node root, int ll, int rr, long delta) {
TreapPair t1 = split(root, rr + 1);
TreapPair t2 = split(t1.left, ll);
if (t2.right != null)
t2.right.apply(delta);
return merge(merge(t2.left, t2.right), t1.right);
}
public static class TreapAndResult {
Node treap;
long mx;
long sum;
TreapAndResult(Node t, long mx, long sum) {
this.treap = t;
this.mx = mx;
this.sum = sum;
}
}
public static TreapAndResult query(Node root, int ll, int rr) {
TreapPair t1 = split(root, rr + 1);
TreapPair t2 = split(t1.left, ll);
long mx = Node.getMx(t2.right);
long sum = Node.getSum(t2.right);
return new TreapAndResult(merge(merge(t2.left, t2.right), t1.right), mx, sum);
}
// calls all FALSE elements to the left of the sought position exactly once
public static int findFirst(Node root, int ll, int rr, Predicate<Node> f) {
return findFirst(root, ll, rr, f, 0, Node.getSize(root) - 1);
}
static int findFirst(Node root, int ll, int rr, Predicate<Node> f, int l, int r) {
if (ll <= l && r <= rr && !f.test(root)) {
return -1;
}
if (l == r) {
return l;
}
root.push();
int m = Node.getSize(root.left);
int res = -1;
if (ll < m) {
res = findFirst(root.left, ll, rr, f, l, l + m - 1);
}
if (res == -1) {
Node single = new Node(0);
single.size = 1;
single.apply(root.nodeValue);
res = findFirst(single, ll, rr, f, l + m, l + m);
}
if (rr > m && res == -1) {
res = findFirst(root.right, ll, rr, f, l + m + 1, r);
}
root.pull();
return res;
}
public static void print(Node root) {
if (root == null)
return;
root.push();
print(root.left);
System.out.print(root.nodeValue + " ");
print(root.right);
}
// Returns min(p | p<=rr && sum[ll..p]>=sum). If no such p exists, returns -1
static int sumLowerBound(Node treap, int ll, int rr, long sum) {
long[] sumSoFar = new long[1];
return findFirst(treap, ll, rr, node -> {
if (sumSoFar[0] + node.sum >= sum)
return true;
sumSoFar[0] += node.sum;
return false;
});
}
// Random test
public static void main(String[] args) {
Node treap = null;
List<Integer> list = new ArrayList<>();
Random rnd = new Random(1);
for (int step = 0; step < 100000; step++) {
int cmd = rnd.nextInt(6);
if (cmd < 2 && list.size() < 100) {
int pos = rnd.nextInt(list.size() + 1);
int value = rnd.nextInt(100);
list.add(pos, value);
treap = insert(treap, pos, value);
} else if (cmd < 3 && list.size() > 0) {
int pos = rnd.nextInt(list.size());
list.remove(pos);
treap = remove(treap, pos);
} else if (cmd < 4 && list.size() > 0) {
int b = rnd.nextInt(list.size());
int a = rnd.nextInt(b + 1);
int res = list.get(a);
for (int i = a + 1; i <= b; i++) res = Math.max(res, list.get(i));
TreapAndResult tr = query(treap, a, b);
treap = tr.treap;
if (res != tr.mx)
throw new RuntimeException();
} else if (cmd < 5 && list.size() > 0) {
int b = rnd.nextInt(list.size());
int a = rnd.nextInt(b + 1);
int delta = rnd.nextInt(100) - 50;
for (int i = a; i <= b; i++) list.set(i, list.get(i) + delta);
treap = modify(treap, a, b, delta);
} else {
for (int i = 0; i < list.size(); i++) {
TreapAndResult tr = query(treap, i, i);
treap = tr.treap;
long v = tr.mx;
if (list.get(i) != v)
throw new RuntimeException();
}
}
}
System.out.println("Test passed");
treap = null;
for (long v : new long[] {2, 1, 10, 20}) {
treap = insert(treap, Node.getSize(treap), v);
}
System.out.println(2 == sumLowerBound(treap, 0, treap.size - 1, 12));
}
}