模板 - 数据结构 - 无旋Treap / FHQ Treap

普通平衡树:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls(p) ch[p][0]
#define rs(p) ch[p][1]

const int MAXN = 100000 + 5;
int val[MAXN], ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root;

void Init() {
    tot = root = 0;
}

void PushUp(int p) {
    siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
}

void SplitValue(int p, int v, int &x, int &y) {
    if(!p) {
        x = y = 0;
        return;
    }
    if(v < val[p]) {
        y = p;
        SplitValue(ls(p), v, x, ls(p));
        PushUp(y);
    } else {
        x = p;
        SplitValue(rs(p), v, rs(p), y);
        PushUp(x);
    }
}

void SplitRank(int p, int rk, int &x, int &y) {
    if(!p) {
        x = y = 0;
        return;
    }
    if(rk <= siz[ls(p)]) {
        y = p;
        SplitRank(ls(p), rk, x, ls(p));
        PushUp(y);
    } else {
        x = p;
        SplitRank(rs(p), rk - siz[ls(p)] - 1, rs(p), y);
        PushUp(x);
    }
}

int Merge(int x, int y) {
    if(!x || !y)
        return x | y;
    if(rnd[x] < rnd[y]) {
        rs(x) = Merge(rs(x), y);
        PushUp(x);
        return x;
    } else {
        ls(y) = Merge(x, ls(y));
        PushUp(y);
        return y;
    }
}

int NewNode(int v) {
    ++tot;
    ch[tot][0] = ch[tot][1] = 0;
    val[tot] = v, rnd[tot] = rand();
    siz[tot] = 1;
    return tot;
}

void Insert(int &root, int v) {
    int x = 0, y = 0;
    SplitValue(root, v, x, y);
    root = Merge(Merge(x, NewNode(v)), y);
}

void Remove(int &root, int v) {
    int x = 0, y = 0, z = 0;
    SplitValue(root, v, x, z);
    SplitValue(x, v - 1, x, y);
    y = Merge(ls(y), rs(y));
    root = Merge(Merge(x, y), z);
}

int GetRank(int &root, int v) {
    int x = 0, y = 0;
    SplitValue(root, v - 1, x, y);
    int rk = siz[x] + 1;
    root = Merge(x, y);
    return rk;
}

int GetValue(int &root, int rk) {
    int x = 0, y = 0, z = 0;
    SplitRank(root, rk, x, z);
    SplitRank(x, rk - 1, x, y);
    int v = val[y];
    root = Merge(Merge(x, y), z);
    return v;
}

int GetPrev(int &root, int v) {
    int x = 0, y = 0;
    SplitValue(root, v - 1, x, y);
    int prev = GetValue(x, siz[x]);
    root = Merge(x, y);
    return prev;
}

int GetNext(int &root, int v) {
    int x = 0, y = 0;
    SplitValue(root, v, x, y);
    int next = GetValue(y, 1);
    root = Merge(x, y);
    return next;
}

int main() {
    int n;
    scanf("%d", &n);
    Init();
    for(int i = 1; i <= n; ++i) {
        int op, x;
        scanf("%d%d", &op, &x);
        switch(op) {
            case 1:
                Insert(root, x);
                break;
            case 2:
                Remove(root, x);
                break;
            case 3:
                printf("%d\n", GetRank(root, x));
                break;
            case 4:
                printf("%d\n", GetValue(root, x));
                break;
            case 5:
                printf("%d\n", GetPrev(root, x));
                break;
            case 6:
                printf("%d\n", GetNext(root, x));
                break;
        }
    }
    return 0;
}

O(n)建树与回收:

//O(n)建树,返回新树的根
int st[MAXN], stop;
char buf[MAXN];
inline int Build(int n) {
    stop = 0;
    for(int i = 0; i < n; ++i) {
        int tmp = NewNode(buf[i]), last = 0;
        while(stop && rnd[st[stop]] > rnd[tmp]) {
            last = st[stop];
            PushUp(last);
            st[stop--] = 0;
        }
        if(stop)
            rs(st[stop]) = tmp;
        ls(tmp) = last;
        st[++stop] = tmp;
    }
    while(stop)
        PushUp(st[stop--]);
    return st[1];
}

//O(n)回收整棵树
inline void UnBuild(int p) {
    if(!p)
        return;
    UnBuild(ls(p));
    UnBuild(rs(p));
    RecBin.push(p);
}

非递归查询:

int GetRank2(int p, int v) {
    int rk = 1;
    while(p) {
        if(v < val[p])
            p = ls(p);
        else if(v == val[p])
            p = ls(p);
        else {
            rk += siz[ls(p)] + 1;
            p = rs(p);
        }
    }
    return rk;
}

int GetValue2(int p, int rk) {
    while(p) {
        if(rk <= siz[ls(p)])
            p = ls(p);
        else if(rk == siz[ls(p)] + 1)
            return val[p];
        else {
            rk -= siz[ls(p)] + 1;
            p = rs(p);
        }
    }
}

int GetPrev2(int p, int v) {
    int prev;
    while(p) {
        if(v <= val[p])
            p = ls(p);
        else {
            prev = val[p];
            p = rs(p);
        }
    }
    return prev;
}

int GetNext2(int p, int v) {
    int next;
    while(p) {
        if(v < val[p]) {
            next = val[p];
            p = ls(p);
        } else
            p = rs(p);
    }
    return next;
}

无旋Treap维护序列:

操作1翻转序列[L,R],操作2查询pos位置的字符。

#define ls(p) ch[p][0]
#define rs(p) ch[p][1]

const int MAXN = 1000000 + 5;
char val[MAXN];
int ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root;
bool rev[MAXN];

void Init() {
    tot = root = 0;
}

void PushUp(int p) {
    siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
}

void PushDown(int p) {
    if(rev[p]) {
        swap(ls(p), rs(p));
        rev[ls(p)] ^= 1;
        rev[rs(p)] ^= 1;
        rev[p] = 0;
    }
}

void SplitRank(int p, int rk, int &x, int &y) {
    if(!p) {
        x = y = 0;
        return;
    }
    PushDown(p);
    if(rk <= siz[ls(p)]) {
        y = p;
        SplitRank(ls(p), rk, x, ls(p));
        PushUp(y);
    } else {
        x = p;
        SplitRank(rs(p), rk - siz[ls(p)] - 1, rs(p), y);
        PushUp(x);
    }
}

int Merge(int x, int y) {
    if(!x || !y)
        return x | y;
    if(rnd[x] < rnd[y]) {
        PushDown(x);
        rs(x) = Merge(rs(x), y);
        PushUp(x);
        return x;
    } else {
        PushDown(y);
        ls(y) = Merge(x, ls(y));
        PushUp(y);
        return y;
    }
}

int NewNode(int v) {
    ++tot;
    ch[tot][0] = ch[tot][1] = 0;
    val[tot] = v, rnd[tot] = rand();
    siz[tot] = 1;
    return tot;
}

void ReverseLR(int l, int r) {
    int x, y, z;
    SplitRank(root, l - 1, x, y);
    SplitRank(y, r + 1 - l, y, z);
    PushDown(y);
    rev[y] ^= 1;
    PushUp(y);
    root = Merge(Merge(x, y), z);
}

int ValuePos(int pos) {
    int x, y, z;
    SplitRank(root, pos - 1, x, z);
    SplitRank(z, 1, y, z);
    int ret = val[y];
    root = Merge(x, Merge(y, z));
    return ret;
}

需要搭配快读和线性建树才能卡过去。

posted @ 2020-03-21 07:47  KisekiPurin2019  阅读(115)  评论(0编辑  收藏  举报