-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwavelet-tree.cpp
87 lines (77 loc) · 2.32 KB
/
wavelet-tree.cpp
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
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int>::iterator iter;
//Wavelet tree with succinct representation of bitmaps
struct WaveTreeSucc {
vector<vector<int> > C; int s;
// sigma = size of the alphabet, ie., one more than the maximum element
// in S.
WaveTreeSucc(vector<int> &A, int sigma) : C(sigma*2), s(sigma) {
build(A.begin(), A.end(), 0, s-1, 1);
}
void build(iter b, iter e, int L, int U, int u) {
if (L == U)
return;
int M = (L+U)/2;
// C[u][i] contains number of zeros until position i-1: [0,i)
C[u].reserve(e-b+1); C[u].push_back(0);
for (iter it = b; it != e; ++it)
C[u].push_back(C[u].back() + (*it<=M));
iter p = stable_partition(b, e, [=](int i){return i<=M;});
build(b, p, L, M, u*2);
build(p, e, M+1, U, u*2+1);
}
// Count occurrences of number c until position i.
// ie, occurrences of c in positions [0,i]
int rank(int c, int i) const {
// Internally we consider an interval open on the right: [0, i)
i++;
int L = 0, U = s-1, u = 1, M, r;
while (L != U) {
M = (L+U)/2;
r = C[u][i]; u*=2;
if (c <= M)
i = r, U = M;
else
i -= r, L = M+1, ++u;
}
return i;
}
// Find the k-th smallest element in positions [i,j].
// The smallest element is k=1
int quantile(int k, int i, int j) const {
// internally we we consider an interval open on the right: [i, j)
j++;
int L = 0, U = s-1, u = 1, M, ri, rj;
while (L != U) {
M = (L+U)/2;
ri = C[u][i]; rj = C[u][j]; u*=2;
if (k <= rj-ri)
i = ri, j = rj, U = M;
else
k -= rj-ri, i -= ri, j -= rj,
L = M+1, ++u;
}
return U;
}
// Count number of occurrences of numbers in the range [a, b]
// present in the sequence in positions [i, j], ie, if representing a grid it
// counts number of points in the specified rectangle.
mutable int L, U;
int range(int i, int j, int a, int b) const {
if (b < a or j < i)
return 0;
L = a; U = b;
return range(i, j+1, 0, s-1, 1);
}
int range(int i, int j, int a, int b, int u) const {
if (b < L or U < a)
return 0;
if (L <= a and b <= U)
return j-i;
int M = (a+b)/2, ri = C[u][i], rj = C[u][j];
return range(ri, rj, a, M, u*2) +
range(i-ri, j-rj, M+1, b, u*2+1);
}
};