-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdquery.cpp
More file actions
69 lines (64 loc) · 1.2 KB
/
dquery.cpp
File metadata and controls
69 lines (64 loc) · 1.2 KB
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define BLOCK 174
struct query
{
int l,r,ind;
}Q[200010];
int ans[200010];
int a[30010];
int cnt[1000010];
int answer;
void add(int index)
{
cnt[a[index]]++;
if(cnt[a[index]]==1) answer++;
}
void remove(int index)
{
cnt[a[index]]--;
if(cnt[a[index]]==0) answer--;
}
bool cmp(query f,query s)
{
if((f.l/BLOCK)!=(s.l/BLOCK))
return (f.l/BLOCK)<(s.l/BLOCK);
else
return f.r<s.r;
}
int main(){
ios_base::sync_with_stdio(false);
int n,q;
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&q);
for (int i=0;i<q;i++)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].ind=i;
Q[i].l--;
Q[i].r--;
}
sort(Q,Q+q,cmp);
int cl=0,cr=0;
for (int i=0;i<q;i++)
{
int left=Q[i].l,right=Q[i].r;
while(cl<left)
remove(cl),cl++;
while(cl>left)
add(cl-1),cl--;
while(cr<=right)
add(cr),cr++;
while(cr>(right+1))
remove(cr-1),cr--;
ans[Q[i].ind]=answer;
}
for (int i=0;i<q;i++)
printf("%d\n",ans[i]);
return 0;
}