-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathMoAlgorithm.java
More file actions
59 lines (54 loc) · 1.91 KB
/
Copy pathMoAlgorithm.java
File metadata and controls
59 lines (54 loc) · 1.91 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
public class MoAlgorithm {
public static class Query {
public int l, r, idx;
public Query(int l, int r, int idx) { this.l = l; this.r = r; this.idx = idx; }
}
public static int[] mosDistinct(int[] arr, Query[] queries) {
int n = arr.length;
int q = queries.length;
int block = (int)Math.max(1, Math.sqrt(n));
java.util.Arrays.sort(queries, (a, b) -> {
int ab = a.l / block;
int bb = b.l / block;
if (ab != bb) return Integer.compare(ab, bb);
// Alternate ordering for improved cache behavior
return ((ab & 1) == 0) ? Integer.compare(a.r, b.r) : Integer.compare(b.r, a.r);
});
java.util.Map<Integer, Integer> freq = new java.util.HashMap<>();
int curL = 0, curR = -1;
int distinct = 0;
int[] ans = new int[q];
for (Query qu : queries) {
while (curL > qu.l) {
curL--;
int v = arr[curL];
int f = freq.getOrDefault(v, 0) + 1;
freq.put(v, f);
if (f == 1) distinct++;
}
while (curR < qu.r) {
curR++;
int v = arr[curR];
int f = freq.getOrDefault(v, 0) + 1;
freq.put(v, f);
if (f == 1) distinct++;
}
while (curL < qu.l) {
int v = arr[curL];
int f = freq.get(v);
if (f == 1) freq.remove(v); else freq.put(v, f - 1);
if (f == 1) distinct--;
curL++;
}
while (curR > qu.r) {
int v = arr[curR];
int f = freq.get(v);
if (f == 1) freq.remove(v); else freq.put(v, f - 1);
if (f == 1) distinct--;
curR--;
}
ans[qu.idx] = distinct;
}
return ans;
}
}