【UVA】11922 Permutation Transformer 伸展树

题目分析:第一道splay!!!调了一天了终于调好了。。T U T

区间剪切以及区间翻转,区间翻转就和线段树一样用一个标记来延迟。

伸展树尤其要注意pushdown的合适时机。今天是因为伸展操作写搓了,可能在操作中对null元素进行了操作导致出错。。。。。唉,还是太嫩了,不过伸展树蛮好玩的啊~


处女作一份献上~


代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define CLR( a , x ) memset ( a , x , sizeof a )

const int MAXN = 150005 ;

struct Node {
	Node *ch[2] , *fa ;
	int s , v ;
	int flip ;
	int cmp ( int x ) {
		return x == v ? -1 : ( x < v ? 0 : 1 ) ;
	}
	void maintain () {
		s = ch[0] -> s + ch[1] -> s + 1 ;
	}
	void pushdown () {
		if ( !flip ) return ;
		swap ( ch[0] , ch[1] ) ;
		ch[0] -> flip ^= 1 ;
		ch[1] -> flip ^= 1 ;
		flip = 0 ;
	}
} node[MAXN] , *root , *null ;

int n , m ;

void init () {
	null = new Node ;
	null -> v = null -> flip = null -> s = 0 ;
	null -> fa = null -> ch[0] = null -> ch[1] = NULL ;
	Node *p = node ;
	root = null ;
	FOR ( i , 0 , n ) {
		p -> v = i ;
		p -> flip = 0 ;
		p -> ch[0] = root ;
		p -> ch[1] = null ;
		p -> fa = null ;
		p -> maintain () ;
		root -> fa = p ;
		root = p ++ ;
	}
}

void rotate ( Node *&o , int d ) {
	Node *p = o -> fa ;
	p -> pushdown () ;
	o -> pushdown () ;
	p -> ch[d ^ 1] = o -> ch[d] ;
	if ( o -> ch[d] != null ) o -> ch[d] -> fa = p ;
	o -> fa = p -> fa ;
	if ( p != root ) {
		if ( p == p -> fa -> ch[0] ) p -> fa -> ch[0] = o ;
		else p -> fa -> ch[1] = o ;
	}
	o -> ch[d] = p ;
	p -> fa = o ;
	p -> maintain () ;
	if ( p == root ) root = o ;
}

void splay ( Node *&o ) {
	while ( o -> fa != null ) {
		Node *p = o -> fa ;
		Node *gp = p -> fa ;
		if ( gp == null ) {
			if ( o == p -> ch[0] ) rotate ( o , 1 ) ;
			else rotate ( o , 0 ) ;
		} else {
			if ( p == gp -> ch[0] ) {
				if ( o == p -> ch[0] ) rotate ( p , 1 ) , rotate ( o , 1 ) ;
				else rotate ( o , 0 ) , rotate ( o , 1 ) ;
			} else {
				if ( o == p -> ch[1] ) rotate ( p , 0 ) , rotate ( o , 0 ) ;
				else rotate ( o , 1 ) , rotate ( o , 0 ) ;
			}
		}
	}
	o -> maintain () ;
}

void select ( Node *&o , int k ) {
	while ( o != null ) {
		o -> pushdown () ;
		int s = o -> ch[0] -> s ;
		if ( k == s + 1 ) break ;
		if ( k <= s ) o = o -> ch[0] ;
		else {
			k -= s + 1 ;
			o = o -> ch[1] ;
		}
	}
	splay ( o ) ;
}

Node *merge ( Node *left , Node *right ) {
	select ( left , left -> s ) ;
	left -> ch[1] = right ;
	right -> fa = left ;
	left -> maintain () ;
	return left ;
}

void split ( Node *o , int k , Node *&left , Node *&right ) {
	select ( o , k ) ;
	right = o -> ch[1] ;
	right -> fa = null ;
	left = o ;
	left -> ch[1] = null ;
	left -> maintain () ;
}

void print ( Node *o ) {
	o -> pushdown () ;
	if ( o -> ch[0] != null ) print ( o -> ch[0] ) ;
	if ( o -> v ) printf ( "%d\n" , o -> v ) ;
	if ( o -> ch[1] != null ) print ( o -> ch[1] ) ;
}

void solve () {
	Node *left , *right , *mid , *tmp ;
	int L , R ;
	init () ;
	while ( m -- ) {
		scanf ( "%d%d" , &L , &R ) ;
		split ( root , L , left , tmp ) ;
		split ( tmp , R - L + 1 , mid , right ) ;
		mid -> flip ^= 1 ;
		merge ( merge ( left , right ) , mid ) ;
		//print ( root ) ;
	}
	print ( root ) ;
}
		

int main () {
	while ( ~scanf ( "%d%d" , &n , &m ) )
		solve () ;
	return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值