bzoj3223文艺平衡树 原tyvj题目

文艺的splay


一个小错误查半天 

教练都说我效率低了大哭



#include <cstdio>
#include <stack>
#define lp(i,j,k) for(int i = j;i <= k;++i)
int n,m,u,v;
int root,ch[100010][2],fa[100010],sz[100010];
bool lazy[100010];
std::stack<int>s;
void build (int l,int r) {
	int mid = (l + r) / 2;
	sz[mid] = 1;
	if(l == r)
		return;
	if(l != mid) {
		build(l,mid - 1);
		fa[(l + mid - 1) / 2] = mid;
		ch[mid][0] = (l + mid - 1) / 2;
		sz[mid] += sz[ch[mid][0]];
	}
	build(mid + 1,r);
	fa[(mid + 1 + r) / 2] = mid;
	ch[mid][1] = (mid + 1 + r) / 2;
	sz[mid] += sz[ch[mid][1]];
}

void reverse (int X) {
	if(!X)
		return;
	int t = ch[X][1];
	ch[X][1] = ch[X][0];
	ch[X][0] = t;
	lazy[X] ^= 1;
}

void push_down (int X) {
	if(lazy[X]) {
		if(ch[X][0])
			reverse(ch[X][0]);
		if(ch[X][1])
			reverse(ch[X][1]);
		lazy[X] = 0;
	}
}

int rank (int goal,int X,int ran) {
	push_down(X);
	ran += sz[ch[X][1]];
	if(ran == goal)
		return X;
	if(ran > goal)
		return rank(goal,ch[X][1],ran - sz[ch[X][1]]);
	return rank(goal,ch[X][0],ran + 1);
}

void rot (int X,int d) {
	int t = sz[X];
	sz[X] = sz[fa[X]];
	sz[fa[X]] -= t;
	sz[fa[X]] += sz[ch[X][d]];
	ch[fa[X]][d ^ 1] = ch[X][d];
	if(ch[X][d])
		fa[ch[X][d]] = fa[X];
	ch[X][d] = fa[X];
	t = fa[fa[X]];
	fa[fa[X]] = X;
	fa[X] = t;
	if(!fa[X])
		return;
	if(ch[fa[X]][1] == ch[X][d])
		ch[fa[X]][1] = X;
	else ch[fa[X]][0] = X;
}

void all_push_down (int X) {
	if(!X)
		return;
	push_down(X);
	all_push_down(ch[X][0]);
	all_push_down(ch[X][1]);
}

void splay (int X) {
	while(fa[X]) {
		s.push(X);
		X = fa[X];
	}
	while(!s.empty()) {
		X = s.top();
		push_down(X);
		s.pop();
	}
	while(fa[X]) {
		if(fa[fa[X]]) {
			int d = ch[fa[fa[X]]][0] == fa[X];
			if(ch[fa[X]][d] == X) {
				rot(X,d ^ 1);
				rot(X,d);
			}
			else {
				rot(X,d);
				rot(X,d);
			}
		}
		else {
			if(ch[fa[X]][0] == X)
				rot(X,1);
			else rot(X,0);
		}
	}
	root = X;
}
int main () {
	scanf("%d%d",&n,&m);
	build(1,n + 2);
	root = (n + 3) / 2;
	lp(i,1,m) {
		scanf("%d%d",&u,&v);
		int d1 = rank(n + 3 - u,root,1);
		int d2 = rank(n + 1 - v,root,1);
		splay(d1);
		splay(d2);
		reverse(ch[d1][1]);
	}
	int t = root;
	all_push_down(root);
	while(ch[t][1] + ch[t][0]) {
		while(ch[t][0]) {
			rot(ch[t][0],1);
			if(root == t)
				root = fa[t];
			t = fa[t];
		}
		t = ch[t][1];
	}
	t = ch[root][1];
	lp(i,1,n) {
		printf("%d ",t - 1);
		t = ch[t][1];
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值