无旋树堆

fhq Treap

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using p = pair<int, int>;
const int inf(0x3f3f3f3f);
const int maxn(1e5 + 10);
int idx, root;
int x, y, z;

struct node {
    int l, r;
    int key, val;
    int siz;
} tree[maxn];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, bool ln)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (ln) putchar(10);
}

inline int new_node(int key)
{
    ++idx;
    tree[idx].key = key;
    tree[idx].val = rand();
    tree[idx].siz = 1;
    return idx;
}

inline void push_up(int cur)
{
    tree[cur].siz = tree[tree[cur].l].siz + tree[tree[cur].r].siz + 1;
}

inline void split(int cur, int key, int& x, int& y)
{
    if (not cur) {
        x = y = 0;
    } else if (tree[cur].key <= key) {
        x = cur;
        split(tree[cur].r, key, tree[cur].r, y);
        push_up(cur);
    } else {
        y = cur;
        split(tree[cur].l, key, x, tree[cur].l);
        push_up(cur);
    }
}

inline int merge(int x, int y)
{
    if (not x or not y) {
        return x + y;
    }
    if (tree[x].val > tree[y].val) {
        tree[x].r = merge(tree[x].r, y);
        push_up(x);
        return x;
    }
    tree[y].l = merge(x, tree[y].l);
    push_up(y);
    return y;
}

inline void insert(int key)
{
    split(root, key, x, y);
    root = merge(merge(x, new_node(key)), y);
}

inline void remove(int key)
{
    split(root, key, x, z);
    split(x, key - 1, x, y);
    y = merge(tree[y].l, tree[y].r);
    root = merge(merge(x, y), z);
}

inline int get_rank(int key)
{
    split(root, key - 1, x, y);
    int res = tree[x].siz + 1;
    root = merge(x, y);
    return res;
}

inline int get_key(int cur, int rank)
{
    if (rank == tree[tree[cur].l].siz + 1) {
        return tree[cur].key;
    }
    if (rank <= tree[tree[cur].l].siz) {
        return get_key(tree[cur].l, rank);
    }
    return get_key(tree[cur].r, rank - tree[tree[cur].l].siz - 1);
}

inline int get_prev(int key)
{
    split(root, key - 1, x, y);
    int cur = x;
    while (tree[cur].r) {
        cur = tree[cur].r;
    }
    int res = tree[cur].key;
    root = merge(x, y);
    return res;
}

inline int get_next(int key)
{
    split(root, key, x, y);
    int cur = y;
    while (tree[cur].l) {
        cur = tree[cur].l;
    }
    int res = tree[cur].key;
    root = merge(x, y);
    return res;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    int n = read();
    while (n--) {
        int op = read(), x = read();
        if (op == 1) {
            insert(x);
        } else if (op == 2) {
            remove(x);
        } else if (op == 3) {
            write(get_rank(x), true);
        } else if (op == 4) {
            write(get_key(root, x), true);
        } else if (op == 5) {
            write(get_prev(x), true);
        } else {
            write(get_next(x), true);
        }
    }
    return 0;
}
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using p = pair<int, int>;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int maxn(1e5 + 10);
int idx, root;

struct node {
    int l, r;
    int key, val;
    int siz;
    bool rev;
} tree[maxn];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, char c)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (c) putchar(c);
}

inline int new_node(int key)
{
    ++idx;
    tree[idx].key = key;
    tree[idx].val = rand();
    tree[idx].siz = 1;
    return idx;
}

inline void push_up(int cur)
{
    tree[cur].siz = tree[tree[cur].l].siz + tree[tree[cur].r].siz + 1;
}

inline void push_down(int cur)
{
    if (tree[cur].rev) {
        swap(tree[cur].l, tree[cur].r);
        tree[tree[cur].l].rev ^= 1;
        tree[tree[cur].r].rev ^= 1;
        tree[cur].rev = false;
    }
}

inline void split(int cur, int siz, int& x, int& y)
{
    if (not cur) {
        x = y = 0;
    } else {
        push_down(cur);
        if (siz > tree[tree[cur].l].siz) {
            x = cur;
            split(tree[cur].r, siz - tree[tree[cur].l].siz - 1, tree[cur].r, y);
        } else {
            y = cur;
            split(tree[cur].l, siz, x, tree[cur].l);
        }
        push_up(cur);
    }
}

inline int merge(int x, int y)
{
    if (not x or not y) {
        return x + y;
    }
    if (tree[x].val < tree[y].val) {
        push_down(x);
        tree[x].r = merge(tree[x].r, y);
        push_up(x);
        return x;
    }
    push_down(y);
    tree[y].l = merge(x, tree[y].l);
    push_up(y);
    return y;
}

inline void reverse(int l, int r)
{
    int x, y, z;
    split(root, l - 1, x, y);
    split(y, r - l + 1, y, z);
    tree[y].rev ^= 1;
    root = merge(merge(x, y), z);
}

void dfs(int cur)
{
    if (not cur) return;
    push_down(cur);
    dfs(tree[cur].l);
    write(tree[cur].key, ' ');
    dfs(tree[cur].r);
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    int n = read(), m = read();
    for (int i = 1; i <= n; ++i) {
        root = merge(root, new_node(i));
    }
    while (m--) {
        int l = read(), r = read();
        reverse(l, r);
    }
    dfs(root);
    return 0;
}

最后更新于