hdu 2852 KiKi's K-Number(主席树)

来源:互联网 发布:中国政治知乎 编辑:程序博客网 时间:2024/06/02 04:48

KiKi's K-Number

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4213    Accepted Submission(s): 1907


Problem Description
For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. Now Kiki meets a very similar problem, kiki wants to design a container, the container is to support the three operations.

Push: Push a given element e to container

Pop: Pop element of a given e from container

Query: Given two elements a and k, query the kth larger number which greater than a in container;

Although Kiki is very intelligent, she can not think of how to do it, can you help her to solve this problem?
 

Input
Input some groups of test data ,each test data the first number is an integer m (1 <= m <100000), means that the number of operation to do. The next m lines, each line will be an integer p at the beginning, p which has three values:
If p is 0, then there will be an integer e (0 <e <100000), means press element e into Container.

If p is 1, then there will be an integer e (0 <e <100000), indicated that delete the element e from the container  

If p is 2, then there will be two integers a and k (0 <a <100000, 0 <k <10000),means the inquiries, the element is greater than a, and the k-th larger number.
 

Output
For each deletion, if you want to delete the element which does not exist, the output "No Elment!". For each query, output the suitable answers in line .if the number does not exist, the output "Not Find!".
 

Sample Input
50 51 20 62 3 22 8 170 20 20 42 1 12 1 22 1 32 1 4
 

Sample Output
No Elment!6Not Find!224Not Find!

主席树的基本操作

#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>#include<map>#include <bits/stdc++.h>using namespace std;const int N = 4e5+100;typedef long long LL;int rt[N], ls[N*100], rs[N*100], sum[N*100];int a[N], b[N], tot;void build(int &o,int l,int r){    o= ++tot,sum[o]=0;    if(l==r) return ;    int mid=(l+r)/2;    build(ls[o],l,mid);    build(rs[o],mid+1,r);    return ;}void update(int &o,int l,int r,int last,int p,int v){    o= ++tot;    ls[o]=ls[last],rs[o]=rs[last];    sum[o]=sum[last]+v;    if(l==r) return ;    int mid=(l+r)/2;    if(p<=mid) update(ls[o],l,mid,ls[last],p,v);    else update(rs[o],mid+1,r,rs[last],p,v);    return ;}int vis[100000+10], tmp;void query(int tt,int l,int r,int p){    if(r==l)    {        tmp+=sum[tt];        return;    }    int mid=(l+r)/2;    if(p<=mid) query(ls[tt],l,mid,p);    else tmp+=sum[ls[tt]],query(rs[tt],mid+1,r,p);    return ;}int query1(int tt,int l,int r,int cnt){    if(r==l) return l;    int mid=(l+r)/2;    int ans=sum[ls[tt]];    if(ans>=cnt) return query1(ls[tt],l,mid,cnt);    else return query1(rs[tt],mid+1,r,cnt-ans);}int main(){    int n;    while(scanf("%d", &n)!=EOF)    {        memset(vis,0,sizeof(vis));        tot=0;        int len=100000+1, cnt=0;        int root;        build(rt[0],1,n);        root=rt[0];        for(int i=1; i<=n; i++)        {            int p, x, k;            scanf("%d", &p);            if(p==0)            {                scanf("%d", &x);                update(rt[i],1,len,root,x,1);                root=rt[i];                vis[x]++,cnt++;            }            else if(p==1)            {                scanf("%d", &x);                if(!vis[x]) puts("No Elment!");                else                {                    update(rt[i],1,len,root,x,-1);                    root=rt[i],vis[x]--,cnt--;                }            }            else            {                scanf("%d %d", &x, &k);                tmp=0;                query(root,1,len,x);                if(tmp+k>cnt) puts("Not Find!");                else  printf("%d\n",query1(root,1,len,tmp+k));            }        }    }    return 0;}