Pagini recente » Cod sursa (job #2336921) | Cod sursa (job #2152675) | Cod sursa (job #2321476) | Cod sursa (job #1767698) | Cod sursa (job #2781641)
#include <iostream>
#include <fstream>
#include<vector>
#include<climits>
using namespace std;
vector <int> g[16002];
int v[200001],h[200001],poz[200001],k;
int nr,n,m;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
void insert_heap(int k){
while(k/2>0&&v[h[k]]<v[h[k/2]]){
{
swap(h[k],h[k/2]);
poz[h[k]]=k;
poz[h[k/2]]=k/2;
k=k/2;
}
}
}
void remove_heap(int p){
if(p==k){
k--;
return;
}
h[p]=h[k--];
poz[h[p]]=p;
while(p/2>0&&v[h[p]]<v[h[p/2]]){
{
swap(h[p],h[p/2]);
poz[h[p]]=p;
poz[h[p/2]]=p/2;
p=p/2;
}
}
while(2*p<=k){
int st=2*p, dr=2*p+1,crt=p;
if(v[h[st]]<v[h[crt]])
crt=st;
if(dr<=k&&v[h[dr]]<v[h[crt]])
crt=dr;
if(crt!=p){
swap(h[p],h[crt]);
poz[h[crt]]=crt;
poz[h[p]]=p;
p=crt;
}
else
break;
}
}
int main(){
fin>>n; int c,val;
for(int i=1;i<=n;i++){
fin>>c;
if(c==1){
fin>>val;
v[++nr]=val;
h[++k]=nr;
poz[nr]=k;
insert_heap(k);
}
else
if(c==2){
fin>>val;
remove_heap(poz[val]);
}
else
fout<<v[h[1]]<<'\n';
}
return 0;
}