6 in 874 query out in 24622 in 12194 query
Case #1: 874 24622
主席树随便搞搞就行。听别人说说树状数组也能搞,于是学习了下,果然神奇
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1e4 + 10; int c[maxn],tot; void Modify(int x,int d) { for(int i = x; i <= tot; i += i&-i) c[i] += d; } int findk(int k,int limt) { int ans = 0,cnt = 0; for(int i = limt; i >= 0; i--) { ans += (1<<i); if(ans > tot || cnt + c[ans] >= k) ans -= (1<<i); else cnt += c[ans]; } return ans + 1; } char Q[maxn]; int date[maxn],sorted[maxn]; int main(int argc, char const *argv[]) { int n; int cas = 1; while(scanf("%d",&n) == 1) { printf("Case #%d:\n",cas++); int cur = 1; for(int i = 1; i <= n; i++) { char cmd[10]; scanf("%s",cmd); Q[i] = cmd[0]; if(Q[i]=='i') { scanf("%d",date + cur); sorted[cur] = date[cur]; ++cur; } } tot = cur; memset(c,0,sizeof(c[0])*tot); sort(sorted+1,sorted+cur); int ql = 0,qr = 0; for(int i = 1; i <= n; i++) { if(Q[i]=='i') { ++qr; int pos = lower_bound(sorted+1,sorted+cur,date[qr]) - sorted; Modify(pos,1); }else if(Q[i] == 'o') { ++ql; int pos = lower_bound(sorted+1,sorted+cur,date[ql]) - sorted; Modify(pos,-1); }else { int k = (qr - ql + 2) >> 1; printf("%d\n",sorted[findk(k,18)]); } } } return 0; }
原文地址:http://blog.csdn.net/acvcla/article/details/46279943