调不出Splay板子求救
查看原帖
调不出Splay板子求救
536745
WaTleZero_pt楼主2024/12/6 23:49

rt,看不出来哪里出错了,但是样例都过不掉,盯了一个多小时都看不出来

#include<bits/stdc++.h>
using namespace std;
const int N=100005;
const int oo=INT_MAX;
int n;

struct Node{int pos,hi;};
bool cmp(Node a,Node b){return a.hi==b.hi?a.pos<b.pos:a.hi<b.hi;}
class Splay{
	public:
	int ch[N][2];
	int fa[N];
	int sz[N];
	bool rev[N];
	Node node[N];
	int root;
	bool isrchi(int x){return ch[fa[x]][1]==x;}
	void pushup(int x){sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;}
	void pushdown(int x){
		if(rev[x]){
			rev[ch[x][0]]^=1;
			rev[ch[x][1]]^=1;
			swap(ch[x][0],ch[x][1]);
			rev[x]=0;
		} 
	}
	void build(int l,int r,int f){
		if(l>r) return;
		int m=l+r>>1;
		ch[f][m>=f]=m; fa[m]=f; sz[m]=1;
		if(l==r) return;
		build(l,m-1,m);
		build(m+1,r,m);
		pushup(m);
	}
	void rotate(int x){
		int kind=isrchi(x),kind2=isrchi(fa[x]);
		int f=fa[x]; int anc=fa[f];
		pushdown(f); pushdown(x);
		ch[f][kind]=ch[x][kind^1];
		ch[x][kind^1]=f;
		fa[ch[f][kind]]=f; fa[f]=x; fa[x]=anc;
		if(anc) ch[anc][kind2]=x;
		pushup(f); pushup(x);
	}
	void splay(int x,int anc){
		for(int f;(f=fa[x])!=anc;rotate(x)){
			pushdown(fa[f]); pushdown(f); pushdown(x);
			if(fa[f]!=anc) (isrchi(f)==isrchi(x))?rotate(f):rotate(x);
		}
		if(anc==0) root=x; 
	}
	int kth(int x){
		int now=root;
		while(1){
			if(rev[now]) pushdown(now);
			if(x<=sz[ch[now][0]]&&ch[now][0])
				now=ch[now][0];
			else{
				x-=sz[ch[now][0]]+1;
				if(!x) return now;
				now=ch[now][1];
			}
		}
	}
	void reverse(int l,int r){
		int a=kth(l-1),b=kth(r+1);
		splay(a,0);
		splay(b,a);
		rev[ch[b][0]]^=1;
	}
}s;

void work(){
	scanf("%d",&n);
	if(!n) return;
	for(int i=1;i<=n+2;i++)
		s.node[i].pos=i;
	s.node[1].hi=-oo;
	s.node[n+2].hi=oo;
	for(int i=2;i<=n+1;i++)
		scanf("%d",&s.node[i].hi);
	sort(s.node+1,s.node+n+3,cmp);
	s.build(1,n+2,0);
	s.root=(n+3)>>1;
	for(int i=2;i<=n+1;i++){
		s.splay(s.node[i].pos,0);
		printf("%d ",s.sz[s.ch[s.root][0]]);
		s.reverse(i,s.sz[s.ch[s.root][0]]+1);
	}
} 
int main(){
	work();
	return 0;
}
2024/12/6 23:49
加载中...