50pts莫队TLE求条
查看原帖
50pts莫队TLE求条
1021786
GeorgeDeng楼主2025/1/6 09:32
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

inline int read(){
	int x = 0,f = 1;
	char ch;
	ch = getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f = -f;
		ch = getchar();
	}
	while(ch>='0'&&ch<='9'){
		x = x*10+(ch-'0');
		ch = getchar();
	}
	return x*f;
}
struct node{
	int l,r;
	int id;
};
int n,q;
int answer = 0,curL,curR;
bool cmp(node x,node y){
	if(x.l/sqrt(n)==x.l/sqrt(n))
	{
		if((int)(x.l/sqrt(n))&1) return x.r<y.r;
		return x.r>y.r;
	}
	return x.l<y.l;
}
node query[100005];
int a[100005];
int cnt[100005];
void add(int pos){
    if((++cnt[a[pos]])==1) ++answer;
}
void del(int pos){
    if((--cnt[a[pos]])==0) --answer;
}
int ans[100005];
signed main()
{
//	freopen("P3901_1.in","r",stdin);
//	freopen("P3901.out","w",stdout);
	n = read(),q = read();
	for(int i = 1;i<=n;i++){
		a[i] = read();
	}
	for(int i = 1;i<=q;i++){
		query[i].l = read(),query[i].r = read();
		query[i].id = i;
	}
	sort(query+1,query+1+q,cmp);
	for(int i = 1;i<=q;i++){
		int l = query[i].l,r = query[i].r;
		while(curL<l){
			del(curL++);
		}
		while(curL>l){
			add(--curL);
		}
		while(curR<r){
			add(++curR);
		}
		while(curR>r){
			del(curR--);
		}
		if(answer==(r-l+1)){
			ans[query[i].id] = 1;
		}
	}
	for(int i = 1;i<=q;i++){
		if(ans[i]){
			cout<<"Yes"<<endl;
		}else{
			cout<<"No"<<endl;
		}
	}
	return 0;
}

2025/1/6 09:32
加载中...