首页 > 其他分享 >splay板子

splay板子

时间:2022-11-29 21:37:01浏览次数:40  
标签:ch return int 板子 splay fa key query

#include <bits/stdc++.h>

using namespace std;
const int maxn = 100000+5;
struct Splay{
    int ch[maxn][0],ch[maxn][1],fa[maxn],siz[maxn],key[maxn],tot;
    void init(int t, int val = 0, int par = 0){
        ch[t][0] = ch[t][1] = 0; key[t] = val; fa[t] = par; key[t] = val;
    }
    void up(int t){
        siz[t] = siz[ch[t][1]] + siz[ch[t][0]];
    }
    void init(){
        init(0, 0, 0);
        tot = root = 0;
    }
    int find(int t = root, int x){
        if(!t)return 0;
        if(x == key[t]){
            splay(t, 0);
            return t;
        }
        if(x < key[t])return find(ch[t][0], x);
        else return find(ch[t][1], x);
    }
    void rotate(int x, int d){
        int y = fa[x];
        ch[y][d^1] = ch[x][d];
        if(ch[x][d])fa[ch[x][d]] = y;
        fa[x] = fa[y];
        if(fa[y]){
            if(y == ch[fa[y]][0])ch[fa[y]][0] = x;
            else ch[fa[y]][1]] = x;
        }    
        ch[x][d] = y;
        up(x), up(y);
    }
    void splay(int x, int targrt){
        
        while(x != targrt){
            int y = fa[x];
            if(x == ch[y][0]){
                if(targrt && y == ch[fa[y]][0])
                    rotate(y, 1);
                rotate(x, 1);
            }
            else {
                if(targrt && y == ch[fa[y]][1])
                    rotate(y, 0);
                rotate(x, 0);
            }
        }
        if(!targrt)root = x;
    }
    void insert(int t = root, int x, int par = 0){
        if(!t){
            t = ++tot;    
            init(t, x, par);
            splay(t, 0);
        }
        else {
            int cur = t;
            if(x < key[t])insert(ch[t][0], x, cur);
            else insert(ch[t][1], x, cur);
        }
    }
    
    int rank(int t = root,int x){
        if(!t)return 0;
        if(x == key[t])return 1;
        if(x < key[t])return query(ch[t][0], x);
        return 1+siz[ch[t][0]]+query(ch[t][1], x);
    }
    int query(int t = root, int x){
        if(!x)return t;
        if(x == siz[ch[t][0] + 1)return t;
        if(x > siz[ch[t][0]] + 1)
            return query(ch[t][1], x - 1 - siz[ch[t][0]]);
        return query(ch[t][0], x);
    }
    int Query(int x){
        printf("%d\n",val[query(x)]);
    }
    void erase(int x){
        int k = rank(x), lnd = query(k-1), rnd = query(k+1);
        splay(lnd, 0);splay(rnd, lnd);
        ch[rnd][0] = 0;
        up(rnd), up(lnd);
    }
   
    int getpre(int t = root, int x){
        if()
        
        
    }
    int getscc(int t = root, int x){
        if(!t)return x;
        if(t < key)return getscc(ch[t][1], x);
        if(t == key[t])return key[ch[t][1]];
        return key[ch[t][0] >= x ? getscc(ch[t][0], x) : key[t];
    }
}Tr;
int main()
{
    int n;
    scanf("%d",&n);
    while(n--){
        int opt,x;
        scanf("%d%d",&opt,&x);
        switch(opt){
            case 1:Tr.insert(x);break;
            case 2:Tr.erase(x);break;
            case 3:Tr.rank(x);break;
            case 4:Tr.Query(x);break;
            case 5:Tr.getpre(x);break;
            case 6:Tr.getscc(x);break;
        }
    }
    return 0;
}


/*
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
*/

 

标签:ch,return,int,板子,splay,fa,key,query
From: https://www.cnblogs.com/EdSheeran/p/8678941.html

相关文章

  • lca 板子
    这题#10130. 「一本通4.4例1」点的距离求树上两点的距离 #include<bits/stdc++.h>usingnamespacestd;constintN=1e6+2,M=N;intnxt[M],hd[N],all,go[M......
  • Patch Available for Cut or Copy displaying “insuf
    you'reexperiencingfrequent“insufficientmemory”errorswhendoingsmallcutorcopyoperationsinVisualStudio2010RTM,please​​downloadthispatch​......
  • 割点板子
    一些直白的理解,和标准定义有差别,但也足够了 点双连通:一个图任意去掉一个点后仍然联通;   边双连通同理割点:去掉某个点后,图不连通     割边同理 求割点(l......
  • 高精度板子
     #include<bits/stdc++.h>usingnamespacestd;intcompare(stringstr1,stringstr2){if(str1.length()>str2.length())return1;elseif(str1.length(......
  • 一些板子
    离散化://离散化,可以处理一些跨越区间比较大的时候的位置关系,空间更紧凑intn,m;inta[N],b[N],c[N];intcnt=0;//lower_bound第一个大于等于x的数//upper_bound......
  • CSS中的 display:inline-block
    display:inline-block与display:inline相比,主要区别在于display:inline-block允许在元素上设置宽度和高度。同样,如果设置了display:inline-block,将保留上下外......
  • CSP-J/S & NOIP 常用板子大全 !
    HNCSP-J/S2022RP++!序号算法①SPFA②并查集③最小生成树④拓扑排序⑤堆⑥字典树N懒得加了1.SPFA题目链接题目描述输入......
  • [XState + React] using @xstate/inspect to display state machine char in webapp
    import"./styles.css";importReactfrom"react";importReactDOMfrom"react-dom";import{createMachine,assign}from"xstate";import{useMachine}from......
  • 快速幂(板子)
    先讨论无需取模的当b为偶数时:ab=a(b/2)*2=(a2)b/2当b为奇数时:ab=a*ab-1=a*(a2)(b-1)/2如 28=(22)4     27=2*(22)3所以,我们可以如此迭代下......
  • 表格table中ta的display属性在不同浏览器中展示的不同
    今天发现表格table中ta的display属性在不同浏览器中展示的不同,特别是block属性。<table><tr><tdstyle="display:block">111111111111111111111111111111<td><tdstyle="dis......