记录编号 376929 评测结果 AAAAAAAAAA
题目名称 [Tyvj 1728]普通平衡树 最终得分 100
用户昵称 Gravatarsxysxy 是否通过 通过
代码语言 C++ 运行时间 0.959 s
提交时间 2017-02-28 16:33:49 内存使用 0.37 MiB
显示代码纯文本
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdarg>
#include <cctype>
#include <algorithm>
using namespace std;
int L_LIMIT = 0;
int R_LIMIT = 0;
namespace IO
{
    char buf[1<<16], *fs, *ft;
    inline char readc()
    {
        return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<16,stdin), fs==ft))?0:*fs++;
    }
    inline int fast_read()
    {
        char c = readc();
        int r = 0;
        bool sig = false;
        while(c > '9' || c < '0')
        {
            if(c == '-')sig = true;
            c = readc();
        }
        while(c >= '0' && c <= '9')
        {
            r = (r<<3)+(r<<1)+(c^0x30);
            c = readc();
        }
        return sig?-r:r;
    }
}using IO::fast_read;using IO::readc;
struct node
{
    node *ls, *rs;
    int cnt;
    node()
    {
        ls = rs = NULL;
        cnt = 0;
    }
    inline void pushup()
    {
        cnt = 0;
        if(ls)
        {
            if(!ls->cnt)
            {
                delete ls;
                ls = NULL;
            }else cnt += ls->cnt;
        }
        if(rs)
        {
            if(!rs->cnt)
            {
                delete rs;
                rs = NULL;
            }else cnt += rs->cnt;
        }
    }
}*root;
void insert(node *&x, int p, int L, int R, int d = 1)
{
    if(!x)x = new node();
    if(L == R && L == p){x->cnt += d;return;}
    else 
    {
        int M = (L+R)>>1;
        if(p <= M)insert(x->ls, p, L, M, d);
        else if(p > M)insert(x->rs, p, M+1, R, d);
    }
    x->pushup();
}
int query(node *x, int ql, int qr, int L, int R)
{
    if(!x)return 0;
    if(L >= ql && R <= qr)return x->cnt;
    else{
        int r = 0, M = (L+R)>>1;
        if(ql <= M)r += query(x->ls, ql, qr, L, M);
        if(qr > M)r += query(x->rs, ql, qr, M+1, R);
        return r;
    }
}
int kth(int k)
{
    int l = L_LIMIT, r = R_LIMIT;
    int ans;
    while(l <= r)
    {
        int m = (l+r)>>1;
        if(query(root, L_LIMIT, m, L_LIMIT, R_LIMIT) >= k)
        {
            ans = m;
            r = m-1;
        }else l = m+1;
    }
    return ans;
}
inline int rank(int v)
{
    return query(root, L_LIMIT, v-1, L_LIMIT, R_LIMIT)+1;
}
inline int pre(int v)
{
    int k = rank(v);
    return kth(k-1);
}
inline int succ(int v)
{
    int k = query(root, L_LIMIT, v, L_LIMIT, R_LIMIT);
    return kth(k+1);    
}
int main()
{
  //  freopen("test_data.txt", "r", stdin);
    freopen("phs.in", "r", stdin);
    freopen("phs.out", "w", stdout);
    L_LIMIT = -1e7, R_LIMIT = 1e7;
    int n = fast_read();
    while(n--)
    {
        int op = fast_read();
        int v = fast_read();
        switch(op)
        {
            case 1:
                insert(root, v, L_LIMIT, R_LIMIT);
            break;
            case 2:
                insert(root, v, L_LIMIT, R_LIMIT, -1);
            break;
            case 3:
                printf("%d\n", rank(v));
            break;
            case 4:
                printf("%d\n", kth(v));
            break;
            case 5:
                printf("%d\n", pre(v));
            break;
            case 6:
                printf("%d\n", succ(v));
            break;
        }
    }
    return 0;
}