• Note -「模板」Treap


    // Treap
    
    const int MAXN = 1e5 + 5;
    
    struct Treap_Tree {
    #define INF 0x3f3f3f3f
    #define mod 998244353
        struct Treap_Node {
            int son[2], val, dat, cnt, size;
    #define lson tr[p].son[0]
    #define rson tr[p].son[1]
            Treap_Node() {}
            Treap_Node(int Val, int Dat, int Cnt, int Size) {
                val = Val;
                dat = Dat;
                cnt = Cnt;
                size = Size;
            }
        } tr[MAXN];
    
        int tot, root;
    
        int Get(int val) {
            tr[++tot] = Treap_Node(val, rand() % mod, 1, 1);
            tr[tot].son[0] = 0;
            tr[tot].son[1] = 0;
            return tot;
        }
    
        void Update(int p) { tr[p].size = tr[p].cnt + tr[lson].size + tr[rson].size; }
    
        int Get_Rank(int p, int val) {
            if (!p)
                return 1;
            if (val == tr[p].val)
                return tr[lson].size + 1;
            if (val < tr[p].val)
                return Get_Rank(lson, val);
            return tr[lson].size + tr[p].cnt + Get_Rank(rson, val);
        }
    
        int Get_Val(int p, int r) {
            if (!p)
                return INF;
            if (tr[lson].size >= r)
                return Get_Val(lson, r);
            if (tr[lson].size + tr[p].cnt >= r)
                return tr[p].val;
            return Get_Val(rson, r - tr[lson].size - tr[p].cnt);
        }
    
        void Rotate(int &p, int t) {
            int q = tr[p].son[!t];
            tr[p].son[!t] = tr[q].son[t];
            tr[q].son[t] = p;
            p = q;
            Update(tr[p].son[t]);
            Update(p);
        }
    
        void Insert(int &p, int val) {
            if (!p) {
                p = Get(val);
                return;
            }
            if (val == tr[p].val) {
                tr[p].cnt++;
                Update(p);
                return;
            }
            if (val < tr[p].val) {
                Insert(lson, val);
                if (tr[p].dat < tr[lson].dat)
                    Rotate(p, 1);
            } else {
                Insert(rson, val);
                if (tr[p].dat < tr[rson].dat)
                    Rotate(p, 0);
            }
            Update(p);
        }
    
        int Get_Pre(int x) {
            int p = root, ret = -INF;
            while (p) {
                if (tr[p].val <= x) {
                    ret = tr[p].val;
                    p = rson;
                } else
                    p = lson;
            }
            return ret;
        }
    
        int Get_Next(int x) {
            int p = root, ret = INF;
            while (p) {
                if (tr[p].val >= x) {
                    ret = tr[p].val;
                    p = lson;
                } else
                    p = rson;
            }
            return ret;
        }
    
        void Delete(int &p, int val) {
            if (!p)
                return;
            if (val == tr[p].val) {
                if (tr[p].cnt > 1) {
                    tr[p].cnt--;
                    Update(p);
                    return;
                }
                if (lson || rson) {
                    if (rson == 0 || tr[lson].dat > tr[rson].dat) {
                        Rotate(p, 1);
                        Delete(rson, val);
                    } else {
                        Rotate(p, 0);
                        Delete(lson, val);
                    }
                    Update(p);
                } else
                    p = 0;
                return;
            }
            if (val < tr[p].val)
                Delete(lson, val);
            else
                Delete(rson, val);
            Update(p);
        }
    #undef lson
    #undef rson
    } tree;
    
  • 相关阅读:
    MFC中CDialog与其对话框资源的绑定 dll中资源的切换
    DirectDraw 显示 YUV
    ClipCursor与GetClipCursor 用法
    MFC消息处理流程概述 .
    HTML5 WebSocket 技术介绍
    NodepartySZ1 深圳聚会活动回顾总结[2012.01.08] CNode
    index QuickWeb文档
    Node.js Manual
    An innovative way to replace AJAX and JSONP using node.js and socket.io
    RequireJS
  • 原文地址:https://www.cnblogs.com/Chain-Forward-Star/p/15047431.html
Copyright © 2020-2023  润新知