求助Splay(指针),大概是某处漏了pushdown,但是找不到
查看原帖
求助Splay(指针),大概是某处漏了pushdown,但是找不到
171487
cmll02楼主2021/2/28 20:04

RT,求助

问题是我主函数后面有个dfs(调试用,顺带pushdown)

如果执行了就正确,没有就错,显然是少了pushdown。。。

#include <stdio.h>
#include <stdlib.h>
inline int read()
{
    int num=0,f=1;char c=getchar();
    while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
    while(c>47&&c<58)num=(num<<3)+(num<<1)+(c^48),c=getchar();
    return num*f;
}
struct Node{
	Node *fa,*lc,*rc;
	int sz,x,tag;
	Node(){fa=lc=rc=NULL;sz=x=tag=0;}
}*root,*o[100005];
inline int d(Node* x){return x->fa?x->fa->rc==x:-1;}
inline void pushdown(Node* x)
{
	if(x&&x->tag)
	{
		x->tag=0;
		Node *q=x->lc,*p=x->rc;
		{
			x->lc=x->rc;
			x->rc=q;
			p=x->lc,q=x->rc;
			if(p)p->tag^=1;
			if(q)q->tag^=1;
		}
	}
}
inline void maintain(Node* x)
{
	if(x==NULL)return;
	pushdown(x);
	x->sz=1;
	if(x->lc)x->sz+=x->lc->sz;
	if(x->rc)x->sz+=x->rc->sz;
}
inline void rotateR(Node *x)
{
	if(x==NULL)exit(19191810);
	if(x->fa==NULL)return;
	int f=d(x->fa);
	x->fa->lc=x->rc;
	if(x->rc)x->rc->fa=x->fa;
	x->rc=x->fa;
	Node *q=x->fa->fa;
	x->fa->fa=x;
	x->fa=q;
	if(q)
		if(f)q->rc=x;
		else q->lc=x;
	maintain(x->rc);
	maintain(x);
}
inline void rotateL(Node *x)
{
	if(x==NULL)exit(1919810);
	if(x->fa==NULL)return;
	int f=d(x->fa);
	x->fa->rc=x->lc;
	if(x->lc)x->lc->fa=x->fa;
	x->lc=x->fa;
	Node *q=x->fa->fa;
	x->fa->fa=x;
	x->fa=q;
	if(q)
		if(f)q->rc=x;
		else q->lc=x;
	maintain(x->lc);
	maintain(x);
}
inline void rotate(Node *x)
{
	int f=d(x);
	pushdown(x);
	if(f==1)rotateL(x);
	else if(f==0)rotateR(x);
}
inline int rotate2(Node *x,Node *end=NULL)
{
	if(x->fa==end)return 0;
	if(x->fa->fa!=end)
	{
		if(d(x)==d(x->fa))
		{
			rotate(x->fa);
			rotate(x);
		}
		else
		{
			rotate(x);
			rotate(x);
		}
		return 1;
	}
	else rotate(x);
	return 0;
}
inline void splay(Node *x,Node* end=NULL)
{
	while(rotate2(x,end));
	if(end==NULL)root=x;
}
inline void ins(Node *x,int v,Node *fa)
{
	if(x==NULL)
	{
		x=new Node();
		x->fa=fa;
		x->x=v;
		x->sz=1;
		x->lc=x->rc=NULL;
		if(fa->x>v)fa->lc=x;
		else fa->rc=x;
		splay(x);
		return;
	}
	if(x->x>v)ins(x->lc,v,x);
	else ins(x->rc,v,x);
	maintain(x);
}
inline Node* find_by_rnk_main(Node *x,int k)
{
	pushdown(x);
	int ls=x->lc?x->lc->sz:0;
	if(ls+1==k)return x;
	if(ls+1<k)return find_by_rnk_main(x->rc,k-ls-1);
	return find_by_rnk_main(x->lc,k);
}
inline int find_by_rnk_and_splay(int k,Node *end=NULL)
{
	Node* p=find_by_rnk_main(root,k);
	splay(p,end);
	return p->x;
}
inline int size()
{
	return root->sz-2;
}
inline void build(int n)
{
	root=new Node();
	root->fa=NULL;
	root->lc=new Node();
	root->rc=NULL;
	root->sz=2;
	root->x=n+1;
	root->lc->fa=root;
	root->lc->lc=root->lc->rc=NULL;
	root->lc->sz=1;
	root->lc->x=0;
}
void rev(int l,int r)
{
	int y=find_by_rnk_and_splay(l);
	int x=find_by_rnk_and_splay(r+2,root);
	if(root->rc&&root->rc->lc)root->rc->lc->tag^=1;
}
int n;
void dfs(Node *x)
{
	if(x==NULL)return;
	pushdown(x);
	dfs(x->lc);
	if(x->x&&x->x<=n)printf("%d ",x->x);
	dfs(x->rc);
}
int a[80005];
void A(Node *x=root)
{
	if(x==NULL)return;
	A(x->lc);
	A(x->rc);
	if(x->x>0&&x->x<80001)x->x=a[x->x],o[x->x]=x;
}
inline char gc()
{
	char c=getchar();
	while(c<'A'||c>'Z')c=getchar();
	return c;
}
Node* A(Node *x,int v)
{
	/*if(x==NULL)return x;
	pushdown(x);
	if(x->x==v)return x;
	Node *t=A(x->lc,v);
	if(t)return t;
	return A(x->rc,v);*/
	return o[v];
}
int main()
{
	n=read();build(n);
	int m=read();
	for(int i=1;i<=n;i++)ins(root,i,NULL);
	for(int i=1;i<=n;i++)a[i]=read();
	A();
	while(m--)
	{
		int op=gc();
		if(op=='Q')
		{
			printf("%d\n",find_by_rnk_and_splay(read()+1));
		}
		else if(op=='I')
		{
			int x=read(),y=read();
			splay(A(root,x));
			x=root->lc->sz;
			if(y==1)rev(x,x+y);
			else if(y==-1)rev(x-1,x);
		}
		else if(op=='B')
		{
			int x=read();
			splay(A(root,x));
			x=root->lc->sz;
			rev(x+1,n);
			rev(x,n);
		}
		else if(op=='A')
		{
			int x=read();
			Node *i=A(root,x);
			splay(i);
			printf("%d\n",root->lc->sz-1);
		}
		else
		{
			int x=read();
			splay(A(root,x));
			x=root->lc->sz;
			rev(1,x);rev(2,x);
		}
		//dfs(root);puts("");
	}
	return 0;
}
2021/2/28 20:04
加载中...