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

题目地址:HDU 2852

真是弱渣。。。连是线段树的题都没看出来。。。。自己用了种最搓的方法,还差点TLE。。。

搓代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
int sum[400002];
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 get(int k)
{
    int high=100000, low =1, mid, ans=-1, s;
    while(low<=high)
    {
        mid=low+high>>1;
        s=query(1,mid,1,100000,1);
        if(s>=k)
        {
            high=mid-1;
            ans=mid;
        }
        else if(s<k)
        {
            low=mid+1;
        }
    }
    return ans;
}
int main()
{
    int n, p, i, a, k;
    while(scanf("%d",&n)!=EOF)
    {
        memset(sum,0,sizeof(sum));
        while(n--)
        {
            scanf("%d",&p);
            if(p==0)
            {
                scanf("%d",&a);
                update(1,a,1,100000,1);
            }
            else if(p==1)
            {
                scanf("%d",&a);
                if(!query(a,a,1,100000,1))
                {
                    printf("No Elment!\n");
                    continue ;
                }
                update(-1,a,1,100000,1);
            }
            else
            {
                scanf("%d%d",&a,&k);
                int s=query(1,a,1,100000,1);
                int ans=get(s+k);
                //printf("------%d %d\n",s,k);
                if(ans==-1)
                {
                    printf("Not Find!\n");
                }
                else
                {
                    printf("%d\n",ans);
                }
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值