关于数据强度的疑问
查看原帖
关于数据强度的疑问
390742
qwqUwU楼主2022/3/30 13:31

这是我这题的代码:

#include<bits/stdc++.h>
#define ll long long
#define lson tree[x].son[0]
#define rson tree[x].son[1]
using namespace std;
const int N=1e5+10;
const int INF=0x3fffffff;
inline int read(){
	int x=0,c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x;
}
int n,m,Q;
struct Balanced_Tree{
	struct Tree{
		int fa,son[2],vall,siz;
	};
	vector<Tree> tree;
	int root;
	inline void pushup(int x){
		tree[x].siz=tree[lson].siz+tree[rson].siz+1;
	}
	inline int New(int val,int fa){
		tree.push_back((Tree){fa,{0,0},val,1});
		return tree.size()-1;
	}
	inline void build(){
		tree.push_back((Tree){0,{0,0},0,0});
		root=New(-INF,0);
		tree[root].son[1]=New(INF,root);
		pushup(root);
	}
	inline void rotate(int x){
		int y=tree[x].fa,z=tree[y].fa;
		int d=(tree[y].son[1]==x);
		tree[z].son[tree[z].son[1]==y]=x,tree[x].fa=z;
		tree[y].son[d]=tree[x].son[d^1],tree[tree[x].son[d^1]].fa=y;
		tree[x].son[d^1]=y,tree[y].fa=x;
		pushup(y),pushup(x);
	}
	inline void splay(int x,int goal){
		while(tree[x].fa!=goal){
			int y=tree[x].fa,z=tree[y].fa;
			if(z!=goal)(tree[y].son[1]==x)^(tree[z].son[1]==y)?rotate(x):rotate(y);
			rotate(x);
		}
		if(goal==0)root=x;
	}
	inline void Insert(int val){
		int x=root,fa=0;
		while(x&&tree[x].vall!=val){
			fa=x;
			x=tree[x].son[tree[x].vall<val];
		}
		x=New(val,fa);
		if(fa)tree[fa].son[tree[fa].vall<val]=x;
		splay(x,0);
	}
	inline int kth(int rank){
		int x=root;
		while(1){
			int y=tree[x].son[0];
			if(rank>tree[y].siz+1){
				rank-=tree[y].siz+1;
				x=tree[x].son[1];
			}
			else if(rank<=tree[y].siz)x=y;
			else {
				splay(x,0);
				return tree[x].vall;
			}
		}
	}
}t[N];
int p[N],Val[N],Fa[N],Size[N];
inline int find(int x){
	return x==Fa[x]?x:Fa[x]=find(Fa[x]);
}
inline void Union(int u,int v){
	Fa[u]=v;
	Size[v]+=Size[u],Size[u]=0;
	for(int i=1;i<t[u].tree.size();i++){
		int V=t[u].tree[i].vall;
		if(V==-INF||V==INF)continue;
		t[v].Insert(V);
	}
	t[u].tree.clear();
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++)p[i]=read(),Val[p[i]]=i;
	for(int i=1;i<=n;i++){
		Fa[i]=i;
		t[i].build();
		t[i].Insert(p[i]);
		Size[i]=1;
	}
	while(m--){
		int u=find(read()),v=find(read());
		if(u==v)continue;
		if(Size[u]>Size[v])swap(u,v);
		Union(u,v);
	}
	scanf("%d",&Q);
	while(Q--){
		char op=getchar();
		while(op!='Q'&&op!='B')op=getchar();
		if(op=='Q'){
			int x=find(read()),y=read();
			if(y>t[x].tree.size()-2)printf("-1\n");
			else printf("%d\n",Val[t[x].kth(y+1)]);
		}
		if(op=='B'){
			int u=find(read()),v=find(read());
			if(u==v)continue;
			if(Size[u]>Size[v])swap(u,v);
			Union(u,v);
		}
	}
	return 0;
}

它不能通过样例,在调试程序时甚至会 RE,但交上去能 AC.

所以数据这么水吗?

2022/3/30 13:31
加载中...