普通莫队板子

int n,m;
int a[maxn];
int cnt[maxn];
struct node{
	int l,r,id;
}q[maxn];
int k;
bool cmp(node a,node b){
	if(a.l/k!=b.l/k)return a.l/k < b.l/k;
	if((a.l/k)&1)return a.r<b.r;else return a.r>b.r;//优化常数
}
int res;
void add(int x){
	if(!cnt[a[x]])res++;
	cnt[a[x]]++;
}
void del(int x){
	cnt[a[x]]--;
	if(!cnt[a[x]])res--;
}
sort(q.begin(),q.end(),cmp);
for(int i=0,l=1,r=0;i<m;i++){
	while(l>q[i].l)add(--l);
	while(l<q[i].l)del(l++);
	while(r<q[i].r)add(++r);
	while(r>q[i].r)del(r--);
	ans[q[i].id]=res;
}
k=sqrt(n);
posted @ 2025-04-04 17:35  Marinaco  阅读(14)  评论(0)    收藏  举报
//雪花飘落效果