-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
mo.cpp
67 lines (63 loc) · 1.38 KB
/
mo.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
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
const int N = 1e6 + 5;
int a[N], bucket[N], cnt[N];
int bucket_size;
struct query {
int l, r, i;
} q[N];
int ans = 0;
void add(int index) {
cnt[a[index]]++;
if (cnt[a[index]] == 1)
ans++;
}
void remove(int index) {
cnt[a[index]]--;
if (cnt[a[index]] == 0)
ans--;
}
bool mycmp(query x, query y) {
if (x.l / bucket_size != y.l / bucket_size)
return x.l / bucket_size < y.l / bucket_size;
return x.r < y.r;
}
int main() {
int n, t, i, j, k = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
bucket_size = ceil(sqrt(n));
scanf("%d", &t);
for (i = 0; i < t; i++) {
scanf("%d %d", &q[i].l, &q[i].r);
q[i].l--;
q[i].r--;
q[i].i = i;
}
sort(q, q + t, mycmp);
int left = 0, right = 0;
for (i = 0; i < t; i++) {
int L = q[i].l, R = q[i].r;
while (left < L) {
remove(left);
left++;
}
while (left > L) {
add(left - 1);
left--;
}
while (right <= R) {
add(right);
right++;
}
while (right > R + 1) {
remove(right - 1);
right--;
}
bucket[q[i].i] = ans;
}
for (i = 0; i < t; i++) printf("%d\n", bucket[i]);
return 0;
}