fhq treap

这是一篇绝对优质的blog!

旋转!合并!easy!quickly!
上代码
普通

// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
using namespace std;
#define MAXN 1000000
int n, opt, a, cnt, root, x, y, z;
struct node {
    int l, r, val, key, size;
} t[MAXN];
inline int read() {
    int s = 0, w = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
    for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
    return s * w;
}
inline int New(int val) {
    t[++cnt].val = val, t[cnt].key = rand() * rand(), t[cnt].size = 1;
    return cnt;
}
inline void update(int now) {
    t[now].size = t[t[now].l].size + t[t[now].r].size + 1;
}
inline void Split(int now, int w, int &u, int &v) {
    if (!now) u = v = 0;
    else {
        if (t[now].val <= w) u = now, Split(t[now].r, w, t[u].r, v);
        else v = now, Split(t[now].l, w, u, t[v].l);
        update(now);
    }
}
inline int Merge(int u, int v) {
    if (!u || !v) return u + v;
    if (t[u].key < t[v].key) {
        t[u].r = Merge(t[u].r, v);
        update(u);
        return u;
    }
    else {
        t[v].l = Merge(u, t[v].l);
        update(v);
        return v;
    }
}
inline void Insert(int val) {
    int x, y;
    Split(root, val, x, y);
    root = Merge(Merge(x, New(val)), y);
}

inline int Kth(int now, int sum) {
    while (1) {
        if (sum <= t[t[now].l].size) now = t[now].l;
        else if (sum == t[t[now].l].size + 1) return now;
        else sum -= t[t[now].l].size + 1 , now = t[now].r;
    }
}
int main() {
    srand(time(0));
    n = read();
    while (n--) {
        opt = read(), a = read();
        switch (opt) {
            case 1 : {
                Insert(a);
                break;
            }
            case 2 : {
                Split(root, a, x, z);
                Split(x, a - 1, x, y);
                y = Merge(t[y].l, t[y].r);
                root = Merge(Merge(x, y), z);
                break;
            }
            case 3 : {
                Split(root, a - 1, x, y);
                printf("%d\n", t[x].size + 1);
                root = Merge(x, y);
                break;
            }
            case 4 : {
                printf("%d\n", t[Kth(root, a)].val);
                break;
            }
            case 5 : {
                Split(root, a - 1, x, y);
                printf("%d\n", t[Kth(x, t[x].size)].val);
                root = Merge(x, y);
                break;
            }
            case 6 : {
                Split(root, a, x, y);
                printf("%d\n", t[Kth(y, 1)].val);
                root = Merge(x, y);
                break;
            }
        }
    }
    return 0;
}

文艺

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
using namespace std;
#define MAXN 200010
int n, m, root, cnt;
struct node {
    int l, r, val, key, size, tag;
} t[MAXN];
inline int read() {
    int s = 0, w = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
    for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
    return s * w;
}
inline int New(int val) {
    t[++cnt].val = val, t[cnt].key = rand() * rand(), t[cnt].size = 1;
    return cnt;
}
inline void update(int now) {
    t[now].size = t[t[now].l].size + t[t[now].r].size + 1;
}
inline void pushdown(int now) {
    if (t[now].tag) {
        swap(t[now].l, t[now].r);
        t[t[now].l].tag ^= 1, t[t[now].r].tag ^= 1;
        t[now].tag = 0;
    }
}
void Split(int now, int w, int &u, int &v) {
    if (!now) u = v = 0;
    else {
        pushdown(now);
        if (t[t[now].l].size < w)
            u = now, Split(t[now].r, w - t[t[now].l].size - 1, t[now].r, v);
        else
            v = now, Split(t[now].l, w, u, t[now].l);
        update(now);
    }
}
int Merge(int u, int v) {
    if (!u || !v) return u + v;
    if (t[u].key < t[v].key) {
        pushdown(u);
        t[u].r = Merge(t[u].r, v);
        update(u);
        return u;
    }
    else {
        pushdown(v);
        t[v].l = Merge(u, t[v].l);
        update(v);
        return v;
    }
}
void write(int now) {
    if (!now) return;
    pushdown(now);
    write(t[now].l);
    printf("%d ", t[now].val);
    write(t[now].r);
}
int main() {
    srand(time(0));
    n = read(), m = read();
    for (register int i = 1; i <= n; i++)
        root = Merge(root, New(i));
    while (m--) {
        int l = read(), r = read(), x, y, z;
        Split(root, r, x, y);
        Split(x, l - 1, x, z);
        t[z].tag ^= 1;
        root = Merge(Merge(x, z), y);
    }
    write(root);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值