HIHOCODER 1329 stl

传送门 : HIHO 1329

11111

尝试splay, 未果
发现我对splay的理解太浅了, 不适合这样搞
还是太急了, 需要多点时间入门
大物考试啊, 祝我不挂
这题直接用set维护
有趣
也算补了一下stl


code:

#include<iostream>
#include<cstring>
#include<set>
using namespace std;

#define keyTree ch[ch[root][1]][0]
#define ls ch[x][0]
#define rs ch[x][1]
typedef long long LL;
const int maxn = 200000 + 5;

struct Splay {
    int  pre[maxn], ch[maxn][2];
    LL v[maxn];
    int root, rt;

    Splay() {
        root = rt = 0;
        memset(v, 0, sizeof(v));
        memset(pre, 0, sizeof(pre));
        memset(ch, 0, sizeof(ch));
    }


    void newNode(int &x,LL c, int f) {//creat
        x = ++rt;
        v[x] = c;
        pre[x] = f;
    }


    void Rotate(int x, int d) {
        int y = pre[x];
        ch[y][!d] = ch[x][d];
        pre[ch[y][!d]] = y;

        pre[x] = pre[y];
        ch[x][d] = y;

        if (pre[y]) ch[pre[y]][ch[pre[y]][1] == y] = x;
        pre[y] = x;

    }

    void splay(int x, int to) {

        while (pre[x] != to) {
            if (pre[pre[x]] == to) Rotate(x, ch[pre[x]][0] == x);
            else {
                int y = pre[x], z = pre[y];
                int f = (ch[z][1] == y);
                if (ch[y][f] == x)
                    Rotate(y, !f), Rotate(x, !f);
                else
                    Rotate(x, f), Rotate(x, !f);
            }
        }

        if (!to) root = x;
    }

    void ins(LL k) {
        //cout << k << endl;
        int x = root;
        int tmp = root;
        while (x != 0) {
            tmp = x;
            if (v[x] < k) x = rs;
            else if (v[x] > k) x = ls; else return;
        }

        newNode(ch[tmp][v[tmp] < k], k, tmp);
        splay(ch[tmp][v[tmp] < k],0);
        ch[0][0] = ch[0][1] = 0;
        //cout << k << endl;
    }

    int upSearch(LL val) {
        int x = root, idx = 0;

        while (x != 0 && v[x] != val) {
            if (v[x] < val)
                x = rs;
            else {
                idx = x;
                x = ls;
            }
        }

        if (x) idx = x;

        return idx;
    }

    void del(LL l, LL r) {
        int a = query(l - 1);
        int b = upSearch(r + 1);
        if(a) splay(a, 0);
        if(b) splay(b, root);
        pre[keyTree] = 0;
        keyTree = 0;
    }

    int query(LL val) {
        int x = root, idx;
        while (x != 0 && v[x] != val) {
            if (v[x] < val) {
                idx = x;
                x = rs;

            }
            else x = ls;
        }
        if (x) idx = x;
        return idx;
    }

}sp;

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif 
    //cout << "love yy" << endl;
    cin.tie(0);
    cin.sync_with_stdio(false);

    int n;
    LL l, r;
    char s[5];

    while (cin >> n) {
        set<int>q;
        set<int>::iterator it;
        while (n--) {
            cin >> s >> l;
            if (s[0] == 'Q') {
                it = q.upper_bound(l);
                --it;
                cout << *it << endl;
            }
            else {
                if (s[0] == 'I') q.insert(l);
                else {
                    cin >> r;
                    q.erase(q.lower_bound(l), q.upper_bound(r));
                }
            }
        }
    }


    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值