-
Notifications
You must be signed in to change notification settings - Fork 0
/
1057.cpp
63 lines (61 loc) · 1.32 KB
/
1057.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <cstdio>
#include <cstring>
#include <stack>
#include <string>
#include <algorithm>
#include <iostream>
#define lowbit(i) ((i)&(-i))
using namespace std;
const int MAXN = 100010;
int c[MAXN];
stack<int> st;
void update(int x,int v){
for(int i=x;i<MAXN;i+=lowbit(i))
c[i] += v;
}
int getSum(int x){
int sum = 0;
for(int i=x;i>0;i-=lowbit(i))
sum += c[i];
return sum;
}
void Peek(){
int l= 1,r = MAXN,mid,k = (st.size()+1)/2;
while (l < r) {
mid = (l+r) / 2;
if(getSum(mid) >= k) r= mid;
else l = mid+1;
}
printf("%d\n",l);
}
int main(){
int n;
scanf("%d%*c",&n);
fill(c, c+MAXN, 0);
for(int i=0;i<n;i++){
string str;
getline(cin,str);
if(str[1] == 'o'){
if(st.empty())
printf("Invalid\n");
else{
int val = st.top(); st.pop();
printf("%d\n",val);
update(val, -1);
}
}else if( str[1] == 'u'){
int val = 0;
for(int i=5;i<str.size();i++){
val = val*10+(str[i]-'0');
}
st.push(val);
update(val,1);
}else {
if(st.empty())
printf("Invalid\n");
else{
Peek();
}
}
}
}