1. 程式人生 > 實用技巧 >P3901 數列找不同

P3901 數列找不同

Miku

還是莫隊板子

啥叫互不相同?區間內不同的元素的數量==區間元素數

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
int l=1,r;
int cnt;
int now[100005];
struct q{
	int l;
	int r;
	int id;
}qo[100005];
int n,m;
int block[100005];
int b;
int f[100005];
int ans[100005];
bool cmp(q x, q y){
	return block[x.l]==block[y.l] ? x.r<y.r : block[x.l]<block[y.l];
}
void add(int x){
	if(now[f[x]]==0)
	cnt++;
	now[f[x]]++;
}
void del(int x){
	now[f[x]]--;
	if(now[f[x]]==0)
	cnt--;
}
int main(){
	scanf("%d%d",&n,&m);
	b=ceil(sqrt(n));
	for(int i=1;i<=b;++i){
		for(int j=(i-1)*b;j<=i*b;++j)
			block[j]=i;
	}
	for(int i=1;i<=n;++i){
		scanf("%d",&f[i]);
	}
	for(int i=1;i<=m;++i){
		scanf("%d%d",&qo[i].l,&qo[i].r);
		qo[i].id=i;
	}
	sort(qo+1,qo+1+m,cmp);
	for(int i=1;i<=m;++i){

		while(l<qo[i].l){
			del(l++);
		}
		while(l>qo[i].l){
			add(--l);
		}
		while(r<qo[i].r){
			add(++r);
		}
		while(r>qo[i].r){
			del(r--);
		}
		if(qo[i].r-qo[i].l+1==cnt){
			ans[qo[i].id]=1;
		}else{
			ans[qo[i].id]=0;
		}
	}	
	for(int i=1;i<=m;++i){
		if(ans[i])
		printf("Yes\n");
		else
		printf("No\n");
	}
	return 0;
}