Pagini recente » Cod sursa (job #2170939) | Cod sursa (job #1680967) | Cod sursa (job #2175738) | Cod sursa (job #115372) | Cod sursa (job #754507)
Cod sursa(job #754507)
#include<fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int N = 200000;
int v[N], poz[N], h[N], nn, nh = 0, n = 0;
void schimb(int x, int y)
{
int aux = h[x];
h[x] = h[y];
h[y] = aux;
poz[h[x]] = x;
poz[h[y]] = y;
}
void up(int p)
{
while(p > 1 && v[h[p]] < v[h[p/2]]) {
schimb(p, p/2);
p /= 2;
}
}
void down(int p)
{
int fs = p * 2, fd = p * 2 + 1, pmin = p;
if(fs <= nh && v[h[fs]] <= v[h[pmin]])
pmin = fs;
if(fd <= nh && v[h[fd]] <= v[h[pmin]])
pmin = fd;
if(pmin != p)
{
schimb(p, pmin);
down(pmin);
}
}
int main()
{
int type, nr, p;
in >> nn;
nh = 0;
for(int i=1;i<=nn;i++)
{
in >> type;
if(type == 1) {
in >> v[++n];
poz[n] = ++nh;
h[nh] = n;
up(nh);
} else if(type == 2){
in >> nr;
p = poz[nr];
h[p] = h[nh--];
poz[h[p]] = p;
up(p);
down(p);
} else
out << v[h[1]] << "\n";
}
return 0;
}