Fhq treap P3369 【模板】普通平衡樹

傳送門:

學了Fhq Treap之後,我深深的瞭解到 Fhq Treap的牛逼,因爲上一張學了替罪羊平衡樹,碼量很大,操作繁瑣,不支持提取區間信息,雖然簡單理解,但是Fhq也很好理解呀,而且碼量不大,能快速維護一顆平衡樹,支持提取區間信息

Fhq Treap 的騷操作:

  • 分裂
  • 合併

分裂:

分裂操作其實很簡單理解,把一顆平衡樹按照插入的值爲界限,分裂成兩棵樹,記錄兩個樹的根節點即可

合併:

合併的規則根據隨機索引去合併二叉堆即可,這樣這棵樹就很難因爲出題人造的數據而退化爲一條鏈,除非運氣十分的不好

參考代碼:

#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <map>
#include <queue>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <set>
#include <random>
using namespace std;
const int inf = (1 << 30);
typedef long long ll;
const int maxn = 1e6 + 5;
vector<int> vec;
struct node
{
    int ls, rs, w, key;
    int sizx;
} tr[maxn * 32];
int cnt, rt;
int newnode(int w)
{
    tr[++cnt].w = w, tr[cnt].key = rand();
    tr[cnt].sizx = 1;
    return cnt;
}
void pushup(int now)
{
    tr[now].sizx = tr[tr[now].ls].sizx + tr[tr[now].rs].sizx + 1;
}
void split(int now, int w, int &x, int &y)
{
    if (!now)
        x = y = 0;
    else
    {
        if (tr[now].w <= w)
        {
            x = now, split(tr[now].rs, w, tr[now].rs, y);
        }
        else
        {
            y = now, split(tr[now].ls, w, x, tr[now].ls);
        }
        pushup(now);
    }
}
int merge(int x, int y)
{
    if (!x || !y)
        return x + y;
    if (tr[x].key > tr[y].key)
    {
        tr[x].rs = merge(tr[x].rs, y);
        pushup(x);
        return x;
    }
    else
    {
        tr[y].ls = merge(x, tr[y].ls);
        pushup(y);
        return y;
    }
}
int x, y, z;
void inser(int w)
{
    split(rt, w, x, y);
    rt = merge(merge(x, newnode(w)), y);
}
void dele(int w)
{
    split(rt, w, x, z);
    split(x, w - 1, x, y);
    y = merge(tr[y].ls, tr[y].rs);
    rt = merge(merge(x, y), z);
}
void getrank(int w)
{
    split(rt, w - 1, x, y);
    cout << tr[x].sizx + 1 << endl;
    rt = merge(x, y);
}
void getnum(int rank)
{
    int now = rt;
    while (now)
    {
        if (tr[tr[now].ls].sizx + 1 == rank)
            break;
        else if (tr[tr[now].ls].sizx >= rank)
            now = tr[now].ls;
        else
        {
            rank -= tr[tr[now].ls].sizx + 1;
            now = tr[now].rs;
        }
    }
    cout << tr[now].w << endl;
}
void pre(int w)
{
    split(rt, w - 1, x, y);
    int now = x;
    while (tr[now].rs)
        now = tr[now].rs;
    cout << tr[now].w << endl;
    rt = merge(x, y);
}
void nxt(int w)
{
    split(rt, w, x, y);
    int now = y;
    while (tr[now].ls)
        now = tr[now].ls;
    cout << tr[now].w << endl;
    rt = merge(x, y);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    while (n--)
    {
        int op, x;
        cin >> op >> x;
        switch (op)
        {
        case 1:
            inser(x);
            break;
        case 2:
            dele(x);
            break;
        case 3:
            getrank(x);
            break;
        case 4:
            getnum(x);
            break;
        case 5:
            pre(x);
            break;
        case 6:
            nxt(x);
            break;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章