Pagini recente » Cod sursa (job #2334737) | Cod sursa (job #1713204) | Cod sursa (job #26219) | Cod sursa (job #160479) | Cod sursa (job #2437903)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int inf = 1000000005;
int heap[200005], v[200005], v2[200005], poz, n;
void inserez(int x){
poz++;
n++;
heap[poz] = x;
v[n] = poz;
v2[poz] = n;
int p = poz;
while (heap[p] < heap[p / 2]){
int aux = v2[p / 2];
v2[p / 2] = v2[p];
v2[p] = aux;
v[v2[p]] = p;
v[v2[p / 2]] = p / 2;
swap(heap[p], heap[p / 2]);
p /= 2;
}
}
void sterg(int x){
int nod = v[x];
v2[nod] = n;
v[v2[nod]] = nod;
heap[nod] = heap[poz];
heap[poz] = inf;
v[poz] = 0;
v2[poz] = 0;
poz--;
int p = nod;
while (2 * p < poz){
if (heap[2 * p] < heap[2 * p + 1]){
int aux = v2[p];
v2[p] = v2[2 * p];
v2[2 * p] = aux;
v[v2[2 * p]] = 2 * p;
v[v2[p]] = p;
swap(heap[p], heap[2 * p]);
p = 2 * p;
}
else{
int aux = v2[p];
v2[p] = v2[2 * p + 1];
v2[2 * p + 1] = aux;
v[v2[2 * p + 1]] = 2 * p + 1;
v[v2[p]] = p;
swap(heap[p], heap[2 * p + 1]);
p = 2 * p + 1;
}
}
poz--;
}
int main()
{
int nr;
fin >> nr;
for (int i = 1; i <= nr; i++){
int cod, x;
fin >> cod;
if (cod == 1){
fin >> x;
inserez(x);
}
else if (cod == 2){
fin >> x;
sterg(x);
}
else
fout << heap[1] << '\n';
//cout << v[1] << ' ' << v[2] << ' ' << v[3] << ' ' << v[4] << ' ' << v[5] << '\n';
//cout << heap[1] << ' ' << heap[2] << ' ' << heap[3] << ' ' << heap[4] << ' ' << heap[5] << '\n';
//cout << '\n';
}
return 0;
}