Pagini recente » Cod sursa (job #3181865) | Utilizatori inregistrati la Infoarena Monthly 2014 - Runda 1 | Cod sursa (job #2957609) | Cod sursa (job #3249160) | Cod sursa (job #2627473)
#include <bits/stdc++.h>
using namespace std;
const int nmax=200005;
const int inf=(1<<30);
int heap[nmax],where[nmax],which[nmax];
int nodes,added;
void swapOrder(int a,int b){
swap(heap[a],heap[b]);
swap(which[a],which[b]);
where[which[a]]=a;
where[which[b]]=b;
}
void up(int pos){
while(pos!=1&&heap[pos]<heap[pos/2]){
swapOrder(pos,pos/2);
pos/=2;
}
}
void down(int pos=1){
bool changed=true;
while(changed){
changed=false;
if(2*pos<=nodes){
int minSon=2*pos;
if(2*pos+1<=nodes&&heap[2*pos+1]<=heap[2*pos])
minSon=2*pos+1;
if(heap[minSon]<heap[pos]){
changed=true;
swapOrder(minSon,pos);
pos=minSon;
}
}
}
}
int add(int val){
++nodes;
++added;
which[nodes]=added;
where[added]=nodes;
heap[nodes]=val;
up(nodes);
}
int del(int wh){
heap[where[wh]]=-inf;
up(where[wh]);
heap[1]=heap[nodes];
which[1]=which[nodes];
nodes--;
if(nodes)
down();
}
int getMin(){
return heap[1];
}
int main()
{
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n;
for(f>>n;n--;){
int tip,x;
f>>tip;
if(tip!=3)
f>>x;
if(tip==1)
add(x);
if(tip==2)
del(x);
if(tip==3)
g<<getMin()<<'\n';
}
return 0;
}