-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreap.java
More file actions
517 lines (442 loc) · 10.8 KB
/
Copy pathTreap.java
File metadata and controls
517 lines (442 loc) · 10.8 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
class TreapSet<K> implements NavigableSet<K> {
protected static final Random rnd = new Random();
protected final Node nullNode;
protected final Comparator<? super K> comparator;
protected Node root;
private final K from;
private final K to;
private final boolean fromInclusive;
private final boolean toInclusive;
public TreapSet() {
this((Comparator<? super K>)null);
}
public TreapSet(Comparator<? super K> comparator) {
this(null, null, false, false, comparator, null, null);
}
protected TreapSet(K from, K to, boolean fromInclusive, boolean toInclusive, Comparator<? super K> comparator, Node root, Node nullNode) {
this.comparator = comparator;
this.from = from;
this.to = to;
this.fromInclusive = fromInclusive;
this.toInclusive = toInclusive;
if (nullNode == null)
nullNode = new NullNode();
if (root == null)
root = nullNode;
this.root = root;
this.nullNode = nullNode;
}
public boolean addAll(Iterable<? extends K> collection) {
boolean result = false;
for (K element : collection)
result |= add(element);
return result;
}
public K lower(K k) {
Node target = root.lower(k);
if (target == nullNode)
return null;
if (belongs(target.key))
return target.key;
return null;
}
private boolean belongs(K key) {
int valueFrom = compare(from, key);
int valueTo = compare(key, to);
return (valueFrom < 0 || valueFrom == 0 && fromInclusive) && (valueTo < 0 || valueTo == 0 && toInclusive);
}
public K floor(K k) {
Node target = root.floor(k);
if (target == nullNode)
return null;
if (belongs(target.key))
return target.key;
return null;
}
public K ceiling(K k) {
Node target = root.ceil(k);
if (target == nullNode)
return null;
if (belongs(target.key))
return target.key;
return null;
}
public K higher(K k) {
Node target = root.higher(k);
if (target == nullNode)
return null;
if (belongs(target.key))
return target.key;
return null;
}
public K pollFirst() {
K first = first();
if (first == null)
throw new NoSuchElementException();
root.erase(first);
return first;
}
public K pollLast() {
K last = last();
if (last == null)
throw new NoSuchElementException();
root.erase(last);
return last;
}
public int size() {
if (from == null && to == null)
return root.size;
if (from == null) {
Node to = toInclusive ? root.floor(this.to) : root.lower(this.to);
if (to == nullNode)
return 0;
return root.indexOf(to) + 1;
}
if (to == null) {
Node from = fromInclusive ? root.ceil(this.from) : root.higher(this.from);
if (from == nullNode)
return 0;
return root.size - root.indexOf(from);
}
Node from = fromInclusive ? root.ceil(this.from) : root.higher(this.from);
if (from == nullNode || !belongs(from.key))
return 0;
Node to = toInclusive ? root.floor(this.to) : root.lower(this.to);
return root.indexOf(to) - root.indexOf(from) + 1;
}
public boolean isEmpty() {
return size() == 0;
}
public boolean contains(Object o) {
return belongs((K) o) && root.search((K) o) != nullNode;
}
public Iterator<K> iterator() {
return new Iterator<K>() {
private K current = first();
public boolean hasNext() {
return current != null;
}
public K next() {
K result = current;
current = higher(current);
return result;
}
public void remove() {
TreapSet.this.remove(current);
}
};
}
public Object[] toArray() {
Object[] array = new Object[size()];
int index = 0;
for (K key : this)
array[index++] = key;
return array;
}
public <T> T[] toArray(T[] a) {
if (a.length < size())
throw new IllegalArgumentException();
int index = 0;
for (K key : this)
a[index++] = (T) key;
return a;
}
public boolean add(K k) {
if (k == null)
throw new NullPointerException();
if (contains(k))
return false;
root = root.insert(createNode(k));
return true;
}
protected Node createNode(K k) {
return new Node(k, rnd.nextLong());
}
public boolean remove(Object o) {
if (!contains(o))
return false;
root = root.erase((K) o);
return true;
}
public boolean containsAll(Collection<?> c) {
for (Object o : c) {
if (!contains(o))
return false;
}
return true;
}
public boolean addAll(Collection<? extends K> c) {
return addAll((Iterable<? extends K>)c);
}
public boolean retainAll(Collection<?> c) {
List<K> toRemove = new ArrayList<K>();
for (K key : this) {
if (!c.contains(key))
toRemove.add(key);
}
return removeAll(toRemove);
}
public boolean removeAll(Collection<?> c) {
boolean result = false;
for (Object o : c)
result |= remove(o);
return result;
}
public void clear() {
retainAll(Collections.<Object>emptySet());
}
public NavigableSet<K> descendingSet() {
throw new UnsupportedOperationException();
}
public Iterator<K> descendingIterator() {
return new Iterator<K>() {
private K current = last();
public boolean hasNext() {
return current != null;
}
public K next() {
K result = current;
current = lower(current);
return result;
}
public void remove() {
TreapSet.this.remove(current);
}
};
}
public NavigableSet<K> subSet(K fromElement, boolean fromInclusive, K toElement, boolean toInclusive) {
return new TreapSet<K>(fromElement, toElement, fromInclusive, toInclusive, comparator, root, nullNode);
}
public NavigableSet<K> headSet(K toElement, boolean inclusive) {
return subSet(null, false, toElement, inclusive);
}
public NavigableSet<K> tailSet(K fromElement, boolean inclusive) {
return subSet(fromElement, inclusive, null, false);
}
public Comparator<? super K> comparator() {
return comparator;
}
public SortedSet<K> subSet(K fromElement, K toElement) {
return subSet(fromElement, true, toElement, false);
}
public SortedSet<K> headSet(K toElement) {
return subSet(null, false, toElement, false);
}
public SortedSet<K> tailSet(K fromElement) {
return tailSet(fromElement, true);
}
public K first() {
if (isEmpty())
return null;
if (from == null)
return root.first().key;
if (fromInclusive)
return ceiling(from);
return higher(from);
}
public K last() {
if (isEmpty())
return null;
if (to == null)
return root.last().key;
if (toInclusive)
return floor(to);
return lower(to);
}
public K get(int index) {
if (index >= size() || index < 0)
throw new IndexOutOfBoundsException(Integer.toString(index));
if (from != null)
index += headSet(from, !fromInclusive).size();
return root.get(index);
}
protected int compare(K first, K second) {
if (first == null || second == null)
return -1;
if (comparator != null)
return comparator.compare(first, second);
return ((Comparable<? super K>)first).compareTo(second);
}
protected class Node {
protected final K key;
protected final long priority;
protected Node left;
protected Node right;
protected int size;
protected Node(K key, long priority) {
this.key = key;
this.priority = priority;
left = nullNode;
right = nullNode;
size = 1;
}
protected Object[] split(K key) {
if (compare(key, this.key) < 0) {
Object[] result = left.split(key);
left = (Node) result[1];
result[1] = this;
updateSize();
return result;
}
Object[] result = right.split(key);
right = (Node) result[0];
result[0] = this;
updateSize();
return result;
}
protected void updateSize() {
size = left.size + right.size + 1;
}
protected Node insert(Node node) {
if (node.priority > priority) {
Object[] result = split(node.key);
node.left = (Node) result[0];
node.right = (Node) result[1];
node.updateSize();
return node;
}
if (compare(node.key, this.key) < 0) {
left = left.insert(node);
updateSize();
return this;
}
right = right.insert(node);
updateSize();
return this;
}
protected Node merge(Node left, Node right) {
if (left == nullNode)
return right;
if (right == nullNode)
return left;
if (left.priority > right.priority) {
left.right = left.right.merge(left.right, right);
left.updateSize();
return left;
}
right.left = right.left.merge(left, right.left);
right.updateSize();
return right;
}
protected Node erase(K key) {
int value = compare(key, this.key);
if (value == 0)
return merge(left, right);
if (value < 0) {
left = left.erase(key);
updateSize();
return this;
}
right = right.erase(key);
updateSize();
return this;
}
protected Node lower(K key) {
if (compare(key, this.key) <= 0)
return left.lower(key);
Node result = right.lower(key);
if (result == nullNode)
return this;
return result;
}
protected Node floor(K key) {
if (compare(key, this.key) < 0)
return left.floor(key);
Node result = right.floor(key);
if (result == nullNode)
return this;
return result;
}
protected Node higher(K key) {
if (compare(key, this.key) >= 0)
return right.higher(key);
Node result = left.higher(key);
if (result == nullNode)
return this;
return result;
}
protected Node ceil(K key) {
if (compare(key, this.key) > 0)
return right.ceil(key);
Node result = left.ceil(key);
if (result == nullNode)
return this;
return result;
}
protected Node first() {
if (left == nullNode)
return this;
return left.first();
}
protected Node last() {
if (right == nullNode)
return this;
return right.last();
}
protected Node search(K key) {
int value = compare(key, this.key);
if (value == 0)
return this;
if (value < 0)
return left.search(key);
return right.search(key);
}
public int indexOf(Node node) {
if (this == node)
return left.size;
if (compare(node.key, this.key) > 0)
return left.size + 1 + right.indexOf(node);
return left.indexOf(node);
}
public K get(int index) {
if (index < left.size)
return left.get(index);
else if (index == left.size)
return key;
else
return right.get(index - left.size - 1);
}
}
private class NullNode extends Node {
private Object[] splitResult = new Object[2];
private NullNode() {
super(null, Long.MIN_VALUE);
left = this;
right = this;
size = 0;
}
protected Object[] split(K key) {
splitResult[0] = splitResult[1] = this;
return splitResult;
}
protected Node insert(Node node) {
return node;
}
protected Node erase(K key) {
return this;
}
protected Node lower(K key) {
return this;
}
protected Node floor(K key) {
return this;
}
protected Node higher(K key) {
return this;
}
protected Node ceil(K key) {
return this;
}
protected Node first() {
throw new NoSuchElementException();
}
protected Node last() {
throw new NoSuchElementException();
}
protected void updateSize() {
}
protected Node search(K key) {
return this;
}
}
}