-
Notifications
You must be signed in to change notification settings - Fork 9
/
DQUERY - D-query(MO).cpp
77 lines (76 loc) · 1.56 KB
/
DQUERY - D-query(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
68
69
70
71
72
73
74
75
76
77
#include<bits/stdc++.h>
using namespace std;
int block_size;
int arr[30004];
struct Query
{
int l;
int r;
int idx;
bool operator<(const Query& ob) const
{
if(l/block_size != ob.l/block_size)
return l/block_size < ob.l/block_size;
return r<ob.r;
}
};
Query query[200005];int ans[200005];
int cnt[1000006];
int answer;
void add(int pos)
{
int val = arr[pos];
cnt[val]++;
if(cnt[val]==1) answer++;
}
void remove(int pos)
{
int val = arr[pos];
cnt[val]--;
if(cnt[val]==0) answer--;
}
void solve(int n, int q)
{
block_size = sqrt(n)+1;
answer = 0;
memset(cnt, 0, sizeof cnt);
sort(query, query+q);
int currL = 0, currR = -1;
for(int i=0; i<q; i++){
int L = query[i].l;
int R = query[i].r;
while(currL > L){
currL--;
add(currL);
}
while(currR < R){
currR++;
add(currR);
}
while(currL < L){
remove(currL);
currL++;
}
while(currR > R){
remove(currR);
currR--;
}
ans[ query[i].idx ] = answer;
}
}
int main()
{
int n,q;
scanf("%d",&n);
for(int i=0; i<n; i++) scanf("%d",&arr[i]);
scanf("%d",&q);
for(int i=0; i<q; i++){
scanf("%d%d",&query[i].l, &query[i].r);
query[i].l--;
query[i].r--;
query[i].idx = i;
}
solve(n,q);
for(int i=0; i<q; i++)
printf("%d\n", ans[i]);
}