求助,splay全wa了
查看原帖
求助,splay全wa了
104324
abruce楼主2020/5/8 11:42
#include<bits/stdc++.h>
using namespace std;
struct tree {
	int sum,f,num,siz,ln,rn,bj;
	int ch[2];
} t[100001];
int n,m,news,root,cnt,p,q;
void greaterx(int rot ,int x);
void lessx(int rot,int x);
void pushup(int x) {
	t[x].siz=t[t[x].ch[0]].siz+t[t[x].ch[1]].siz+t[x].num;
	return;
}
void rotate(int x,int &rot) {
	int y=t[x].f,z=t[y].f,L=(t[y].ch[0]!=x),R=L^1;
	if(y==rot) {
		rot=x;
	} else {
		t[z].ch[t[z].ch[0]!=y]=x;
	}
	t[x].f=z;
	t[y].f=x;
	t[t[x].ch[R]].f=y;
	t[y].ch[L]=t[x].ch[R];
	t[x].ch[R]=y;
	pushup(y);
	pushup(x);
}
void splay(int x,int &rot) {
	while(x!=rot) {
		int y=t[x].f,z=t[y].f;
		if(y!=rot) {
			if(t[y].ch[0]==x^t[z].ch[0]==y) {
				rotate(x,rot);
			} else {
				rotate(y,rot);
			}
		}
		rotate(x,rot);
	}
}
int getfront() {
	int x=t[root].ch[0];
	while(t[x].ch[1]) {
		x=t[x].ch[1];
	}
	return x;
}
int getback() {
	int x=t[root].ch[1];
	while(t[x].ch[0]) {
		x=t[x].ch[0];
	}
	return x;
}
void insert(int rot,int x) {
	if(t[rot].sum>x&&t[rot].ch[0]) {
		insert(t[rot].ch[0],x);
	} else if(t[rot].sum<x&&t[rot].ch[1]) {
		insert(t[rot].ch[1],x);
	} else if(t[rot].sum==x) {
		t[rot].num++;
		t[rot].siz++;
		news=rot;
	} else {
		t[rot].ch[t[rot].sum<x]=++cnt;
		t[cnt].f=rot;
		t[cnt].siz=1;
		t[cnt].num=1;
		t[cnt].sum=x;
		news=cnt;
	}
	pushup(rot);
}
int findk(int rot,int k) {
	int lsiz=t[t[rot].ch[0]].siz;
	if(!rot)return 0;
	if(lsiz+t[rot].num>=k&&lsiz<k) {
		return rot;
	} else if(lsiz>=k) {
		return findk(t[rot].ch[0],k);
	} else {
		return findk(t[rot].ch[1],k-lsiz-t[rot].num);
	}
}
int build(int l,int r,int f) {
	if(l>r)return 0;
	int mid=(l+r)/2;
	int now=++cnt;
	t[now].f=f;
	if(mid==1)t[now].sum=INT_MIN;
	else if(mid==n+2)t[now].sum=INT_MAX;
	else t[now].sum=mid-1;
	t[now].num=t[now].siz=1;
	t[now].ch[0]=build(l,mid-1,now);
	t[now].ch[1]=build(mid+1,r,now);
	pushup(now);
	return now;
}
void pushdown(int x) {
	if(t[x].bj) {
		t[t[x].ch[0]].bj^=1;
		t[t[x].ch[1]].bj^=1;
		swap(t[x].ch[0],t[x].ch[1]);
		t[x].bj=0;
	}
}
void dfscout(int x) {
	pushdown(x);
	if(t[x].ch[0])
		dfscout(t[x].ch[0]);
	if(t[x].sum!=INT_MIN&&t[x].sum!=INT_MAX)
	    printf("%d ",t[x].sum);
	if(t[x].ch[1])
		dfscout(t[x].ch[1]);
	return;
}
int main() {
	int x,y;
	scanf("%d%d",&n,&m);
	root=build(1,n+2,0);
	for(register int i=1; i<=m; i++) {
		scanf("%d%d",&y,&x);
		if(y>=x) {
			continue;
		}
		splay(findk(root,y),root);
		splay(findk(root,x+2),t[root].ch[1]);
		t[t[t[root].ch[1]].ch[0]].bj^=1;
	}
	dfscout(root);
	return 0;
}
2020/5/8 11:42
加载中...