-
Notifications
You must be signed in to change notification settings - Fork 522
/
Treap.java
188 lines (160 loc) · 4.96 KB
/
Treap.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
package structures;
import java.util.Random;
// https://cp-algorithms.com/data_structures/treap.html
public class Treap {
static Random random = new Random();
public static class Node {
long nodeValue;
long mx;
long sum;
long add;
long key; // keys should be unique
int size;
long prio;
Node left;
Node right;
Node(long key, long value) {
this.key = key;
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, long minRight) {
if (root == null)
return new TreapPair(null, null);
root.push();
if (root.key >= 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);
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, long key, long value) {
TreapPair t = split(root, key);
return merge(merge(t.left, new Node(key, value)), t.right);
}
public static Node remove(Node root, long key) {
TreapPair t = split(root, key);
return merge(t.left, split(t.right, key + 1).right);
}
public static Node modify(Node root, long ll, long 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, long ll, long 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);
}
static Node kth(Node root, int k) {
if (k < Node.getSize(root.left))
return kth(root.left, k);
else if (k > Node.getSize(root.left))
return kth(root.right, k - Node.getSize(root.left) - 1);
return root;
}
public static void print(Node root) {
if (root == null)
return;
root.push();
print(root.left);
System.out.print(root.nodeValue + " ");
print(root.right);
}
// Random test
public static void main(String[] args) {
Node treap = null;
treap = insert(treap, 5, 3);
treap = insert(treap, 3, 2);
treap = insert(treap, 6, 1);
System.out.println(kth(treap, 1).key);
TreapAndResult treapAndResult1 = query(treap, 1, 10);
treap = treapAndResult1.treap;
System.out.println(treapAndResult1.mx);
treap = remove(treap, 5);
TreapAndResult treapAndResult2 = query(treap, 1, 10);
treap = treapAndResult2.treap;
System.out.println(treapAndResult2.mx);
}
}