MnZn求助
查看原帖
MnZn求助
275079
Rainylower楼主2023/10/27 16:41

这个代码全WA

#include<bits/stdc++.h>
using namespace std;
#define int long long
int const maxn = 2e5 + 10;
int a[maxn];
int ed[maxn],st[maxn];
int bel[maxn];
struct node{
	int l,r,ans,num;
}p[maxn];

bool cmp(node x,node y){
	if(bel[x.l]==bel[y.l]){
		return x.r < y.r;
	}
	return x.l < y.l;
}

int x;
int cnt[maxn];
void add(int pos){
//	cout << a[pos]<<'\n';
	if(!cnt[a[pos]]){
		cnt[a[pos]] ++;
		x++;
	}else {
		cnt[a[pos]]++;
	}
	
}
void del(int pos){
	if(cnt[a[pos]]){
		if(!(--cnt[a[pos]]))x--;
	}
}
bool cmp1(node xx,node yy){
	return xx.num < yy.num;
}
signed main(){
	int n,q;
	cin >> n >> q;
	for(int i = 1;i <= n;i ++){
		cin >> a[i];
	}
	int sqlen = sqrt(n);
	int sqnum = n/sqlen;
	for(int i = 1;i <= sqnum;i ++){
		st[i] = (i-1)*sqlen + 1;
		ed[i] = i*sqlen;
	}
	ed[sqnum] = n;
	for(int i = 1;i <= sqnum;i ++){
		for(int j = st[i];j <= ed[i];j ++){
			bel[j] = i;
		}
	}
	for(int i = 1;i <= q;i ++){
		cin >> p[i].l >> p[i].r;
		p[i].num = i;
	}
	sort(p+1,p+q+1,cmp);
	int l = 1,r = 0;
	for(int i = 1;i <= q;i ++){
		int ll = p[i].l,rr = p[i].r;
		while(l < ll){
			del(l++);
		}
		while(l > ll){
			add(--l);
		}
		while(r < rr){
			add(++r);
		}
		while(r > rr){
			del(r--);
		}
     //   cout << r <<' '<<l<<' '<<r-l+1<<' '<<x  <<'\n';
		if(x==r-l+1){
			p[i].ans = 1;
		}else {
			p[i].ans = 0;
		}
	}
		
	
	sort(p+1,p+q+1,cmp1);
	for(int i = 1;i <= q;i ++)cout << ((p[i].ans==1) ? "Yes":"No") <<'\n';
	return 0;
}

但是只要把del里的最外层if删了后就能A了。为啥判断是否大于0必须删了。

2023/10/27 16:41
加载中...