AW239 奇偶游戏

题目地址


注意点:

  • 当小A的回答为偶数时,存储在结构体内的值应当设为0(防止中间状态不合法).

易错点:

  • 并查集注意初始化fa[i]=i.
  • 离散化后,使用时应当先进行二分(lower_bound(a+1,a+n+1)-a)获取离散化后的值.
  • 使用并查集时,应当使用输入时获取的数据,而不是并查集编号.(p->x,q->y)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXM=2e4;
struct Query{
	int l,r,ans;
}query[MAXM];
int fa[MAXM],type[MAXM];
int get(int x){
	if(x==fa[x])return x;
	int root=get(fa[x]);
	type[x]^=type[fa[x]];
	return fa[x]=root;
}
int a[MAXM],cnt=0;
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		char tmp[10];
		scanf("%d%d%s",&query[i].l,&query[i].r,tmp);
		a[++cnt]=query[i].l-1;
		a[++cnt]=query[i].r;
		query[i].ans=tmp[0]=='o'?1:0;
	}
	sort(a+1,a+cnt+1);
	n=unique(a+1,a+cnt+1)-a-1;
	for(int i=1;i<=n;i++){
		fa[i]=i;
	}
	for(int i=1;i<=m;i++){
		Query nowQuery=query[i];
		int x=lower_bound(a+1,a+n+1,nowQuery.l-1)-a;
		int y=lower_bound(a+1,a+n+1,nowQuery.r)-a;
		int p=get(x),q=get(y);
		if(p==q){
			if(type[x]^type[y]==nowQuery.ans)continue;
			printf("%d
",i-1);
			return 0;
		}else{
			fa[p]=q;
			type[p]=type[x]^type[y]^nowQuery.ans;
		}
	}
	printf("%d
",m);
	return 0;
}
原文地址:https://www.cnblogs.com/zbsy-wwx/p/11680544.html