zoukankan      html  css  js  c++  java
  • Uva 11922 Permutation Transformer

    闲的没事打了打一个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
    ",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;
    }
    

      

  • 相关阅读:
    maven中文乱码问题——打包错误
    maven中文乱码问题——编译错误
    Vue.js 十五分钟入门
    Vue+SpringBoot+Mybatis的简单员工管理项目
    vue.js+boostrap最佳实践
    Chrome教程(二)使用ChromeDevTools命令菜单运行命令
    Chrome教程(一)NetWork面板分析网络请求
    Vue.js——vue-router 60分钟快速入门
    Vue.js——60分钟组件快速入门(下篇)
    Vue.js——60分钟组件快速入门(上篇)
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8448213.html
Copyright © 2011-2022 走看看