好高兴!一遍过,但我觉得有些地方比较冗余
查看原帖
好高兴!一遍过,但我觉得有些地方比较冗余
591458
qinziao楼主2024/8/16 12:01
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+3,M=1e6+3;
struct pp{
	int l,r,ans,vi;
}q[M];
int n,m,a[N],t[N],last[N];
bool p[N];
int lowbit(int x){
	return x&(-x);
}
bool cmp1(const pp &a,const pp &b){
	return a.r<b.r;
}
bool cmp2(const pp &a,const pp &b){
	return a.vi<b.vi;
}
void update(int i){
	while(i<=n){
		t[i]++;
		i+=lowbit(i);
	}
}
void det(int i){
	while(i<=n){
		t[i]--;
		i+=lowbit(i);
	}
}
int sum(int i){
	int e=0;
	while(i>=1){
		e+=t[i];
		i-=lowbit(i);
	}
	return e;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		scanf("%d%d",&q[i].l,&q[i].r);
		q[i].vi=i;
	}
	sort(q+1,q+1+m,cmp1);
	int k=1;
	for(int i=1;i<=n;i++){
		if(p[a[i]]){
			det(last[a[i]]);
		}
		else{
			p[a[i]]=1;
		}
		update(i);
		last[a[i]]=i;
		while(1){
			if(q[k].r!=i){
				break;
			}
			q[k].ans=sum(q[k].r)-sum(q[k].l-1);
			k++;
		}
	}
	sort(q+1,q+1+m,cmp2);
	for(int i=1;i<=m;i++){
		printf("%d\n",q[i].ans);
	}
	return 0;
}
2024/8/16 12:01
加载中...