小清新无旋Treap求调
查看原帖
小清新无旋Treap求调
1149429
FlamingBlade楼主2024/1/3 18:28
//start time:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
inline int read(){
    int s = 0,f = 1;char c = getchar();
    while (!isdigit(c)){if (c == '-')f = -1;c = getchar();}
    while (isdigit(c)){s = (s<<3) + (s<<1) + (c ^ 48);c = getchar();}
    return s * f;
}
struct Treap{
    #define lc tree[rt].l
    #define rc tree[rt].r
    int root,idx;
    struct node{
        int l,r;
        int val,key,siz;
    }tree[N];
    int Newnode(int val){
        tree[++idx] = (node){0,0,val,rand(),1};
        return idx;
    }
    void pushup(int rt){
        tree[rt].siz = tree[lc].siz + tree[rc].siz + 1;
    } void split(int rt,int &l,int &r,int k){
        if (!rt){l = r = 0;return ;}
        if (k < tree[rt].val){
            r = rt;split(lc,l,lc,k);
        } else {l = rt;split(rc,rc,r,k);}
        pushup(rt);
    } int merge(int l,int r){
        if (!l || !r) return l + r;
        if (tree[l].key <= tree[r].key){
            tree[r].l = merge(l,tree[r].l);
            return pushup(r),r;
        } else {
            tree[l].r = merge(tree[l].r,r);
            return pushup(l),l;
        }
    } void insert(int val){
        int l = 0,r = 0;
        split(root,l,r,val - 1);
        root = merge(merge(l,Newnode(val)),r);
    } void erase(int val){
        int l = 0,r = 0,p = 0;
        split(root,l,r,val);
        split(root,l,p,val - 1);
        p = merge(tree[p].l,tree[p].r);
        root = merge(merge(l,p),r);
    } void rank(int val){
        int l = 0,r = 0;
        split(root,l,r,val - 1);
        printf("%d\n",tree[l].siz + 1);
        root = merge(l,r);
    } void get_rank(int val){
        int rt = root;
        while (1){
            if (tree[lc].siz >= val)
                rt = lc;
            else if (tree[lc].siz + 1 == val){
                printf("%d\n",tree[rt].val);return;
            } else rt = rc,val -= (tree[lc].siz + 1);
        }
    } void get_pre(int val){
        int l = 0,r = 0;
        split(root,l,r,val - 1);
        int rt = l;
        while (rc) rt = rc;
        printf("%d\n",tree[rt].val);
        root = merge(l,r);
    } void get_suf(int val){
        int l = 0,r = 0;
        split(root,l,r,val);
        int rt = r;
        while (lc) rt = lc;
        printf("%d\n",tree[rt].val);
        root = merge(l,r);
    }
    #undef lc
    #undef rc
}Treap;
int n;
int main(){
    n = read();
    srand(time(NULL));
    while (n--){
        int opt = read(),x = read();
        if (opt == 1) Treap.insert(x);
        if (opt == 2) Treap.erase(x);
        if (opt == 3) Treap.rank(x);
        if (opt == 4) Treap.get_rank(x);
        if (opt == 5) Treap.get_pre(x);
        if (opt == 6) Treap.get_suf(x);
    }
    return 0;
}

好多点 TLE 和 MLE。

2024/1/3 18:28
加载中...