码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2852-KiKi's K-Number(线段树)

时间:2015-04-17 11:42:50      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:

KiKi‘s K-Number
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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!

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int MAXN=100010;
int sum[MAXN<<2];
void PushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void Update(int p, int x, int l, int r, int rt)
{
    if(l==r) {
        sum[rt]+=p;
        return ;
    }
    int mid=(l+r)>>1;
    if(x<=mid)
        Update(p, x, lson);
    else
        Update(p, x, rson);
    PushUp(rt);
}
int Query(int ll, int rr, int l, int r, int rt)
{
    if(ll<=l&&rr>=r) {
        return sum[rt];
    }
    int mid=l+r>>1;
    int ans=0;
    if(ll<=mid)
        ans+=Query(ll,rr,lson);
    if(rr>mid)
        ans+=Query(ll,rr,rson);
    return ans;
}
int binsearch(int k)
{
    int high=100010, low =1, mid;
    int ans=-1;
    int res;
    while(low<=high) {
        mid=(low+high)>>1;
        res=Query(1,mid,1,100010,1);
        if(res>=k) {
            high=mid-1;
            ans=mid;
        } else if(res<k) {
            low=mid+1;
        }
    }
    return ans;
}
int main()
{
    int n,i;
    int p,a,k;
    while(~scanf("%d",&n)) {
        memset(sum,0,sizeof(sum));
        while(n--) {
            scanf("%d",&p);
            if(p==0) {
                scanf("%d",&a);
                Update(1,a,1,100010,1);
            } else if(p==1) {
                scanf("%d",&a);
                if(!Query(a,a,1,100010,1)) {
                    printf("No Elment!\n");
                    continue ;
                }
                Update(-1,a,1,100010,1);
            } else {
                scanf("%d%d",&a,&k);
                int s=Query(1,a,1,100010,1);
                int ans=binsearch(s+k);
                if(ans==-1)
                    printf("Not Find!\n");
                else
                    printf("%d\n",ans);
            }
        }
    }
    return 0;
}


HDU 2852-KiKi's K-Number(线段树)

标签:

原文地址:http://blog.csdn.net/u013486414/article/details/45081401

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!