5 0 5 1 2 0 6 2 3 2 2 8 1 7 0 2 0 2 0 4 2 1 1 2 1 2 2 1 3 2 1 4
No Elment! 6 Not Find! 2 2 4 Not Find!
线段树差点超时,感觉还是树状数组棒,但是毕竟刚学线段树,要练手
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int maxn = 300010; const int maxm = 100001; #define lson l, mid, rt<<1 #define rson mid+1, r, rt<<1|1 #define MAX INT_MAX #define MIN INT_MIN struct node { int left,right; int num; }T[maxm<<2]; int ans = 0; void Creat(int left,int right,int id)//建树 { T[id].left =left; T[id].right =right; T[id].num =0; if(T[id].left ==T[id].right ) return ; Creat(left,(left+right)/2,2*id); Creat((left+right)/2+1,right,2*id+1); } void UPdata(int id,int i,int j) { if(T[id].left<=i&&T[id].right >=i) T[id].num +=j; if(T[id].left ==T[id].right ) return; if(i>T[id].right ) return; if(i<T[id].left ) return; int mid=(T[id].left +T[id].right )/2; if(i<=mid) UPdata(id*2,i,j); else UPdata(id*2+1,i,j); } void query(int id,int l,int r) { int mid=(T[id].left +T[id].right)/2; if(T[id].left ==l&&T[id].right ==r) { ans+=T[id].num ; return; } if(r<=mid) query(2*id,l,r); else if(l>mid) query(2*id+1,l,r); else { query(2*id,l,mid); query(2*id+1,mid+1,r); } } int B_search(int x) { int low = 1; int high = maxm; int mid,wz=-1; while(low<=high) { mid = (low + high) / 2; ans = 0; query(1,1,mid); if(ans>=x) { high = mid - 1; wz = mid; } else { low = mid+1; } } return wz; } int main() { int n,a,b,k; while(~scanf("%d",&n)) { Creat(1,100001,1); for(int i = 0;i<n;i++) { scanf("%d",&a); if(a==0) { scanf("%d",&b); UPdata(1,b,1); } else if(a==1) { scanf("%d",&b); ans = 0; query(1,b,b); if(!ans) puts("No Elment!"); else UPdata(1,b,-1); } else if(a==2) { scanf("%d%d",&b,&k); ans = 0; query(1,1,b); int tem = B_search(ans+k); if(tem!=-1) printf("%d\n",tem); else puts("Not Find!"); } } } return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #define init(a) memset(a,0,sizeof(a)) using namespace std; #define MAX INT_MAX #define MIN INT_MIN #define LL __int64 #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 const int maxn = 300010; const int maxm = 100010; using namespace std; int c[maxn]; int hash[maxm]; int lowbit(int x) { return x&(-x); } void add(int x,int w) { while(x<=maxn) { c[x]+=w; x+=lowbit(x); } } int sum(int i) { int s=0; while(i>0) { s += c[i]; i =i - lowbit(i); } return s; } void B_search(int b,int k) { int tem = sum(b); int low = b; int high = maxm; int mid; while(low <= high) { mid = (low + high) /2; int st = sum(mid); if(hash[mid]>0 && st-hash[mid]-tem<k&&st-tem>=k) { break; } if(st-tem<k) { low = mid + 1; } else high = mid - 1; } printf("%d\n",mid); } bool query(int b,int k) { if(sum(maxn)-sum(b)<k)return 0; B_search(b,k); return 1; } int main() { int n,a,b,k; while(scanf("%d",&n)!=EOF) { init(c); init(hash); for(int i=0;i<n;i++) { scanf("%d",&a); if(a==0) { scanf("%d",&b); add(b,1); hash[b]++; } else if(a==1) { scanf("%d",&b); if(hash[b]>0) { add(b,-1); hash[b]--; } else puts("No Elment!"); } else if(a==2) { scanf("%d%d",&b,&k); bool tep = query(b,k); if(!tep) puts("Not Find!"); } } } return 0; }
HDU 2852 KiKi's K-Number(线段树+树状数组),布布扣,bubuko.com
HDU 2852 KiKi's K-Number(线段树+树状数组)
原文:http://blog.csdn.net/wjw0130/article/details/38559433