-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdquery.cpp
More file actions
73 lines (71 loc) · 1.22 KB
/
dquery.cpp
File metadata and controls
73 lines (71 loc) · 1.22 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
70
71
72
73
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
struct val {
int x,y,idx;
};
int block;
bool cmp(val a,val b) {
if(a.x/block!=b.x/block)
return a.x<b.x;
return a.y<b.y;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<int> A(n);
for(int i=0;i<n;i++)
cin>>A[i];
int m;
cin>>m;
vector<val> v(m);
for(int i=0;i<m;i++) {
int a,b;
cin>>a>>b;
v[i].x=a-1;
v[i].y=b-1;
v[i].idx=i;
}
block=sqrt(n);
sort(v.begin(),v.end(),cmp);
vector<int> ans(m),freq(1000001,0);
int l=0,r=-1,c=0;
for(int i=0;i<m;i++) {
while(r<v[i].y) {
r++;
freq[A[r]]++;
if(freq[A[r]]==1)
c++;
}
while(r>v[i].y) {
freq[A[r]]--;
if(freq[A[r]]==0)
c--;
r--;
}
while(l>v[i].x) {
l--;
freq[A[l]]++;
if(freq[A[l]]==1)
c++;
}
while(l<v[i].x) {
freq[A[l]]--;
if(freq[A[l]]==0)
c--;
l++;
}
ans[v[i].idx]=c;
}
for(int i=0;i<m;i++)
cout<<ans[i]<<"\n";
return 0;
}