Pagini recente » Monitorul de evaluare | Borderou de evaluare (job #639078) | Borderou de evaluare (job #3181251) | Cod sursa (job #1365583) | Cod sursa (job #2381655)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int i,n,v[200001],k,a,x,h[200001],p[200001];
void down_heap(int poz,int n){
while(poz*2<=n){
int st=poz*2;
if(st+1<=n&&v[h[st+1]]<v[h[st]])
st=st+1;
if(v[h[poz]]>v[h[st]]){
swap(h[poz],h[st]);
p[h[poz]]=poz;
p[h[st]]=st;
poz=st;
}
else
return;
}
}
void up_heap(int poz){
while(poz/2>=1){
int st=poz/2;
if(v[h[poz]]<v[h[st]]){
swap(h[poz],h[st]);
p[h[poz]]=poz;
p[h[st]]=st;
poz=st;
}
else
return;
}
}
void sort_heap(){
for(int i=n/2;i>=1;i--)
down_heap(i,k);
}
int main()
{ f>>n;
for(i=1;i<=n;i++){
f>>a;
if(a==3)
g<<v[h[1]]<<'\n';
else
if(a==1){
f>>x;
h[++k]=p[k]=k;
v[k]=x;
up_heap(k);
}
else{
f>>x;
int pos=p[x];
swap(p[x],p[h[k]]);
swap(h[k],h[pos]);
k--;
down_heap(1,k);
}
}
return 0;
}