hdu 5.3.1 2852 KiKi's K-Number

来源:互联网 发布:网络英文 编辑:程序博客网 时间:2024/05/16 05:28

好像线段树和树状数组都可以解……不过困死了,先贴树状数组的吧……我也不知道为什么我一时脑残居然选了一个新的解法

KiKi's K-Number

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 44 Accepted Submission(s): 22 
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 <cstring>#define maxn 300003#define maxm 100002using namespace std;int a[maxm];int c[maxn];int lowbit(int x)//find the max power of two that x can be divided{return -x&x;}void p(int pos,int num){    while(pos<maxn)    {        c[pos]+=num;        pos+=lowbit(pos);//find the node higher than this    }}int sum(int end){    int sum=0;    while(end>0)    {        sum+=c[end];        end-=lowbit(end);//find the node smaller than this    }    return sum;}int query(int n,int k){    int mid,low,high;    int i,j,t;    if(sum(maxm)-sum(n)<k)        return 0;    i=sum(n);//how many number smaller than or equal to a    low=n;    high=maxm;    while(low<=high)    {        mid=(low+high)>>1;        t=sum(mid);//how many number smaller than or equal to mid        if(a[mid] && t-a[mid]<k+i && t>=k+i)        //have number mid               break;        else if(t<k+i)            low =mid+1;        else            high=mid-1;    }    printf("%d\n",mid);    return 1;}                        int main(){    int t;    while(cin>>t)    {         memset(c,0,sizeof(c));         memset(a,0,sizeof(a));         while(t--)         {             int s;             int n,m;             scanf("%d",&s);             if(!s)//s==0             {                 scanf("%d",&n);                 p(n,1);                 a[n]++;             }             else if(s==1)             {                  scanf("%d",&n);                  if(a[n]<=0)                      printf("No Elment!\n");                  else                  {                      p(n,-1);                      a[n]--;                  }             }             else             {                 scanf("%d%d",&n,&m);                 if(!query(n,m))                     printf("Not Find!\n");             }         }    }                           return 0;}

解里面的lowbit很有趣,把数组拗成了一棵树……

总体来说这个做法我只能到看懂的程度……

顺便吐槽一下你妹element题目居然拼错了……偶尔没直接复制不用就这么整我吧晕……

困死了……

原创粉丝点击