forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaveletTree.java
96 lines (87 loc) · 3.18 KB
/
WaveletTree.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
package structures;
import java.util.Arrays;
import java.util.function.Predicate;
public class WaveletTree {
static class Node {
int lo;
int hi;
Node left;
Node right;
int[] b;
// kth smallest element in [from, to]
public int kth(int from, int to, int k) {
if (from > to)
return 0;
if (lo == hi)
return lo;
int inLeft = b[to] - b[from - 1];
int lb = b[from - 1]; // amt of nos in first (from-1) nos that go in left
int rb = b[to]; // amt of nos in first (to) nos that go in left
return k <= inLeft ? left.kth(lb + 1, rb, k) : right.kth(from - lb, to - rb, k - inLeft);
}
// number of elements in [from, to] less than or equal to k
public int countLessOrEq(int from, int to, int k) {
if (from > to || k < lo)
return 0;
if (hi <= k)
return to - from + 1;
int lb = b[from - 1], rb = b[to];
return left.countLessOrEq(lb + 1, rb, k) + right.countLessOrEq(from - lb, to - rb, k);
}
// number of elements in [from, to] equal to k
public int countEq(int from, int to, int k) {
if (from > to || k < lo || k > hi)
return 0;
if (lo == hi)
return to - from + 1;
int lb = b[from - 1];
int rb = b[to];
int mid = (lo + hi) >>> 1;
return k <= mid ? left.countEq(lb + 1, rb, k) : right.countEq(from - lb, to - rb, k);
}
}
public static Node createTree(int[] a) {
int lo = Arrays.stream(a).min().orElse(Integer.MAX_VALUE);
int hi = Arrays.stream(a).max().orElse(Integer.MIN_VALUE);
return build(a, 0, a.length, lo, hi);
}
static Node build(int[] a, int from, int to, int lo, int hi) {
Node node = new Node();
node.lo = lo;
node.hi = hi;
if (lo < hi && from < to) {
int mid = (lo + hi) >>> 1;
Predicate<Integer> p = x -> x <= mid;
node.b = new int[to - from + 1];
for (int i = 0; i + 1 < node.b.length; i++) {
node.b[i + 1] = node.b[i] + (p.test(a[i + from]) ? 1 : 0);
}
int pivot = stablePartition(a, from, to, p);
node.left = build(a, from, pivot, lo, mid);
node.right = build(a, pivot, to, mid + 1, hi);
}
return node;
}
static int stablePartition(int[] a, int from, int to, Predicate<Integer> p) {
int[] b1 = new int[to - from + 1];
int[] b2 = new int[to - from + 1];
int cnt1 = 0;
int cnt2 = 0;
for (int i = from; i < to; i++) {
if (p.test(a[i])) {
b1[cnt1++] = a[i];
} else {
b2[cnt2++] = a[i];
}
}
System.arraycopy(b1, 0, a, from, cnt1);
System.arraycopy(b2, 0, a, from + cnt1, cnt2);
return cnt1;
}
// Usage example
public static void main(String[] args) {
int[] a = {5, 1, 2, 1, 1};
Node t = WaveletTree.createTree(a);
System.out.println(t.countEq(1, 5, 1));
}
}