Uva 11922 Permutation Transformer

本文深入探讨Splay树的数据结构及其应用,并通过一个具体的实战案例来讲解如何正确使用Splay树解决实际问题。强调了在遍历和修改操作中需要注意的细节,如下传标记和维护受影响节点的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

闲的没事打了打一个splay,,,,,

注意只要是遍历数据结构就要下传标记!!!!

只要是修改就要维护所有受到影响的!!!

希望以后不要再犯这种zz错误了hhh

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#define ll long long
#define maxn 100005
using namespace std;
int ch[maxn][2],f[maxn],root;
int n,m,siz[maxn],tag[maxn];
int le,ri,q[maxn],tp;

inline int get(int x){
	return ch[f[x]][1]==x;
}

inline void update(int x){
	siz[x]=1+siz[ch[x][1]]+siz[ch[x][0]];
}

inline void pushdown(int x){
	if(tag[x]){
		tag[x]=0,swap(ch[x][0],ch[x][1]);
		tag[ch[x][0]]^=1,tag[ch[x][1]]^=1;
	}
}

inline void rotate(int x){
	int fa=f[x],ffa=f[fa],tp=get(x);
	ch[fa][tp]=ch[x][tp^1],f[ch[fa][tp]]=fa;
	ch[x][tp^1]=fa,f[fa]=x;
	f[x]=ffa;
	
	if(ffa) ch[ffa][ch[ffa][1]==fa]=x;
	if(fa==root) root=x;
	
	update(fa),update(x);
}

inline void splay(int x,int d){
	for(int i=x;i;i=f[i]) q[++tp]=i;
	for(;tp;tp--) pushdown(q[tp]);
	
	for(;f[x]!=d;rotate(x))
	    if(f[f[x]]!=d) rotate(get(f[x])==get(x)?f[x]:x);
}

inline int kth(int x){
	int now=root;
	while(1){
		pushdown(now);
		
		if(x<=siz[ch[now][0]]){
			now=ch[now][0];
		}
		else{
			x-=1+siz[ch[now][0]];
			if(!x) return now;
			now=ch[now][1];
		}
	}
}

int build(int l,int r,int fa){
	if(l>r) return 0;
	int mid=l+r>>1;
	f[mid]=fa,siz[mid]=r-l+1;
	
	ch[mid][0]=build(l,mid-1,mid);
	ch[mid][1]=build(mid+1,r,mid);
	
	return mid;
}

void print(int x){
	pushdown(x);
	if(ch[x][0]) print(ch[x][0]);
	if(x>1&&x<=n) printf("%d\n",x-1);
	if(ch[x][1]) print(ch[x][1]);
}

int main(){
	scanf("%d%d",&n,&m);
	root=build(1,n+2,0);
	
	while(m--){
		scanf("%d%d",&le,&ri);
		int aa=kth(le),bb=kth(ri+2);
		int sz=ri-le+1,rot;
		splay(aa,0),splay(bb,aa);
		rot=ch[ch[root][1]][0];
		
		f[rot]=ch[ch[root][1]][0]=0;
		update(ch[root][1]),update(root);
		
		tag[rot]^=1;
		
		aa=kth(n-sz+1),splay(aa,0);
		
		ch[ch[root][1]][0]=rot,f[rot]=ch[root][1];
		update(ch[root][1]),update(root);
	}
	
	n++;
	print(root);
	
	return 0;
}

  

转载于:https://www.cnblogs.com/JYYHH/p/8448213.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值