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?
InputInput 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.
OutputFor 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 4Sample Output
No Elment! 6 Not Find! 2 2 4 Not Find!
题意:有3种操作:0.将所给元素压进容器。1.将所给元素从容器中删除,若没有输出No Elment! 2.找比a大的数中第k大的数(可用二分逼近)
#include<iostream> #include<cstring> #include<vector> #include<string> #include<stdio.h> using namespace std; typedef long long ll; int lowbit(int x){return x&-x;} const int maxn=100010; int c[maxn+10]; int n,m; void update(int x,int v) { for(int i=x;i<=maxn;i+=lowbit(i)) c[i]+=v; } int sum(int x) { int ans=0; for(int i=x;i>=1;i-=lowbit(i)) ans+=c[i]; return ans; } int main() { ios::sync_with_stdio(0); int m; while(cin>>m){ memset(c,0,sizeof(c)); while(m--){ int op,x,k; cin>>op; if(op==0){ cin>>x; update(x,1); } else if(op==1){ cin>>x; if(sum(x)-sum(x-1)==0)cout<<"No Elment!"<<endl; else update(x,-1); } else{ cin>>x>>k; int l=x+1,r=100010,ans=-1; while(l<=r){ int mid=(l+r)/2; if(sum(mid)-sum(x)>=k)//如果[x+1,mid]区间比x大的超过k个,说明答案可以更小 ans=mid,r=mid-1; else l=mid+1;//否则答案需要更大 } if(ans==-1)cout<<"Not Find!"<<endl; else cout<<ans<<endl; } } } return 0; }
2:线段树版本(不能简单的用二分了,需要直接写个递归函数,有点像权值线段树的意思了)
#include<iostream> #include<cstring> #include<vector> #include<string> #include<stdio.h> using namespace std; typedef long long ll; int lowbit(int x){return x&-x;} const int maxn=100010; int tree[maxn<<2]; void update(int l,int r,int x,int v,int rt) { if(l==r){ if(v==1)tree[rt]++; else{ if(tree[rt]==0) cout<<"No Elment!"<<endl; else tree[rt]--; } return ; } int mid=(l+r)/2; if(x<=mid)update(l,mid,x,v,rt*2); else update(mid+1,r,x,v,rt*2+1); tree[rt]=tree[rt*2]+tree[rt*2+1]; } int querysum(int l,int r,int L,int R,int rt) { if(L<=l&&R>=r)return tree[rt]; int mid=(l+r)/2; int ans=0; if(L<=mid)ans+=querysum(l,mid,L,R,rt*2); if(R>=mid+1)ans+=querysum(mid+1,r,L,R,rt*2+1); return ans; } int query(int l,int r,int k,int rt) { if(l==r)return l; int mid=(l+r)/2; if(k<=tree[rt*2])return query(l,mid,k,rt*2); else return query(mid+1,r,k-tree[rt*2],rt*2+1); } int main() { int m; int n=100010; while(scanf("%d", &m) != EOF){ memset(tree,0,sizeof(tree)); while(m--){ int op,x,k; scanf("%d",&op); if(op==0){ scanf("%d",&x); update(1,n,x,1,1); } else if(op==1){ scanf("%d",&x); update(1,n,x,-1,1); } else{ scanf("%d%d",&x,&k); int pos=querysum(1,n,1,x,1); int ans=query(1,n,pos+k,1); if(ans>=maxn)cout<<"Not Find!"<<endl; else cout<<ans<<endl; } } } return 0; }