莫队裸题还不带修改
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n, m, qcnt, ans, col[1000005], bse, blc[50005], a[50005], qwq[200005];
struct Query{
int xxx, yyy, idx;
}q[200005];
bool cmp(Query u, Query v){
if(blc[u.xxx]==blc[v.xxx]) return u.yyy<v.yyy;
else return blc[u.xxx]<blc[v.xxx];
}
void del(int x){
if(--col[x]==0) ans--;
}
void add(int x){
if(++col[x]==1) ans++;
}
void md(){
int l=1, r=0;
for(int i=1; i<=m; i++){
while(l<q[i].xxx) del(a[l++]);
while(l>q[i].xxx) add(a[--l]);
while(r<q[i].yyy) add(a[++r]);
while(r>q[i].yyy) del(a[r--]);
qwq[q[i].idx] = ans;
}
}
int main(){
cin>>n;
bse = sqrt(n);
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
blc[i] = (i - 1) / bse + 1;
}
cin>>m;
for(int i=1; i<=m; i++){
scanf("%d %d", &q[i].xxx, &q[i].yyy);
q[i].idx = i;
}
sort(q+1, q+1+m, cmp);
md();
for(int i=1; i<=m; i++)
printf("%d\n", qwq[i]);
return 0;
}