forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdquery.cpp
62 lines (62 loc) · 1.42 KB
/
dquery.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
// 2014-05-29
#include <cstdio>
#include <utility>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int BIT[30001];
int n;
int loc[1000001];
int query(int i) {
int res = 0;
while (i > 0) {
res += BIT[i];
i -= i & -i;
}
return res;
}
int sum = 0;
void update(int i, int x) {
sum += x;
while (i <= n) {
BIT[i] += x;
i += i & -i;
}
}
int a[30001];
int main() {
memset(loc, -1, sizeof(loc));
scanf("%d", &n);
vector<pair<int, int> > events;
for (int i = 1; i <= n; i++) {
scanf("%d", a+i);
events.push_back(make_pair(i, -1));
}
int q; scanf("%d", &q);
vector<pair<int, int> > queries;
vector<int> answers(q);
for (int i = 0; i < q; i++) {
int a, b; scanf("%d %d", &a, &b);
queries.push_back(make_pair(a, b));
events.push_back(make_pair(b, i));
}
sort(events.begin(), events.end());
for (int i = 0; i < events.size(); i++) {
pair<int, int> evt = events[i];
if (evt.second == -1) {
int j = evt.first;
if (loc[a[j]] != -1) {
update(loc[a[j]], -1);
}
update(j, 1);
loc[a[j]] = j;
} else {
int l = queries[evt.second].first;
answers[evt.second] = sum - query(l - 1);
}
}
for (int i = 0; i < q; i++) {
printf("%d\n", answers[i]);
}
}