Pagini recente » Cod sursa (job #2380933) | Cod sursa (job #2389151) | Cod sursa (job #1101037) | Cod sursa (job #1987680) | Cod sursa (job #2905092)
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int N = 2e5;
int h[N+1],poz[N+1],v[N+1],nh;
void schimb(int p1,int p2){
swap(h[p1],h[p2]);
poz[h[p1]] = p1;
poz[h[p2]] = p2;
}
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 adauga(int val){
h[++nh] = val;
poz[val] = nh;
urca(nh);
}
void sterge(int p){
if(p==nh){
nh--;
return;
}
schimb(p,nh--);
urca(p);
coboara(p);
}
int main() {
int n = 0, nr_op;
in >> nr_op;
for(int i=0;i<nr_op;i++){
int x;
in >> x;
if(x==1){
in >> v[++n];
adauga(n);
}
if(x==2){
int p;
in >> p;
sterge(poz[p]);
}
if(x==3){
out<<v[h[1]]<<"\n";
}
}
return 0;
}