-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBIT, Offline queries, DQUERY
84 lines (73 loc) · 1.4 KB
/
BIT, Offline queries, DQUERY
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define gc getchar_unlocked
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define vi vector<int>
inline int in()
{int NR=0,sign=1; char c=gc();
while( c < 48 || c > 57 ){if(c=='-')sign=0; c=gc();}
while(c>47 && c< 58)
{ NR = (NR << 3) + (NR << 1) + (c - 48); c=gc(); }
return (sign?NR:(-NR));
}
const int m=(int)1e6;
int b[m], BIT[m];
void update(int x, int n, int u)
{
for(; x <= n; x += x&-x)
b[x] += u;
}
int query(int x)
{
int sum = 0;
for(; x > 0; x -= x&-x)
sum += b[x];
return sum;
}
int main(){
int n,q,i,j,k,l,r,,h;
n=in();
int aorig[n+1];
for(i=1; i<=n; i++)
aorig[i]=in();
int last[m+3];
pii a[n+1];
for(i=0;i<=m+1; i++)
last[i]=0;
for(i=1; i<=n; i++){
a[i] = mp(last[aorig[i]],i);
last[aorig[i]] = i;
}
q=in();
pii qsort[q];
pii qorig[q];
for(i=0;i<q;i++){
l=in();
r=in();
qorig[i] = mp(l,r);
qsort[i] = mp(l,i);
}
sort(qsort,qsort+q);
sort(a+1,a+n+1);
int ptr=1,j;
int ans[200010];
for(int num=0; num<q; num++)
{
while(ptr<=n && a[ptr].first < qsort[num].first){
j=a[ptr].second;
update(j,n,1);
ptr++;
}
h = query(qorig[qsort[num].se].se) - query(qsort[num].fi-1);
ans[qsort[num].se] = h;
}
for(i=0;i<n;i++){
printf("%d\n",ans[i]);
}
return 0;
}