Pagini recente » Cod sursa (job #2526500) | Cod sursa (job #2773537) | Cod sursa (job #2320277) | Cod sursa (job #315182) | Cod sursa (job #2452915)
#include <fstream>
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
struct {
int x, timp;
}heap[200005];
int cnt, poz, mut, lg, n, timpi[200005];
int main()
{
int op, x;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> op;
if (op == 1) {
cin >> x;
lg++; cnt++;
heap[lg] = {x, cnt};
poz = lg;
while (heap[poz].x < heap[poz / 2].x) {
timpi[heap[poz / 2].timp] = poz;
swap(heap[poz / 2], heap[poz]);
poz /= 2;
}
timpi[cnt] = poz;
} else if (op == 2) {
cin >> x;
heap[timpi[x]] = heap[lg];
timpi[heap[lg].timp] = timpi[x];
lg--;
poz = timpi[x];
mut = 1;
while (mut) {
mut = 0;
if (2 * poz + 1 <= lg && heap[poz].x > heap[2 * poz].x && heap[poz].x > heap[2 * poz + 1].x) {
mut = 1;
if (heap[2 * poz].x < heap[2 * poz + 1].x) {
swap(timpi[heap[poz].timp], timpi[heap[2 * poz].timp]);
swap(heap[poz], heap[2 * poz]);
poz *= 2;
} else {
swap(timpi[heap[poz].timp], timpi[heap[2 * poz + 1].timp]);
swap(heap[poz], heap[2 * poz + 1]);
poz *= 2; poz++;
}
} else if (2 * poz <= lg && heap[poz].x > heap[2 * poz].x) {
mut = 1;
swap(timpi[heap[poz].timp], timpi[heap[2 * poz].timp]);
swap(heap[poz], heap[2 * poz]);
poz *= 2;
} else if (2 * poz + 1 <= lg && heap[poz].x > heap[2 * poz + 1].x) {
mut = 1;
swap(timpi[heap[poz].timp], timpi[heap[2 * poz + 1].timp]);
swap(heap[poz], heap[2 * poz + 1]);
poz *= 2; poz++;
}
}
} else {
cout << heap[1].x << "\n";
}
}
return 0;
}