社区讨论

splay写的T一个点,不是常数问题

P2596[ZJOI2006] 书架参与者 1已保存回复 0

讨论操作

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

当前回复
0 条
当前快照
1 份
快照标识符
@mi6y1tzm
此快照首次捕获于
2025/11/20 12:41
4 个月前
此快照最后确认于
2025/11/20 12:41
4 个月前
查看原帖
麻烦大佬看看可能T在哪里
CPP
#include<cstdio>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=80005;

int np=0,rt=0;
int v[MAXN];
int fa[MAXN];
int sz[MAXN];
int pos[MAXN];
int ch[MAXN][2];

int new_node(int x){
	np++;
	sz[np]=1;
	v[np]=x;
	pos[x]=np;
	return np;
}
int rel(int x){
	return x==ch[fa[x]][1];
}
void link(int f,int son,int side){
	fa[son]=f;
	ch[f][side]=son;
}
void pushup(int x){
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
	pos[v[ch[x][0]]]=ch[x][0];
	pos[v[ch[x][1]]]=ch[x][1];
}
void rotate(int x){
	int side=rel(x);
	int f=fa[x],f_f=fa[f];
	int son=ch[x][side^1];
	link(f,son,side);
	link(f_f,x,rel(f));
	link(x,f,side^1);
	pushup(f);
	pushup(x);
}
void splay(int x,int goal){
	while(fa[x]!=goal){
		if(fa[fa[x]]!=goal){
			if((rel(x)^rel(fa[x]))==1)
				rotate(fa[x]);
			else rotate(x);
		}
		rotate(x);
	}
	pos[v[x]]=x;
	if(goal==0)rt=x;
}
void init(int x){
	int now;
	now=new_node(x);
	if(rt!=0)fa[now]=rt;
	if(rt!=0)ch[rt][1]=now;
	splay(now,0);
}
void Top_Bottom(int x,int op){
	splay(pos[x],0);
	if(ch[rt][op]==0)return;
	if(ch[rt][op^1]==0)
		swap(ch[rt][0],ch[rt][1]);
	else{
		int now=ch[rt][op^1];
		while(ch[now][op])now=ch[now][op];
		link(now,ch[rt][op],op);
		ch[rt][op]=0;
		splay(ch[now][op],0);
	}
}
void Insert(int x,int op){
	if(op==0)return;
	splay(pos[x],0);
	int t=(op==-1)?0:1;
	int p=pos[x],now=ch[rt][t];
	while(ch[now][t^1])now=ch[now][t^1];
	swap(pos[x],pos[v[now]]);
	swap(v[p],v[now]);
}
int Ask(int x){
	splay(pos[x],0);
	return sz[ch[rt][0]];
}
int Query(int x){
	int now=rt;
	while(1){
		if(x>sz[ch[now][0]]+1){
			x-=sz[ch[now][0]]+1;
			now=ch[now][1];
		}
		else if(x<=sz[ch[now][0]])
			now=ch[now][0];
		else return v[now];
	}
}

int main(){
//	freopen("in.txt","r",stdin);
	ios::sync_with_stdio(false);
	int N,M,x,t;
	char op[20];
	cin>>N>>M;
	for(int i=1;i<=N;i++){
		cin>>x;init(x);
	}
	while(M--){
		cin>>op;
		if(op[0]=='T'){
			cin>>x;
			Top_Bottom(x,0);
		}
		else if(op[0]=='B'){
			cin>>x;
			Top_Bottom(x,1);
		}
		else if(op[0]=='I'){
			cin>>x>>t;
			Insert(x,t);
		}
		else if(op[0]=='A'){
			cin>>x;
			cout<<Ask(x)<<endl;
		}
		else{
			cin>>x;
			cout<<Query(x)<<endl;
		}
	}
	return 0;
}

回复

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

正在加载回复...