HDU2852 大于a的第k小数-树状数组

//sum(a)求的是小于等于a的数字个数

#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define maxn 100001
int c[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int num)
{
    while(x<=maxn)
    {
        c[x]+=num;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int sum=0;
    while(x>0)
    {
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}
int find(int a,int k)
{
    int l=1;
    int r=maxn;
    int mid;
    int s=sum(a);
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(sum(mid)-s<k)
            l=mid+1;
        else
            r=mid-1;
    }
    return l;
}
int main()
{
#ifdef DeBUGs
    freopen("//home//amb//桌面//1.in", "r", stdin);
#endif
    int n;
    while (scanf("%d", &n) + 1)
    {
        memset(c,0,sizeof(c));
        int op, a, k;
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &op);
            if (op == 0)
            {
                scanf("%d", &a);
                add(a, 1);
            }
            else if (op == 1)
            {
                scanf("%d", &a);
                if (sum(a) - sum(a - 1) == 0)//单点求值,是否大于1
                    printf("No Elment!\n");
                else
                    add(a, -1);
            }
            else
            {
                scanf("%d%d", &a, &k);
                if (sum(maxn) - sum(a) < k)
                    printf("Not Find!\n");
                else
                    printf("%d\n", find(a, k));
            }
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值