-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrange_distinct_query_faster.cpp
100 lines (88 loc) · 2.12 KB
/
range_distinct_query_faster.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
88
89
90
91
92
93
94
95
96
97
98
99
100
// online版がある
#include <bits/stdc++.h>
using namespace std;
class BIT {
int n, N;
vector<int> data;
int sum(int i) {
int s = 0;
while (i > 0) {
s += data[i];
i -= i & -i;
}
return s;
}
public:
BIT(int n) : n(n), data(n + 1) {
N = 1;
while (N <= n) N <<= 1;
N >>= 1;
}
void add(int i, int x) {
i++;
while (i <= n) {
data[i] += x;
i += i & -i;
}
}
// [l, r)
int sum(int l, int r) {
return sum(r) - sum(l);
}
};
struct OfflineRangeDistinctQuery {
int MAX = 0;
int n;
vector<int> as, last_visit, order, le, ri;
BIT bit;
int qcnt = 0;
OfflineRangeDistinctQuery(const vector<int> &as, int MAX) : n(as.size()), as(as), MAX(MAX), last_visit(MAX + 1, -1), bit(n) {}
// [l, r]閉区間に対するクエリ。0-indexed
void add_query(int l, int r) {
le.push_back(l);
ri.push_back(r);
}
vector<int> answer() {
int qcnt = le.size();
vector<int> ret(qcnt);
order.resize(qcnt);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&](int a, int b) {
return ri[a] < ri[b];
});
int idx = 0;
for (int i = 0; i < n; i++) {
if (last_visit[as[i]] != -1) {
bit.add(last_visit[as[i]], -1);
}
last_visit[as[i]] = i;
bit.add(i, 1);
while (idx < qcnt && ri[order[idx]] == i) {
ret[order[idx]] = bit.sum(le[order[idx]], ri[order[idx]] + 1);
idx++;
}
}
return ret;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> as(n);
for (int i = 0; i < n; i++) cin >> as[i];
OfflineRangeDistinctQuery rdq(as, 1000000);
int q;
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
a--, b--;
rdq.add_query(a, b);
}
vector<int> ans = rdq.answer();
for (int a : ans) {
cout << a << '\n';
}
}