HDU 2852 (tree array + binary)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2852

Kiki's k-Number Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2668 accepted submission (s): 1227


Problem descriptionfor the k-th number, we all shoshould 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 iner;

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, 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
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!
 
Source2009 multi-university training contest 4-host by HDU idea: Use a tree array to dynamically maintain the features of a range and ~ When a number is added, an additional number is added to the number, indicating that the number of elements is more than one. Similarly, when an element is to be deleted, the number of elements is reduced by one on the element position, which means that the number of elements is reduced by one. Finally, we need to find the k-th largest number among all the elements greater than a. At this time, we need to find them by two points ~ PS: My binary writing is a little amazing. It took me a long time to debug it ~
#include <iostream>#include <stdio.h>#include <string.h>#include <string>#include <cstdio>#include <cmath>const int N=1e5+100;const int M=1e5+10;using namespace std;int c[N],m;int lowbit(int x){  return x&(-x);}void update(int x,int d){  while(x<=M)  {    c[x]+=d;    x+=lowbit(x);  }}int getsum(int x){  int ans=0;  while(x>0)  {    ans+=c[x];    x-=lowbit(x);  }  return ans;}int find(int a,int k){    int ct=getsum(a);    int l=a+1,r=M;    while(l<=r)    {     int mid=(l+r)/2;     int sum=getsum(mid);     if(sum-ct==k)     {      if(getsum(mid-1)-ct<k) return mid;      else r=mid-1;     }     else if(l==r&&sum-ct>k)return l;     else if(sum-ct<k) l=mid+1;     else if(sum-ct>k) r=mid-1;    }    if(getsum(min(l,r))-ct>k)return min(l,r);    if(getsum(max(l,r))-ct>k)return max(l,r);    return 0;}int main(){   int option,e,a,k;   while(scanf("%d",&m)!=EOF)   {     memset(c,0,sizeof(c));     for(int i=0;i<m;i++)     {      scanf("%d",&option);      if(option==0)      {       scanf("%d",&e);       update(e,1);      }      else if(option==1)      {       scanf("%d",&e);       if(getsum(e)-getsum(e-1)==0)       {         printf("No Elment!\n");         continue;       }       update(e,-1);      }      else if(option==2)      {        scanf("%d%d",&a,&k);        int temp=find(a,k);        if(temp==0)        {          printf("Not Find!\n");        }        else printf("%d\n",temp);      }     }   }   return 0;}


HDU 2852 (tree array + binary)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.