-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTreapImplicitKey2.java
232 lines (204 loc) · 6.11 KB
/
TreapImplicitKey2.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
package Data_Structures;
import java.util.*;
// https://en.wikipedia.org/wiki/Treap
public class TreapImplicitKey2 {
// Modify the following 5 methods to implement your custom operations on the tree.
// This example implements Add/Max operations. Operations like Add/Sum, Set/Max can also be implemented.
static int modifyOperation(int x, int y) {
return x + y;
}
// query (or combine) operation
static int queryOperation(int leftValue, int rightValue) {
return Math.max(leftValue, rightValue);
}
static int deltaEffectOnSegment(int delta, int segmentLength) {
if (delta == getNeutralDelta()) return getNeutralDelta();
// Here you must write a fast equivalent of following slow code:
// int result = delta;
// for (int i = 1; i < segmentLength; i++) result = queryOperation(result, delta);
// return result;
return delta;
}
static int getNeutralDelta() {
return 0;
}
static int getNeutralValue() {
return Integer.MIN_VALUE;
}
// generic code
static Random random = new Random();
static int joinValueWithDelta(int value, int delta) {
if (delta == getNeutralDelta()) return value;
return modifyOperation(value, delta);
}
static int joinDeltas(int delta1, int delta2) {
if (delta1 == getNeutralDelta()) return delta2;
if (delta2 == getNeutralDelta()) return delta1;
return modifyOperation(delta1, delta2);
}
static void applyDelta(Treap root, int delta) {
if (root == null)
return;
root.delta = joinDeltas(root.delta, delta);
root.nodeValue = joinValueWithDelta(root.nodeValue, delta);
root.subTreeValue = joinValueWithDelta(root.subTreeValue, deltaEffectOnSegment(delta, root.size));
}
static void pushDelta(Treap root) {
if (root == null)
return;
applyDelta(root.left, root.delta);
applyDelta(root.right, root.delta);
root.delta = getNeutralDelta();
}
public static class Treap {
int nodeValue;
int subTreeValue;
int delta; // delta affects left.nodeValue, right.nodeValue, left.subTreeValue, right.subTreeValue, left.delta and right.delta
int size;
long prio;
Treap left;
Treap right;
Treap(int value) {
nodeValue = value;
subTreeValue = value;
delta = getNeutralDelta();
size = 1;
prio = random.nextLong();
}
void update() {
subTreeValue = queryOperation(queryOperation(getSubTreeValue(left), nodeValue), getSubTreeValue(right));
size = 1 + getSize(left) + getSize(right);
}
}
static int getSize(Treap root) {
return root == null ? 0 : root.size;
}
static int getSubTreeValue(Treap root) {
return root == null ? getNeutralValue() : root.subTreeValue;
}
public static class TreapPair {
Treap left;
Treap right;
TreapPair(Treap left, Treap right) {
this.left = left;
this.right = right;
}
}
public static TreapPair split(Treap root, int minRight) {
if (root == null)
return new TreapPair(null, null);
pushDelta(root);
if (getSize(root.left) >= minRight) {
TreapPair sub = split(root.left, minRight);
root.left = sub.right;
root.update();
sub.right = root;
return sub;
} else {
TreapPair sub = split(root.right, minRight - getSize(root.left) - 1);
root.right = sub.left;
root.update();
sub.left = root;
return sub;
}
}
public static Treap merge(Treap left, Treap right) {
pushDelta(left);
pushDelta(right);
if (left == null)
return right;
if (right == null)
return left;
// if (random.nextInt(left.size + right.size) < left.size) {
if (left.prio > right.prio) {
left.right = merge(left.right, right);
left.update();
return left;
} else {
right.left = merge(left, right.left);
right.update();
return right;
}
}
public static Treap insert(Treap root, int index, int value) {
TreapPair t = split(root, index);
return merge(merge(t.left, new Treap(value)), t.right);
}
public static Treap remove(Treap root, int index) {
TreapPair t = split(root, index);
return merge(t.left, split(t.right, index + 1 - getSize(t.left)).right);
}
public static Treap modify(Treap root, int a, int b, int delta) {
TreapPair t1 = split(root, b + 1);
TreapPair t2 = split(t1.left, a);
applyDelta(t2.right, delta);
return merge(merge(t2.left, t2.right), t1.right);
}
public static class TreapAndResult {
Treap treap;
int value;
TreapAndResult(Treap t, int value) {
this.treap = t;
this.value = value;
}
}
public static TreapAndResult query(Treap root, int a, int b) {
TreapPair t1 = split(root, b + 1);
TreapPair t2 = split(t1.left, a);
int value = getSubTreeValue(t2.right);
return new TreapAndResult(merge(merge(t2.left, t2.right), t1.right), value);
}
public static void print(Treap root) {
if (root == null)
return;
pushDelta(root);
print(root.left);
System.out.print(root.nodeValue + " ");
print(root.right);
}
// Random test
public static void main(String[] args) {
Treap 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 = queryOperation(res, list.get(i));
TreapAndResult tr = query(treap, a, b);
treap = tr.treap;
if (res != tr.value)
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, joinValueWithDelta(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;
int v = tr.value;
if (list.get(i) != v)
throw new RuntimeException();
}
}
}
System.out.println("Test passed");
}
}