HIHOCODER 1329 stl

来源:互联网 发布:nginx反向代理什么意思 编辑:程序博客网 时间:2024/06/08 09:35

传送门 : 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;}
0 0