SPOJ3273——ORDERSET - Order statistic set(Treap)

In this problem, you have to maintain a dynamic set of numbers which support the two fundamental operations

INSERT(S,x): if x is not in S, insert x into S
DELETE(S,x): if x is in S, delete x from S
and the two type of queries

K-TH(S) : return the k-th smallest element of S
COUNT(S,x): return the number of elements of S smaller than x
Input
Line 1: Q (1 ≤ Q ≤ 200000), the number of operations
In the next Q lines, the first token of each line is a character I, D, K or C meaning that the corresponding operation is INSERT, DELETE, K-TH or COUNT, respectively, following by a whitespace and an integer which is the parameter for that operation.
If the parameter is a value x, it is guaranteed that 0 ≤ |x| ≤ 109. If the parameter is an index k, it is guaranteed that 1 ≤ k ≤ 109.

Output
For each query, print the corresponding result in a single line. In particular, for the queries K-TH, if k is larger than the number of elements in S, print the word ‘invalid’.

Example
Input
8
I -1
I -1
I 2
C 0
K 2
D -1
K 1
K 2

Output
1
2
2
invalid
题目:这里写链接内容
题意:维护一个集合S,实现对集合数据的插入,删除,求第K小,求比K小的数的个数。
思路:题中的插入和删除不一定都是合法,意思是插入的数据可能已经存在但之记录一个,删除的可能是集合中不存在的。
第K小,k可能会大于集合大小,此时输出invalid 。
很显然,Treap的模板题,需要注意的是处理不合理的插入,删除,求第K小。不然很容易RE。
代码:

#include<cstdio>
#include<algorithm>
#include <ctime>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn =30100;


struct Treap {

    struct node {
        ll key, weight, cnt, size;
        node *childs[2];

        void init(ll x) {
            key = x;
            weight = rand();
            cnt = 1;
            size = 1;
            childs[0] = childs[1] = NULL;
        }
    };

    node *root;

    void update(node *&x) {
        if (x == NULL)
            return;
        ll right = 0, left = 0;
        if (x->childs[0] != NULL)right = x->childs[0]->size;
        if (x->childs[1] != NULL)left = x->childs[1]->size;
        x->size = right + left + x->cnt;
    }

    void rotate(node *&x, int t) {
        node *y = x->childs[t];
        x->childs[t] = y->childs[1 - t];
        y->childs[1 - t] = x;
        update(x);
        update(y);
        x = y;
    }

    void _insert(node *&x, ll k) {
        if (x != NULL) {
            if (x->key == k) {
                x->cnt=1;
            } else {
                int t = x->key < k;
                _insert(x->childs[t], k);
                if (x->childs[t]->weight < x->weight) {
                    rotate(x, t);
                }
            }
        } else {
            x = (node *) malloc(sizeof(node));
            x->init(k);
        }
        update(x);
    }

    void _erase(node *&x, ll k) {
        if(x==NULL)return;
        if (x->key == k) {
            if (x->cnt > 1) {
                x->cnt--;
            } else {//如果被删除节点存在子节点,先将其旋转至底层再删除
                if (x->childs[0] == NULL && x->childs[1] == NULL) {
                    x->cnt = 0;
                    return;
                }
                int t;
                if (x->childs[0] == NULL)t = 1;
                else if (x->childs[1] == NULL)t = 0;
                else t = x->childs[0]->weight > x->childs[1]->weight;
                rotate(x, t);
                _erase(x, k);
            }
        } else {
            int t = x->key < k;
            if(x->childs[t]!=NULL)_erase(x->childs[t], k);
            if(x->childs[t]!=NULL&&x->childs[t]->cnt == 0)free(x->childs[t]), x->childs[t] = NULL;//动态内存释放
        }
        update(x);
    }

    ll _getkth(node *&x, ll k) {
        ll t = 0;
        if (x->childs[0] != NULL)t = x->childs[0]->size;
        if (k <= t)return _getkth(x->childs[0], k);
        k -= t + x->cnt;
        if (k <= 0)return x->key;
        return _getkth(x->childs[1], k);
    }

    void _lower(node *&x,ll k,ll &ans){
        if(x->key<=k){
            ans=max(ans,x->key);
            if(x->childs[1]!=NULL)_lower(x->childs[1],k,ans);
        }else if(x->childs[0]!=NULL)_lower(x->childs[0],k,ans);
    }

    void _count(node *&x,ll k,ll &ans){
        if(x==NULL)return;
        if(x->key>=k){
            ans-=x->cnt;
            if(x->childs[1]!=NULL)ans-=x->childs[1]->size;
            if(x->childs[0]!=NULL)_count(x->childs[0],k,ans);
        }else if(x->childs[1]!=NULL)_count(x->childs[1],k,ans);
    }

    void insert(ll k) {
        _insert(root, k);
    }

    void erase(ll k) {
        _erase(root, k);
        if(root!=NULL&&root->cnt == 0)free(root), root = NULL;
    }

    ll getkth(ll k) {
        if(root==NULL||k>root->size)return -1;
        return _getkth(root, k);
    }
    void lower(ll k,ll &ans){
        _lower(root,k,ans);
    }
    void count(ll k,ll &ans){
        _count(root,k,ans);
    }
};
Treap tree;
int main() {
    srand((ll)time(NULL));
    int q;
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        char c;
        ll a;
        scanf(" %c%lld",&c,&a);
        if(c=='I')tree.insert(a);
        else if(c=='D')tree.erase(a);
        else if(c=='K'){
            ll ans=tree.getkth(a);
            if(ans==-1)printf("invalid\n");
            else printf("%lld\n",ans);
        }else {
            ll ans=0;
            if(tree.root!=NULL)ans=tree.root->size;
            tree.count(a,ans);
            printf("%lld\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/swust5120160705/article/details/80198173