BZOJ - 3224 Tyvj 1728 普通平衡树 splay

splay的一道模板题

 

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
// const int mod = 10007;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}


/*-----------------------showtime----------------------*/
const int maxn = 100000 + 6;

struct item {int val, id;}; /// value
bool operator < (item A, item B) {
    if(A.val != B.val) return A.val < B.val;
    return A.id < B.id;
}

namespace splay {
    item key[maxn]; int fa[maxn], ch[maxn][2], siz[maxn];
    int root, ncnt;
    int dir(int o) {
        return ch[fa[o]][0]==o ? 0 : 1;
    }
    void maintain(int o) {
        if(o) siz[o] = 1 + siz[ch[o][0]] + siz[ch[o][1]];
    }
    void link(int f, int s, int d) {
        if(f) ch[f][d] = s;
        if(s) fa[s] = f;
    }
    void rotate(int o) {
        int f = fa[o], g = fa[f], d = dir(o), s = ch[o][d^1]; /// about s !!!
        link(g, o, dir(f));
        link(o, f, d^1);
        link(f, s, d); /// g - o   *   o - f   *   f - ch[o][d^1]
        maintain(f); maintain(o);
    }
    void splay(int x, int tar) {
        while(fa[x] != tar) {
            if(fa[fa[x]] != tar) {
                if(dir(x) == dir(fa[x])) rotate(fa[x]);
                else                     rotate(x);
            }
            rotate(x);
        }
        if(!tar) root = x;
    }
    int rnk(item k) {
        int o = root, ans = 0, last;
        while(o) {
            if(key[o] < k)
                ans += siz[ch[o][0]] + 1, o = ch[last = o][1];
            else o = ch[last = o][0];
        }
        splay(last, 0); return ans + 1;
    }
    item kth(int k) {
        int o = root;
        while(o) {
            if(siz[ch[o][0]] + 1 == k) {
                splay(o, 0); return key[o];
            }
            if(k < siz[ch[o][0]] + 1) o = ch[o][0];
            else {
                k -= siz[ch[o][0]] + 1;
                o = ch[o][1];
            }
        }
        return k<=0 ? (item){-2147483647, 0} : (item){2147483647, 0};
    }
    void merge(int x, int y) {
        if(x==0 || y==0) {root = x+y; return;}
        while(ch[y][0]) {
            siz[y] += siz[x]; y = ch[y][0];
        }
        siz[y] += siz[x]; link(y, x, 0); splay(x, 0);
    }
    void insert(item k) {
        key[++ ncnt] = k; siz[ncnt] = 1; ch[ncnt][0] = ch[ncnt][1] = 0;
        if(root == 0) {root = ncnt; return;}
        int o = root, last;
        while(o) {
            ++ siz[last = o];
            if(k < key[o]) {
                o = ch[o][0];
            }else{
                o = ch[o][1];
            }
        }
        link(last, ncnt, k<key[last] ? 0 : 1);
        splay(ncnt, 0);
    }
    void del(int x) { 
        splay(x, 0);
        fa[ch[x][0]] = fa[ch[x][1]] = 0;
        merge(ch[x][0], ch[x][1]);
    }
    int find(int v) {
        int o = root;
        while(o) {
            if(key[o].val == v) {
                splay(o, 0); return o;
            }
            o = ch[o][v<key[o].val ? 0 : 1];
        }
    }
}

int main() {
    int n; scanf("%d", &n);
    for(int tt = 1; tt <= n; tt ++) {
        int opt, x; scanf("%d%d", &opt, &x);
        if(opt == 1) {
            splay::insert((item){x, tt});
        }else if(opt == 2) {
            splay::del(splay::find(x));
        }else if(opt == 3) {
            printf("%d\n", splay::rnk((item){x, 0}));
        }else if(opt == 4) {
            printf("%d\n", splay::kth(x).val);
        }else if(opt == 5) {
            int rnk = splay::rnk((item){x, 0}) -1;
            printf("%d\n", splay::kth(rnk).val);
        }else if(opt == 6) {
            int rnk = splay::rnk((item){x, 0x7f7f7f7f});
            printf("%d\n", splay::kth(rnk).val);
        }
    }
    return 0;
}
View Code

 

posted @ 2018-10-02 16:51  ckxkexing  阅读(154)  评论(0编辑  收藏  举报