Pagini recente » Cod sursa (job #499125) | Cod sursa (job #31423) | Cod sursa (job #2580788) | Cod sursa (job #350505) | Cod sursa (job #2642383)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
const int N_MAX = 2e5;
int g [N_MAX + 3], poz [N_MAX + 3], H [N_MAX + 3];
int k, nr, t, type, x;
void Swap_X (int a, int b) {
swap (g [a], g [b]);
poz [g [a]] = a; poz [g [b]] = b;
}
void up (int nod) {
while (nod != 1 && H [g [nod]] < H [g [nod / 2]]) {
Swap_X (nod, nod / 2);
nod = nod / 2;
}
}
void func (int nod) {int nr = nod;
if (2 * nod <= k && H [g [2 * nod]] < H [g [nr]])nr = 2 * nod;
if (2 * nod + 1 <= k && H [g [2 * nod + 1]] < H [g [nr]])nr = 2 * nod + 1;
if (nr != nod)Swap_X (nod, nr), func (nr);
}
void Erase_X (int X){
if (X < k){
g [X] = g [k --], poz [g [X]] = X;
func (X), up (X);
}
else k --;
}
void add (int X){
g [++ k] = X, poz [X] = k, up (k);
}
int main (){
fin >> t;
for (int i = 0; i < t; i ++){
fin >> type;
if (type == 1)fin >> x, H [++ nr] = x, add (nr);
else if (type == 2)fin >> x, Erase_X (poz [x]);
else fout << H [g [1]] << '\n';
}
return 0;
}