90分求助
查看原帖
90分求助
144118
z_c_c楼主2021/9/24 15:13

Code:

#include<bits/stdc++.h>
using namespace std;
const int mm=820080*4;
const int inf=2147483647;
int n,m,root,tot,p[mm],q[mm];
struct node{
	int s[2],sz,val,fa;
}a[mm];
void BSTpr(int rt)
{
	if(a[rt].s[0]>0) BSTpr(a[rt].s[0]);
	cout<<a[rt].val<<' ';
	if(a[rt].s[1]>0) BSTpr(a[rt].s[1]);
}
int Build(int l,int r)
{
	if(l>r) return 0;
	if(l==r)
	{
		a[l].val=p[l];
		a[l].s[0]=a[l].s[1]=0;
		a[l].sz=1;
		return l;
	}
	int m=(l+r)/2;
	a[m].val=p[m];
	int L=a[m].s[0]=Build(l,m-1);
	int R=a[m].s[1]=Build(m+1,r);
	a[L].fa=a[R].fa=m;
	a[m].sz=a[L].sz+a[R].sz+1;

	return m;
}
void ArrayPr()   /// 调试监视使用
{
        cout<<endl<<"  i ";
        for (int i=1; i<=tot; i++) cout<<setw(3)<<i;
        cout<<endl<<"val ";
        for (int i=1; i<=tot; i++) cout<<setw(3)<<a[i].val;
        cout<<endl<<" s0 ";
        for (int i=1; i<=tot; i++) cout<<setw(3)<<a[i].s[0];
        cout<<endl<<" s1 ";
        for (int i=1; i<=tot; i++) cout<<setw(3)<<a[i].s[1];
        cout<<endl<<" sz ";
        for (int i=1; i<=tot; i++) cout<<setw(3)<<a[i].sz;
        cout<<endl<<" fa ";
        for (int i=1; i<=tot; i++) cout<<setw(3)<<a[i].fa;
        cout<<endl;
}
void pushup(int rt)
{
	int l=a[rt].s[0],r=a[rt].s[1];
	a[rt].sz=a[l].sz+a[r].sz+1;
	a[q[l]].val=l,a[q[r]].val=r;
	}
int son(int x)
{
	int y=a[x].fa;
	return a[y].s[1]==x;
}
void R(int x)
{
	int y=a[x].fa;
	int z=a[y].fa;
	int k=son(x);
	int v=a[x].s[k^1];
	a[y].s[k]=v,a[v].fa=y;
	a[z].s[son(y)]=x,a[x].fa=z;
    a[x].s[k^1]=y,a[y].fa=x;
	pushup(y);pushup(x);
	}
void splay(int x,int rt=root)
{
	rt=a[rt].fa;
	int y,z;
	while(a[x].fa!=rt)
	{
		y=a[x].fa,z=a[y].fa;
		if(z!=rt)
		if(son(x)==son(y)) R(y);
		else R(x);
		R(x);
}
a[q[x]].val=x;
 if(rt==0) root=x;
}
void top(int s)
{
	splay(s,root);
	int x=root;
	int l=a[x].s[0];
	int r=a[x].s[1];
	if(l==0) return;
	if(r==0) a[x].s[1]=l;
	else {
		int p=r;
		while(a[p].s[0]>0) p=a[p].s[0];

		a[p].s[0]=l;
		a[l].fa=p;
		splay(p,r);
		///pushup(p);
	}
	a[x].s[0]=0;
	///swap(a[s].val,a[root].val);
 }
 void bottom (int s)
 {
 	splay(s,root);
 	int x=root;
 	int l=a[x].s[0];
 	int r=a[x].s[1];
 	if(r==0) return;
 	if(l==0) a[x].s[0]=r;
 	else
 	{
 		int p=l;
		 while(a[p].s[1]>0) p=a[p].s[1];

		 a[p].s[1]=r;
		 a[r].fa=p;
		 splay(p,l);
		/// pushup(p);

	 }
	a[x].s[1]=0;
   /// swap(a[s].val,a[root].val);
 }
 int ask(int s)
 {
 	splay(s,root);
 	int l=a[root].s[0];
 	return a[l].sz;
 }
 int query(int rt,int s)
 {
 	if(rt==0) return 0;
 	int l=a[a[rt].s[0]].sz;
 	if(l+1==s) return a[rt].val;
 	if(l+1>s) return query(a[rt].s[0],s);
 	if(l+1<s) return query(a[rt].s[1],s-l-1);
 }
 int pre(int s)
 {
 	splay(s,root);
 	int l=a[s].s[0];
 	if(l==0) return 0;
 	int p=l;
 	while(a[p].s[1]>0) p=a[p].s[1];
 	return p;
 }
 int suc(int s)
 {
 	splay(s,root);
	 int r=a[s].s[1];
	 if(r==0) return 0;
	 int p=r;
	 while(a[p].s[0]>0) p=a[p].s[0];
	 return p;
 }
int main()
{
	///freopen("p2596_2.in","r",stdin);
	///freopen("p2596_2.out","w",stdout);

	cin>>n>>m;
	for(int i=1;i<=n;i++) { int x;
	cin>>x;p[i]=x; q[x]=i;}
	tot=n;
	root=Build(1,n);
	///BSTpr(root); cout<<endl;
//	for(int i=1;i<=n;i++) cout<<q[i]<<' ';
//	cout<<endl;


	string st;
	while(m--)
	{
		int s,t;
		cin>>st;
		if(st=="Top")
		{
			cin>>s;
			top(q[s]);
		}
		if(st=="Bottom")
		{
			cin>>s;
			bottom(q[s]);
		}
		if(st=="Insert")
		{
			cin>>s>>t;
			///splay(q[s],root);
			int x;
			if(t!=0){
			if(t==-1) x=pre(q[s]);
			if(t==1)  x=suc(q[s]);
			int kk=q[s],kkk=a[x].val;
			swap(q[a[x].val],q[s]);
			swap(a[x].val,a[kk].val);
 		}}
 		if(st=="Ask")
 		{
 			cin>>s;
 			cout<<ask(q[s])<<endl;
		 }
		 if(st=="Query")
		 {
		 	cin>>s;
		 	cout<<query(root,s)<<endl;
		 }
		///ArrayPr();
	}
return 0;
}
///myself

最后一个点实在找不到错误了啊QwQ 调了好久了,希望dalao帮忙调试一下

2021/9/24 15:13
加载中...