hdu KiKi's K-Number 主席树
KiKi's K-Number
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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?
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.
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
5
0 5
1 2
0 6
2 3 2
2 8 1
7
0 2
0 2
0 4
2 1 1
2 1 2
2 1 3
2 1 4
Sample Output
No Elment!
6
Not Find!
2
2
4
Not Find!
Source
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=1e5+10,M=1e6+10,inf=2e9; const ll INF=1e18+10,mod=2147493647; struct Chairmantree { int rt[N*20],ls[N*20],rs[N*20],sum[N*20]; int tot; void init() { tot=0; } void build(int l,int r,int &pos) { pos=++tot; sum[pos]=0; if(l==r)return; int mid=(l+r)>>1; build(l,mid,ls[pos]); build(mid+1,r,rs[pos]); } void update(int p,int c,int pre,int l,int r,int &pos) { pos=++tot; ls[pos]=ls[pre]; rs[pos]=rs[pre]; sum[pos]=sum[pre]+c; if(l==r)return; int mid=(l+r)>>1; if(p<=mid) update(p,c,ls[pre],l,mid,ls[pos]); else update(p,c,rs[pre],mid+1,r,rs[pos]); } int rank(int s,int e,int L,int R,int l,int r) { if(L<=l&&r<=R)return sum[e]-sum[s]; int mid=(l+r)>>1; int ans=0; if(L<=mid) ans+=rank(ls[s],ls[e],L,R,l,mid); if(R>mid) ans+=rank(rs[s],rs[e],L,R,mid+1,r); return ans; } int query(int L,int R,int l,int r,int k) { if(l==r)return l; int mid=(l+r)>>1; int x=sum[ls[R]]-sum[ls[L]]; if(k<=x) return query(ls[L],ls[R],l,mid,k); else return query(rs[L],rs[R],mid+1,r,k-x); } }; Chairmantree tree; int main() { int n,le=1e5+10; while(~scanf("%d",&n)) { tree.init(); tree.build(1,le,tree.rt[0]); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); if(x==0) { int z; scanf("%d",&z); tree.update(z,1,tree.rt[i-1],1,le,tree.rt[i]); } else if(x==1) { int z; scanf("%d",&z); if(tree.rank(tree.rt[0],tree.rt[i-1],z,z,1,le)) { tree.update(z,-1,tree.rt[i-1],1,le,tree.rt[i]); } else { tree.update(z,0,tree.rt[i-1],1,le,tree.rt[i]); printf("No Elment!\n"); } } else { tree.update(1,0,tree.rt[i-1],1,le,tree.rt[i]); int a,k; scanf("%d%d",&a,&k); int v=tree.rank(tree.rt[0],tree.rt[i],1,a,1,le); k+=v; int q=tree.rank(tree.rt[0],tree.rt[i],1,le,1,le); //cout<<"xxxx "<<v<<" "<<k<<" "<<q<<endl; if(k>q) printf("Not Find!\n"); else printf("%d\n",tree.query(tree.rt[0],tree.rt[i],1,le,k)); } //for(int j=1;j<=9;j++) //cout<<"xxx "<<j<<" "<<tree.rank(tree.rt[0],tree.rt[i],j,j,1,le)<<endl; } } return 0; }