本文主要是介绍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;
}
这篇关于HDU 2852 KiKi's K-Number(线段树+二分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!