zoukankan      html  css  js  c++  java
  • Luogu3850 [TJOI2007]书架 (平衡树)

    将要插入位置前和前前splay,再连在右子树上。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Abs(a) ((a) < 0 ? -(a) : (a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    
    #define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt","r",stdin);
    
    #else
    
    #define D_e_Line ;
    #define D_e(x)  ;
    #define Pause() ;
    #define FileOpen() ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    const int N = 1000007;
    
    struct Tree{
    	int ch[2], fa, val, siz;
    }t[N];
    int treeIndex,root;
    inline void Pushup(int rt){
    	t[rt].siz = t[t[rt].ch[0]].siz + t[t[rt].ch[1]].siz + 1;
    }
    inline int Ident(int rt){
    	return t[t[rt].fa].ch[1] == rt;
    }
    inline void Rotate(int x){
    	int y = t[x].fa, z = t[y].fa, k = Ident(x);
    	t[z].ch[Ident(y)] = x, t[x].fa = z;
    	t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
    	t[x].ch[k ^ 1] = y, t[y].fa = x;
    	Pushup(y), Pushup(x);
    }
    inline void Splay(int x, int pos){
    	while(t[x].fa != pos){
    		int y = t[x].fa, z = t[y].fa;
    		if(z != pos){
    			Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
    		}
    		Rotate(x);
    	}
    	if(!pos) root = x;
    }
    inline void Insert(int x){
    	int u = root, fa = 0;
    	while(u && t[u].val != x){
    		fa = u;
    		u = t[u].ch[x > t[u].val];
    	}
    	if(u){
    		return;
    	}
    	else{
    		u = ++treeIndex;
    		t[u].ch[0] = t[u].ch[1] = 0;
    		t[u].val = x;
    		t[u].fa = fa;
    		t[u].siz = 1;
    		if(fa) t[fa].ch[x > t[fa].val] = u;
    	}
    	Splay(u, 0);
    }
    inline int Kth(int k){
    	int u = root;
    	while(1){
    		int v = t[u].ch[0];
    		if(t[v].siz + 1 < k){
    			k -= t[v].siz + 1;
    			u = t[u].ch[1];
    		}
    		else if(v && t[v].siz >= k){ // !
    			u = v;
    		}
    		else
    			return u; // !
    	}
    }
    
    char book[N][17];
    int main(){
    //FileOpen();
    
    	Insert(2147483647);
    	Insert(-2147483647);
    	int n;
        io >> n;
        R(i,1,n){
            scanf("%s", book[i]);
            Insert(i);
        }
        int m;
    	io >> m;
        R(i,1,m){
            scanf("%s", book[n+i]);
            int x;
            io >> x;
            int nxtNxt=Kth(x + 2);
            Splay(nxtNxt, 0);
            int nxt=Kth(x + 1);
            Splay(nxt, nxtNxt);
            int u = ++treeIndex;
            t[nxt].ch[1] = u;
            t[u].ch[0] = t[u].ch[1] = 0;
            t[u].fa = nxt;
            t[u].val = x + 2;
            t[u].siz = 1;
            Pushup(nxt), Pushup(nxtNxt);
        }
        int Ques;
        io >> Ques;
        while(Ques--){
            int x;
    		io >> x;
            printf("%s
    ", book[Kth(x + 2) - 2]);
        }
        return 0;
    }
    
  • 相关阅读:
    TSQL Challenge 1
    CTE的使用
    编号问题
    C语言结构体(sizeof长度)偏移量的简单研究
    【更新中】树的遍历
    【更新中】C语言语法汇总(仅记录遇到的坑)
    windows修改PowerShell(命令提示符)默认中文编码方式
    “人工智能”并不可怕
    学习《操作系统》收获
    人类大脑只开发了10%? I don't think so.
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11271131.html
Copyright © 2011-2022 走看看