-
Notifications
You must be signed in to change notification settings - Fork 0
/
DQUERY.cpp
100 lines (85 loc) · 1.55 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
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
#include "bits/stdc++.h"
#define ll __int64
using namespace std;
int Query[200005];
int arr[30005];
int freq[1000005] ;// should be 1000000;
int ans,block;
struct node
{
int x,y,idx;
};
void add(int x)
{
freq[x]++;
if (freq[x]==1)
{
ans++;
}
}
void remove(int x)
{
freq[x]--;
if (freq[x]==0)
{
ans--;
}
}
bool cmp(node m,node n)
{
if (m.x/block!=n.x/block)
{
return m.x/block < n.x/block;
}
return m.y < n.y;
}
int main()
{
//ios::sync_with_stdio(0); cin.tie(0);
int n,Q;
scanf("%d",&n);
block=(int)sqrt(n);
for (int i = 0; i <n; ++i)
scanf("%d",arr+i);
scanf("%d",&Q);
node *N=(node*)malloc(sizeof(node)*Q);
for (int i = 0; i < Q; ++i)
{
scanf("%d%d",&N[i].x,&N[i].y);
N[i].x--,N[i].y--;
N[i].idx=i;
}
sort(N,N+Q,cmp);
int L,R,CurL=0,CurR=-1;
for (int i = 0; i < Q; ++i)
{
L=N[i].x,R=N[i].y;
while(CurL < L)
{
remove(arr[CurL]);
CurL++;
}
while(CurL > L)
{
CurL--;
add(arr[CurL]);
}
while(CurR < R)
{
CurR++;
add(arr[CurR]);
}
while(CurR > R)
{
remove(arr[CurR]);
CurR--;
}
Query[N[i].idx]=ans;
}
for (int i = 0; i < Q; ++i)
{
printf("%d\n", Query[i]);
}
free(N);
return 0;
}