forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
62 lines (55 loc) · 1.22 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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 v) {
while (x <= n) {
c[x] += v;
x += x & -x;
}
}
public int query(int x) {
int s = 0;
while (x > 0) {
s += c[x];
x -= x & -x;
}
return s;
}
}
class MRUQueue {
private int n;
private int[] q;
private BinaryIndexedTree tree;
public MRUQueue(int n) {
this.n = n;
q = new int[n + 2010];
for (int i = 1; i <= n; ++i) {
q[i] = i;
}
tree = new BinaryIndexedTree(n + 2010);
}
public int fetch(int k) {
int l = 1, r = n;
while (l < r) {
int mid = (l + r) >> 1;
if (mid - tree.query(mid) >= k) {
r = mid;
} else {
l = mid + 1;
}
}
int x = q[l];
q[++n] = x;
tree.update(l, 1);
return x;
}
}
/**
* Your MRUQueue object will be instantiated and called as such:
* MRUQueue obj = new MRUQueue(n);
* int param_1 = obj.fetch(k);
*/