题解:
树状求第k大数题目。。二分就行。。。
不过感觉二分是玄学。。。要多试几个不同的二分方式。。。。
代码:
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<map> #include<set> using namespace std; using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define ll long long #define CLR(x) memset(x,0,sizeof x) #define SZ(x) ((int)(x).size()) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++) typedef pair<int,int> P; const double eps=1e-9; const int maxn=100010; const int N=1000+10; const int mod=9901; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } //----------------------------------------------------------------------------- int m,op,a,b; int c[maxn]; int lowbit(int x){return x&-x;} void add(int x,int v){ while(x<maxn){ c[x]+=v; x+=lowbit(x); } } int sum(int x){ int cnt=0; while(x){ cnt+=c[x]; x-=lowbit(x); } return cnt; } int bs(int l,int r,int goal){ int tmp=-1; int m,L=l,R=r; while(L<R){ m=(L+R)>>1; if(sum(m)-sum(l-1)>=goal){ tmp=m; R=m; } else L=m+1; } return tmp; } int main(){ while(~scanf("%d",&m)){ CLR(c); for(int i=1;i<=m;i++){ scanf("%d",&op); if(op==0){ scanf("%d",&a); add(a,1); } if(op==1){ scanf("%d",&b); if(sum(b)-sum(b-1)==0) printf("No Elment!\n"); else add(b,-1); } if(op==2){ scanf("%d%d",&a,&b); if(bs(a+1,maxn,b)==-1) printf("Not Find!\n"); else printf("%d\n",bs(a+1,maxn,b)); } } } return 0; }
HDU2852 KiKi's K-Number
时间: 2024-10-10 09:32:53