Pagini recente » Cod sursa (job #1355075) | Cod sursa (job #408983) | Cod sursa (job #2394338) | Cod sursa (job #2947971) | Cod sursa (job #2381675)
#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],nr;
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;
nr=0;
for(i=1;i<=n;i++){
f>>a;
if(a==3)
g<<v[h[1]]<<'\n';
else
if(a==1){
f>>x;
v[++nr]=x;
h[++k]=nr;
p[nr]=k;
up_heap(k);
}
else{
f>>x;
int pos=p[x];
swap(p[x],p[h[k]]);
swap(h[k],h[pos]);
k--;
down_heap(pos,k);
}
}
return 0;
}