hdu (2852) KiKi's KNumber

二分+树状数组动态的寻找比a大k的数

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <map>
using namespace std;
const int maxn = 100005;
int c[maxn];
int lowbit(int x){
    return x&(-x);
}
void update(int x,int a){
    for(int i = x;i < maxn;i+=lowbit(i))
        c[i] += a;
}
int getsum(int x){
    int ans = 0;
    for(int i = x;i > 0;i-=lowbit(i))
        ans += c[i];
    return ans;
}
void bs(int a,int k){
    int low = a+1,high = maxn;
    int temp = getsum(a);
    while(low<=high){
        int mid = (low+high)>>1;
        //cout<<mid<<endl;
        if(getsum(mid) - temp < k)
            low = mid + 1;
        else
            high = mid - 1;
    }
    if(low >= maxn)
        printf("Not Find!\n");
    else
        printf("%d\n",low);
}
int main(){
    int m;
    while(~scanf("%d",&m)){
        memset(c,0,sizeof(c));
        while(m--){
            int q,a,k;
            scanf("%d",&q);
            if(q == 0){
                scanf("%d",&a);
                update(a,1);
            }
            else if(q == 1){
                scanf("%d",&a);
                if(getsum(a) - getsum(a-1))
                    update(a,-1);
                else
                    printf("No Elment!\n");
            }
            else{
                scanf("%d%d",&a,&k);
                bs(a,k);
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Roly/p/3054463.html