萌新初学OI 1个月,求助
查看原帖
萌新初学OI 1个月,求助
26800
Sshenyyyu楼主2019/3/24 16:58

RT,怎么也找不到WA的原因

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <algorithm>
using namespace std;
const int N=100005;
int n,i,k,ch[N][2],ch2[N][2],head,a[N],lazy[N],s[N],fa[N],fa2[N],c,key,root;
int fr,ba;
void pushdown(int x)
{
	if(ch[x][0]!=0)
		s[ch[x][0]]+=lazy[x];
	if(ch[x][1]!=0)
		s[ch[x][1]]+=lazy[x];
	lazy[x]=0;
}
void rotate(int x)
{
	int y=fa[x];
	bool d=(ch[y][0]==x);
	pushdown(y);
	pushdown(x);
	ch[y][!d]=ch[x][d];
	if(ch[x][d]!=0)
		fa[ch[x][d]]=y;
	fa[x]=fa[y];
	if(fa[y]!=0)
		ch[fa[y]][ch[fa[y]][1]==y]=x;
	fa[y]=x;
	ch[x][d]=y;
}
void splay(int x)
{
	int y=fa[x];
	for(;y!=0;rotate(x),y=fa[x])
		if(fa[y]!=0&&(ch[fa[y]][0]==y)==(ch[y][0]==x))
			rotate(y);
	head=x;
}
void Insert(int x,int f,int u)
{
	if(x==0)
	{
		if(a[u]<a[f])
			ch[f][0]=u;
		else
			ch[f][1]=u;
		fa[u]=f;
		splay(u);
		return;
	}
	if(a[x]>a[u])
		Insert(ch[x][0],x,u);
	else
		Insert(ch[x][1],x,u);
}
void Front(int x,int p,int &mnn)
{
	if(x==0)
		return;
	if(a[x]>=p)
		Front(ch[x][0],p,mnn);
	else
	{
		mnn=x;
		Front(ch[x][1],p,mnn);
	}
}
void Back(int x,int p,int &mnn)
{
	if(x==0)
		return;
	if(a[x]<=p)
		Back(ch[x][1],p,mnn);
	else
	{
		mnn=x;
		Back(ch[x][0],p,mnn);
	}
}
void get_min()
{
	int x=head;
	while(ch[x][0]!=0)
		x=ch[x][0];
	splay(x);
}
void get_max()
{
	int x=head;
	while(ch[x][1]!=0)
		x=ch[x][1];
	splay(x);
}
int main(){
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&c);
		if(c==1)
		{
			scanf("%d",&key);
			a[++k]=key;
			if(head==0)
			{
				head=k;
				root=k;
				s[k]=1;
			}
			else
			{
				Insert(head,0,k);
				fr=-1;
				Front(head,key,fr);
				ba=-1;
				Back(head,key,ba);
				if(fr!=-1&&ch2[fr][1]==0)
				{
					ch2[fr][1]=k;
					fa2[k]=fr;
				}
				if(ba!=-1&&ch2[ba][0]==0)
				{
					ch2[ba][0]=k;
					fa2[k]=ba;
				}
				s[k]=s[fa2[k]]+1;
			}
			printf("%d\n",s[k]);
		}
		if(c==2)
		{
			get_min();
			int x=fa2[head];
			printf("%d\n",s[head]);
			if(x!=0)
			{
				int t=head;
				ch2[x][0]=ch2[t][1];
				if(ch2[t][1]!=0)
					fa2[ch2[t][1]]=x;
				fa2[root]=t;
				ch2[t][1]=root;
				root=t;
				splay(x);
				s[t]=1;
				s[x]++;
				if(ch[x][1]!=0)
				{
					lazy[ch[x][1]]++;
					s[ch[x][1]]++;
				}
			}
		}
		if(c==3)
		{
			get_max();
			int x=fa2[head];
			printf("%d\n",s[head]);
			if(x!=0)
			{
				int t=head;
				ch2[x][1]=ch2[t][0];
				if(ch2[t][0]!=0)
					fa2[ch2[t][0]]=x;
				ch2[t][0]=root;
				fa2[root]=t;
				root=t;
				splay(x);
				s[t]=1;
				s[x]++;
				if(ch[x][0]!=0)
				{
					lazy[ch[x][0]]++;
					s[ch[x][0]]++;
				}
			}
		}
		if(c==4)
		{
			get_min();
			int x=fa2[head];
			printf("%d\n",s[head]);
			if(x==0)
			{
				root=ch2[head][1];
				fa2[root]=0;
				head=ch[head][1];
				lazy[head]--;
			}
			else
			{
				int t=head;
				ch2[x][0]=ch2[t][1];
				if(ch2[t][1]!=0)
					fa2[ch2[t][1]]=x;
				head=ch[head][1];
				fa[head]=0;
				splay(x);
				if(ch[x][0]!=0)
				{
					lazy[ch[x][0]]++;
					s[ch[x][0]]++;
				}
			}
		}
		if(c==5)
		{
			get_max();
			int x=fa2[head];
			printf("%d\n",s[head]);
			if(x==0)
			{
				root=ch2[head][1];
				fa2[root]=0;
				head=ch[head][1];
				lazy[head]--;
			}
			else
			{
				int t=head;
				ch2[x][1]=ch[t][0];
				if(ch2[t][0]!=0)
					fa2[ch2[t][0]]=x;
				head=ch[head][0];
				fa[head]=0;
				splay(x);
				if(ch[x][1]!=0)
				{
					lazy[ch[x][1]]++;
					s[ch[x][1]]++;
				}
			}
		}
	}
}

2019/3/24 16:58
加载中...