社区讨论

样例过,下载的数据也一模一样,为什么全WA?(Splay+并查集)

P3224[HNOI2012] 永无乡参与者 3已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@lwkwyxoi
此快照首次捕获于
2024/05/25 00:46
2 年前
此快照最后确认于
2024/05/25 10:25
2 年前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;
const int N=4e5;
struct Node {
	int son[2],fa,val,sz;
}t[N];
int n,m,root[N];
void pushup (int x)
{
	t[x].sz=t[t[x].son[0]].sz+t[t[x].son[1]].sz+1;
}
void rotate (int x)
{
	int y=t[x].fa, z=t[y].fa,k=(t[y].son[1]==x);
	t[z].son[t[z].son[1]==y]=x; t[x].fa=z;
	t[y].son[k]=t[x].son[k^1]; t[t[x].son[k^1]].fa=y;
	t[x].son[k^1]=y; t[y].fa=x;
	pushup(y); pushup(x);
}
void splay (int x,int r)
{
	while (t[x].fa!=r) {
		int y=t[x].fa,z=t[y].fa;
		if (z!=r) ((t[z].son[1]==y)^(t[y].son[1]==x))?rotate(x):rotate(y);
		rotate(x);
	}
	if (r==0) root[root[x]]=root[x]=x; 
}
void insert (int x,int v,int fa)  // 将第v个插入到第x个的子树里 
{
	if (x==0) {
		t[fa].son[t[fa].val<t[v].val]=v;
		t[v].fa=fa;
		return ;
	}
	insert(t[x].son[t[x].val<t[v].val],v,x);
	pushup(x);
}
int find_num (int now,int x)
{
	if (now==0) return -1;
	if (t[t[now].son[0]].sz+1==x) return now;
	if (t[t[now].son[0]].sz>=x) return find_num(t[now].son[0],x);
	else return find_num(t[now].son[1],x-t[t[now].son[0]].sz-1);
}
int find (int x)
{
	return (root[x]==x)?x:root[x]=find(root[x]);
}
int main()
{
//	freopen("P3224_1.in","r",stdin);
//	freopen("P3224_1.txt","w",stdout);
//	ios_base::sync_with_stdio(false);
//	cin.tie(0); cout.tie(0);
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) {
		scanf("%d",&t[i].val);
		t[i].sz=1;
		root[i]=i;
	}
	for (int i=1;i<=m;i++) {
		int x,y;
		scanf("%d%d",&x,&y);
		if (find(x)!=find(y)) {
			splay(x,0);
			insert(find(y),x,0);
			root[x]=find(y);
			splay(x,0);
		} 
	}
	scanf("%d",&m);
	int flag=0;
	for (int i=1;i<=m;i++) {
		char f; int x,y;
		f=getchar(); while (f<'A' || f>'Z') f=getchar();
		scanf("%d%d",&x,&y);
		if (f=='Q') {
			if (flag==1) printf("\n");
			if (flag==0) flag=1;
			printf("%d",find_num(find(x),y));
			splay(x,0); 
		}
		else {
			splay(x,0);
			insert(find(y),x,0);
			root[x]=find(y);
			splay(x,0);
		}
//		for (int j=1;j<=n;j++) 
//			cout<<t[j].fa<<" "<<t[j].son[0]<<" "<<t[j].son[1]<<" "<<t[j].val<<" "<<root[j]<<endl; 
	}
	return 0;
}

回复

4 条回复,欢迎继续交流。

正在加载回复...