Leafy Tree

Leafy Tree

这是一个特别优秀的平衡树,常数非常小,支持可持久化,可以维护区间,所以去学了。。

他的由叶子节点和辅助节点组成,每个非叶子节点一定有两个孩子,自己则统计了两个孩子所在子树的信息。

每个叶子节点储存了真正的信息。。和线段树很像,所有又有人叫他平衡线段树。。

贴一个加权平衡,也就是WBLT的板子

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
#define newNode(a, b, c, d) (&(*pool[cnt++] = Node(a, b, c, d)))
#define merge(a, b) newNode(a->size + b->size, b->val, a, b)
#define push_up(rt) if(rt->lf->size) rt->size = rt->lf->size + rt->rf->size, rt->val = rt->rf->val
#define ratio 4
const int N = 100005;
int n, cnt;
struct Node{
    int size, val;
    Node *lf, *rf;
    Node(){}
    Node(int size, int val, Node *lf, Node *rf): size(size), val(val), lf(lf), rf(rf){}
}*null, *pool[N<<1], t[N<<1], *root;

inline void maintain(Node *cur){
    if(cur->lf->size > cur->rf->size * ratio)
        cur->rf = merge(cur->lf->rf, cur->rf), pool[--cnt] = cur->lf, cur->lf = cur->lf->lf;
    if(cur->rf->size > cur->lf->size * ratio)
        cur->lf = merge(cur->lf, cur->rf->lf), pool[--cnt] = cur->rf, cur->rf = cur->rf->rf;
}

void insert(Node *cur, int val){
    if(cur->size == 1){
        cur->lf = newNode(1, min(val, cur->val), null, null);
        cur->rf = newNode(1, max(val, cur->val), null, null);
    }
    else insert(val > cur->lf->val ? cur->rf : cur->lf, val);
    push_up(cur), maintain(cur);
}

void erase(Node *cur, int val){
    if(cur->lf->size == 1 && cur->lf->val == val)
        pool[--cnt] = cur->lf, pool[--cnt] = cur->rf, *cur = *cur->rf;
    else if(cur->rf->size == 1 && cur->rf->val == val)
        pool[--cnt] = cur->rf, pool[--cnt] = cur->lf, *cur = *cur->lf;
    else erase(val > cur->lf->val ? cur->rf : cur->lf, val);
    push_up(cur), maintain(cur);
}

int find(Node *cur, int k){
    if(cur->size == 1) return cur->val;
    return k > cur->lf->size ? find(cur->rf, k - cur->lf->size) : find(cur->lf, k);
}

int ranks(Node *cur, int val){
    if(cur->size == 1) return 1;
    return val > cur->lf->val ? cur->lf->size + ranks(cur->rf, val) : ranks(cur->lf, val);
}

int precursor(int val){
    return find(root, ranks(root, val) - 1);
}

int successor(int val){
    return find(root, ranks(root, val + 1));
}

int main(){

    n = read();
    null = new Node(0, 0, nullptr, nullptr);
    root = new Node(1, INF, null, null);
    for(int i = 0; i < (N << 1); i ++) pool[i] = &t[i];
    while(n --){
        int opt = read(), x = read();
        if(opt == 1) insert(root, x);
        else if(opt == 2) erase(root, x);
        else if(opt == 3) printf("%d\n", ranks(root, x));
        else if(opt == 4) printf("%d\n", find(root, x));
        else if(opt == 5) printf("%d\n", precursor(x));
        else printf("%d\n", successor(x));
    }
    return 0;
}

前面说了leafytree是支持可持久化的,而且也很方便。

每次改动节点的时候开新节点连上就好了,注意要单独维护平衡,每次改动都维护一下,同样也是建新的节点,这里要把左右子树全部重开节点,因为原来的树毕竟会有一部分挂在需要的历史版本上。。

具体看代码的maintain函数吧。

#include <bits/stdc++.h>
#define INF 2147483647
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
#define newNode(a, b, c, d) (&(t[cnt++] = Node(a, b, c, d)))
#define merge(a, b) newNode(a->size + b->size, b->val, a, b)
#define push_up(rt) if(rt->lf->size) rt->size = rt->lf->size + rt->rf->size, rt->val = rt->rf->val;
#define ratio 4
const int N = 5000005;
struct Node{
    int size, val;
    Node *lf, *rf;
    Node(){}
    Node(int size, int val, Node *lf, Node *rf): size(size), val(val), lf(lf), rf(rf){}
}*null, *root[N], t[N<<2];
int n, cnt;

Node *maintain(Node *cur){
    Node *node = newNode(cur->size, cur->val, cur->lf, cur->rf);
    if(node->lf->size > node->rf->size * ratio)
        node->lf = maintain(node->lf), node->rf = maintain(node->rf), node->rf = merge(node->lf->rf, node->rf), node->lf = node->lf->lf;
    if(node->rf->size > node->lf->size * ratio)
        node->lf = maintain(node->lf), node->rf = maintain(node->rf), node->lf = merge(node->lf, node->rf->lf), node->rf = node->rf->rf;
    return node;
}

Node *insert(Node *cur, int val){
    Node *node = newNode(cur->size, cur->val, cur->lf, cur->rf);
    if(node->size == 1){
        node->lf = newNode(1, min(val, cur->val), null, null);
        node->rf = newNode(1, max(val, cur->val), null, null);
    }
    else if(val > node->lf->val){
        node->rf = insert(node->rf, val);
    }
    else{
        node->lf = insert(node->lf, val);
    }
    push_up(node);
    return node;
}

Node *erase(Node *cur, int val){
    Node *node = newNode(cur->size, cur->val, cur->lf, cur->rf);
    if(node->size == 1 && node->val != val) return node;
    if(node->lf->size == 1 && node->lf->val == val) *node = *node->rf;
    else if(node->rf->size == 1 && node->rf->val == val) *node = *node->lf;
    else if(val > node->lf->val) node->rf = erase(node->rf, val);
    else node->lf = erase(node->lf, val);
    push_up(node);
    return node;
}

int find(Node *cur, int val){
    if(cur->size == 1) return 1;
    return val > cur->lf->val ? cur->lf->size + find(cur->rf, val) : find(cur->lf, val);
}

int select(Node *cur, int k){
    if(cur->size == 1) return cur->val;
    return k > cur->lf->size ? select(cur->rf, k - cur->lf->size) : select(cur->lf, k);
}

int main(){

    n = read();
    null = new Node(0, 0, nullptr, nullptr);
    root[0] = new Node(1, INF, null, null);
    for(int i = 1; i <= n; i ++){
        int v = read(), opt = read(), x = read();
        if(opt == 1){
            root[i] = maintain(insert(root[v], x));
        }
        else if(opt == 2){
            root[i] = maintain(erase(root[v], x));
        }
        else if(opt == 3){
            printf("%d\n", find(root[v], x));
            root[i] = root[v];
        }
        else if(opt == 4){
            printf("%d\n", select(root[v], x));
            root[i] = root[v];
        }
        else if(opt == 5){
            int rk = find(root[v], x);
            if(rk == 1) printf("-2147483647\n");
            else printf("%d\n", select(root[v], rk - 1));
            root[i] = root[v];
        }
        else{
            printf("%d\n", select(root[v], find(root[v], x + 1)));
            root[i] = root[v];
        }
    }
    return 0;
}
posted @ 2019-06-05 15:27  清楚少女ひなこ  阅读(1278)  评论(1编辑  收藏  举报