hdu 2852 KiKi’s K-Number(樹狀數組+二分)

來源:互聯網
上載者:User

3個操作:

0:插入

1: 刪除,如果沒有,則輸出  No Elment!

2:尋找比a大k個數的數

本來想用堆做,結果優先隊列沒有彈出任意一個數的操作。只能放棄,看大神都是二分做的。

/*Problem ID:meaning:Analyzing:*/#include <iostream>#include <algorithm>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<vector>#include<queue>using namespace std;typedef struct even{int y1,y2,x;}even;#define clr(A,k) memset(A,k,sizeof(A))#define FOR(i,s,t) for(int i=(s); i<(t); i++)#define LL long long#define BUG puts("here!!!")#define print(x) printf("%d\n",x)#define STOP system("pause")#define eps 1e-8#define PI acos(-1.0)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define maxn 100001#define lowbit(x) x&(-x)LL gcd(LL a,LL b) {return a?gcd(b%a,a):b;}priority_queue<int>q1;int A[maxn],C[maxn];void update(int pos,int val){    while(pos<maxn){        C[pos]+=val;        pos+=lowbit(pos);    }}int query(int x){    int ret=0;    while(x>0){        ret+=C[x];        x-=lowbit(x);    }    return ret;}int binsearch(int a,int k){    int l=a+1;    int r=maxn-1;    int ans=maxn;    int s1=query(a);    while(l<=r){        int m=(l+r)>>1;        int s2=query(m);        if(s2-s1>=k){            r=m-1;            if(m<ans) ans=m;        }        else {            l=m+1;        }    }    return ans;}int main(){    int m,op,a,k,e;    while(~scanf("%d",&m)){        clr(A,0);        clr(C,0);        int len=0;        while(m--){            scanf("%d",&op);            if(op==0){                scanf("%d",&e);                if(A[e]==0) update(e,1);            }            else if(op==1) {                scanf("%d",&e);                if(query(e)-query(e-1)==0) puts("No Elment!");                else                    update(e,-1);            }            else if(op==2){                scanf("%d%d",&a,&k);                int ret=binsearch(a,k);                if(ret==maxn) puts("Not Find!");                else printf("%d\n",ret);            }        }    }return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.