hdu 2852 KiKi's K-Number

Written with StackEdit.

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

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!

Solution

  • 由于这道题只需要支持插入,删除,求区间第(k)大,所以可以只用一个权值线段树/树状数组+二分.
  • 每次二分答案,计算出这个数的最小排名和最大排名.若(k)在这个区间内,则答案合法,否则调整(L)(R).
  • 由于此题的权值较小,可以不用离散化.
#include<bits/stdc++.h>
using namespace std;
typedef long long LoveLive;
inline int read()
{
	int out=0,fh=1;
	char jp=getchar();
	while ((jp>'9'||jp<'0')&&jp!='-')
		jp=getchar();
	if (jp=='-')
		{
			fh=-1;
			jp=getchar();
		}
	while (jp>='0'&&jp<='9')
		{
			out=out*10+jp-'0';
			jp=getchar();
		}
	return out*fh;
}
const int MAXN=1e5+10;
#define lowbit(x) x&(-x)
int bit[MAXN];
inline void add(int x,int c)
{
	for(;x<=1e5+1;x+=lowbit(x))
		bit[x]+=c;
}
inline int sum(int x)
{
	int res=0;
	for(;x;x-=lowbit(x))
		res+=bit[x];
	return res;
}
inline void init()
{
	memset(bit,0,sizeof bit);
}
int Find(int a,int k)
{
	int L=a+1,R=1e5;
	if(sum(R)-sum(L-1)<k)
		return -1;
	while(L<=R)
		{
			int mid=(L+R)>>1;
			int minth=sum(mid-1)-sum(a)+1;
			int maxth=sum(mid)-sum(a);
			if(minth<=k && k<=maxth)
				return mid;
			else if(k<minth)
				R=mid-1;
			else
				L=mid+1;
		}
	return -1;
}
int main()
{
	int m;
	while(~scanf("%d",&m))
		{
			init();
			for(int i=1;i<=m;++i)
				{
					int op=read();
					if(op==0)
						{
							int e=read();
							add(e,1);
						}
					else if(op==1)
						{
							int e=read();
							if(sum(e)-sum(e-1))
								add(e,-1);
							else
								puts("No Elment!");
						}
					else
						{
							int a=read(),k=read();
							int ans=Find(a,k);
							if(ans>0)
								printf("%d
",ans);
							else
								puts("Not Find!");
						}
				}
		}
	return 0;
}
原文地址:https://www.cnblogs.com/jklover/p/10130865.html