Pagini recente » Cod sursa (job #3268643) | Cod sursa (job #2527440) | Cod sursa (job #665295) | Cod sursa (job #1402266) | Cod sursa (job #2835998)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int N=2e5+5;
int h[N], poz[N], v[N], nh;
void schimb(int p, int q){
swap(h[p], h[q]);
poz[h[p]]=p;
poz[h[q]]=q;
}
void coboara(int p){
int fs=2*p, fd=2*p+1, bun=p;
if(fs<=nh && v[h[fs]]<v[h[bun]])
bun=fs;
if(fd<=nh && v[h[fd]]<v[h[bun]])
bun=fd;
if(bun!=p){
schimb(p, bun);
coboara(bun);
}
}
void urca(int p){
while(p>1&& v[h[p]]<v[h[p/2]])
{
schimb(p, p/2);
p/=2;
}
}
void sterge(int p){
if(p==nh)
nh--;
else{
schimb(p, nh--);
urca(p);
coboara(p);
}
}
void adauga(int val){
h[++nh]=val;
poz[val]=nh;
urca(nh);
}
int main()
{
int n=0;
int nr_op;
in>>nr_op;
for(int i=0; i<nr_op; i++){
int tip;
in>>tip;
if(tip==1){
in>>v[++n];
adauga(n);
}
else if(tip==2){
int p;
in>>p;
sterge(poz[p]);
}
else{
out<<v[h[1]]<<'\n';
}
}
return 0;
}