这道题在省赛接触过类似的 后来学莫队的时候把他刷了 今天把他二做一下 做法呢就是建立pre数组 pre代表这个数前面那个数出现的坐标 一开始都是0 查询区间内有多少数的pre数组小于l 那么也就是区间内有多少不同种类的数 但是如果x是1 我的版本写的有点炸 写个小特判预处理即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 30025;
int n,m,cnt,x,y,root[MAX_N],arr[MAX_N],pre[MAX_N],use[1000025],sum[MAX_N];
struct node{int l,r,sum;}T[MAX_N*40];
vector<int > vt;
int getid(int x){return lower_bound(vt.begin(),vt.end(),x)-vt.begin()+1;};
void update(int l,int r,int &x,int y ,int pos)
{
T[++cnt] = T[y],T[cnt].sum++,x = cnt;
if(l==r) return ;
int mid =(l+r)>>1;
if(pos<=mid) update(l,mid,T[x].l,T[y].l,pos);
else update(mid+1,r,T[x].r,T[y].r,pos);
}
int query(int l,int r,int x,int y,int k)
{
if(l==r) return T[y].sum - T[x].sum;
int mid = (l+r)>>1,ret = 0;
if(k<=mid)
{
ret+=query(l,mid,T[x].l,T[y].l,k);
}
else
{
ret+=T[T[y].l].sum - T[T[x].l].sum;
ret+=query(mid+1,r,T[x].r,T[y].r,k);
}
return ret;
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
scanf("%d",&n);
for(int i = 1;i<=n;++i)
{
scanf("%d",&arr[i]);
pre[i] = use[arr[i]];
use[arr[i]] = i;
}
for(int i = 1;i<=n;++i)
{
if(pre[i]==0) sum[i] = sum[i-1] + 1;
else sum[i] = sum[i-1];
}
for(int i = 1;i<=n;++i) update(1,n,root[i],root[i-1],pre[i]);
scanf("%d",&m);
for(int i = 1;i<=m;++i)
{
scanf("%d%d",&x,&y);
if(x==1) printf("%d\n",sum[y]);
else printf("%d\n",query(1,n,root[x-1],root[y],x-1));
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}