Pagini recente » Statistici Ionut Buzamat Alexandru (gunther41) | Istoria paginii utilizator/strikeia | Istoria paginii utilizator/ilinca_stefi | Monitorul de evaluare | Cod sursa (job #313621)
Cod sursa(job #313621)
#include<algorithm>
using namespace std;
#define DIM 200001
struct heap{
int val,poz;};
int n,m,cnt,a[DIM];
heap h[DIM];
inline int fath(int nod){
return nod/2;}
inline int lson(int nod){
return 2*nod;}
inline int rson(int nod){
return 2*nod+1;}
void swap(int x,int y){
heap aux;
a[h[x].poz]=y;
a[h[y].poz]=x;
aux=h[x];
h[x]=h[y];
h[y]=aux;}
void uph(int k){
int poz,val;
for(poz=h[k].poz,val=h[k].val; k>1&&val<h[fath(k)].val; k=fath(k)){
h[k]=h[fath(k)];
a[h[k].poz]=k;}
h[k].val=val;
h[k].poz=poz;
a[h[k].poz]=k;}
void downh(int k){
int son;
do{
son=0;
if(lson(k)<=n){
son=lson(k);
if(rson(k)<=n&&h[rson(k)].val<h[lson(k)].val)
son=rson(k);
if(h[son].val>=h[k].val)
son=0;}
if(son){
swap(k,son);
k=son;}}
while(son);}
void insert(int val){
h[++n].val=val;
h[n].poz=cnt;
uph(n);}
void cut(int k){
a[h[n].poz]=k;
h[k]=h[n--];
if(k>1&&h[k].val<h[fath(k)].val)
uph(k);
else
downh(k);}
void solve(){
int i,poz,tip,val;
scanf("%d",&m);
for(i=0; i<m; ++i){
scanf("%d",&tip);
if(tip==1){
scanf("%d",&val);
++cnt;
insert(val);}
else if(tip==2){
scanf("%d",&poz);
cut(a[poz]);}
else
printf("%d\n",h[1].val);}}
int main(){
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
solve();
return 0;}