-
Notifications
You must be signed in to change notification settings - Fork 0
/
10571.cpp
78 lines (78 loc) · 1.22 KB
/
10571.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
#define Maxval 100009
int tree[Maxval+1];
int stack[Maxval+1];
void update(int idx,int val){
while(idx<Maxval){
tree[idx]+=val;
idx+=(idx&-idx);
}
}
int read(int idx){
int sum=0;
while(idx>0){
sum+=tree[idx];
idx-=(idx&-idx);
}
return sum;
}
int find_cumulate(int cumfre){
int low=0,high=Maxval,mid;
while(low<high-1){
if((low+high)&1==1)
mid=(low+high-1)/2;
else
mid=(low+high)/2;
int sum=read(mid);
if(sum>=cumfre)
high=mid;
else
low=mid;
}
return high;
}
int main(){
int n,i;
char str[20];
scanf("%d",&n);
long tmp,top;
memset(tree,0,Maxval*sizeof(int));
memset(stack,0,Maxval*sizeof(int));
top=0;
for(i=0;i<n;i++){
scanf("%str",str);
if(str[1]=='o'){//POP
if(top>0){
tmp=stack[top--];
printf("%d\n",tmp);
update(tmp,-1);
}
else
printf("Invalid\n");
}
else if(str[1]=='u'){//push
cin>>tmp;
stack[++top]=tmp;
update(tmp,1);
}
else if(str[1]=='e'){//media
if(top>0){
if(top&1==1)
tmp=(top+1)/2;
else
tmp=top/2;
tmp=find_cumulate(tmp);
printf("%d\n",tmp);
}
else
printf("Invalid\n");
}
else{
printf("Invalid\n");
}
}
return 0;
}