这个是去年astar的题~
标准做法主席树,然而渣渣并不会(我确实叫zhazha。。。),
所以,他先离线,离散化,然后树状数组+二分水过了。。。。
离线的目的主要是为了离散化,剩下的就和用一个树状数组维护一个数之前有多少个数比他小差不多~~
二分答案+树状数组求和判断即可,注意二分何时终止~~(复杂度O(nlognlogn))
#include <bits/stdc++.h> using namespace std;
const int maxn = + ;
int n, a[maxn], input[maxn], c[maxn];
char str[maxn][]; int flag[maxn]; map<int, int> mp; inline int lowbit(int x){
return x & (-x);
} int sum(int x){
int ret = ;
while(x > ){
ret += c[x];
x -= lowbit(x);
}
return ret;
} void add(int x, int d){
while(x <= n){
c[x] += d;
x += lowbit(x);
}
} void solve(int p){
int left = , right = n+; while(left+<right){
int mid = (left + right)/;
int sum1 = sum(mid);
//cout << "mid: " << mid << " sum: " << sum1 << endl; if( sum1 > p ){
right = mid;
}else if( sum1 < p ){
left = mid;
}else{
if(flag[mid] == ){
printf("%d\n", a[mid]);
return;
}else{
right = mid;
}
}
}
} queue<int> q; int main(void){
int cas = ;
while(scanf("%d", &n) != EOF){
int len = , num;
for(int i = ; i < n; ++i){
scanf("%s", str[i]);
if(str[i][] == 'i'){
scanf("%d", &num);
input[++len] = num;
a[len] = num;
}
} //离散化
sort(a+, a+len+);
mp.clear();
for( int i = ; i <= len; ++i ){
mp[a[i]] = i;
} memset(c, , sizeof(c));
memset(flag, , sizeof(flag)); while(!q.empty())
q.pop(); printf("Case #%d:\n", cas++);
int sz = , cnt = ;
for( int i = ; i < n; ++i ){
if( str[i][] == 'i' ){
num = input[++cnt];
q.push(num);
sz++;
num = mp[num];
add(num, );
flag[num] = ;
}else if( str[i][] == 'q' ){
int p = sz/+;
//cout << "p: " << p << endl;
solve(p);
}else{
sz--;
num = q.front();
q.pop();
num = mp[num];
add(num, -);
flag[num] = ;
}
}
} return ;
} /**
11
in 32
in 16
in 87
out
in 96
in 34
query
out
out
in 3687
query
*/