-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
45 lines (37 loc) · 878 Bytes
/
Solution.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
class BinaryIndexedTree {
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
this.c = new int[n + 1];
}
public void update(int x, int delta) {
for (; x <= n; x += x & -x) {
c[x] += delta;
}
}
public int query(int x) {
int s = 0;
for (; x > 0; x -= x & -x) {
s += c[x];
}
return s;
}
}
class StreamRank {
private BinaryIndexedTree tree = new BinaryIndexedTree(50010);
public StreamRank() {
}
public void track(int x) {
tree.update(x + 1, 1);
}
public int getRankOfNumber(int x) {
return tree.query(x + 1);
}
}
/**
* Your StreamRank object will be instantiated and called as such:
* StreamRank obj = new StreamRank();
* obj.track(x);
* int param_2 = obj.getRankOfNumber(x);
*/