-
Notifications
You must be signed in to change notification settings - Fork 9
/
DQUERY - D-query.cpp
85 lines (85 loc) · 2.13 KB
/
DQUERY - D-query.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
#include<bits/stdc++.h>
using namespace std;
#define mx 30005
int arr[mx], tree[4*mx], mp[1000005];
int ans[ 200005];
struct str
{
int x, y, ind;
bool operator<(const str &ob)const
{
return y<ob.y;
}
};
void update(int pos, int b, int e, int i, int j, int val)
{
if(b>e) return;
if(b>j || e<i) return;
if(b>=i && e<=j){
tree[pos] = val;
return;
}
int mid = (b+e)/2;
int left = pos*2;
int right = left + 1;
update(left, b, mid, i, j, val);
update(right, mid+1, e, i, j, val);
tree[pos] = tree[left] + tree[right];
}
int query(int pos, int b, int e, int i, int j)
{
if(b>e) return 0;
if(b>j || e<i) return 0;
if(b>=i && e<=j){
return tree[pos];
}
int mid = (b+e)/2;
int left = pos*2;
int right = left + 1;
int q1 = query(left, b, mid, i, j);
int q2 = query(right, mid+1,e, i, j);
return q1 + q2;
}
int main()
{
int tc, n, q, cs=1,a,b;
//scanf("%d", &tc);
tc = 1;
while(tc--){
memset(tree, 0, sizeof tree);
memset(mp, -1, sizeof mp);
scanf("%d", &n);
for(int i=1; i<=n; i++){
scanf("%d", &arr[i]);
}
scanf("%d", &q);
vector<str> qry(q);
for(int i=0; i<q; i++){
scanf("%d%d", &qry[i].x, &qry[i].y);
qry[i].ind = i;
}
sort(qry.begin(), qry.end());
int idx = 1;
for(int i=0; i<q; i++){
for(idx; idx<=qry[i].y; idx++){
int val = arr[idx];
if(mp[val] == -1){
mp[val] = idx;
update(1,1,n,idx,idx,1);
}
else{
int ii = mp[val];
update(1,1,n,ii,ii,0);
mp[val] = idx;
update(1,1,n,idx,idx,1);
}
}
ans[ qry[i].ind ] = query(1,1,n,qry[i].x, qry[i].y);
}
//printf("Case %d:\n", cs++);
for(int i=0; i<q; i++){
printf("%d\n", ans[i]);
}
}
return 0;
}