Luogu2343 宝石管理系统(平衡树)

平衡树维护总第K大;插入

#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("\n\n----------\n\n")
#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 = 100007;

struct Treap{
    int ch[2], fa, tot, val, siz;
}t[N << 2];
int treeIndex, root;
inline void Pushup(int rt){
    t[rt].siz = t[t[rt].ch[0]].siz + t[t[rt].ch[1]].siz + t[rt].tot;
}
inline int Ident(int x){
    return t[t[x].fa].ch[1] == x;
}
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){
        ++t[u].tot; 
    }
    else{
        u = ++treeIndex;
        t[u].val = x;
        t[u].ch[0] = t[u].ch[1] = 0;
        t[u].tot = t[u].siz = 1;
        t[u].fa = fa;
        if(fa) t[fa].ch[x > t[fa].val] = u;
    }
    Splay(u, 0);
}
inline int Kth(int x){
    int u = root;
    if(t[u].siz < x) return 0;
    while(1){
        int v = t[u].ch[1];
        if(t[v].siz + t[u].tot < x){
            x -= t[v].siz + t[u].tot;
            u = t[u].ch[0];
        }
        else if(t[v].siz >= x){
            u = v;
        }
        else{
            return t[u].val;
        }
    }
}

int main(){
//FileOpen();
    int n, m;
    io >> n >> m;
    Insert(2147483647);
    Insert(-2147483647);
    R(i,1,n){
        int x;
        io >> x;
        Insert(x);
    }
    
    while(m--){
        int opt, x;
        io >> opt >> x;
        if(opt == 1){
            printf("%d\n", Kth(x + 1));
        }
        else{
            Insert(x);
        }
    }
    
    return 0;
}

转载于:https://www.cnblogs.com/bingoyes/p/11264088.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值