Pagini recente » Cod sursa (job #723800) | Cod sursa (job #2335128) | Cod sursa (job #2188319) | Cod sursa (job #369754) | Cod sursa (job #2746184)
#include <bits/stdc++.h>
using namespace std;
ifstream input("heapuri.in");
ofstream output("heapuri.out");
int main() {
int n;
input >> n;
int op, el, l, nr;
int heap[200001];
int pos[200001];
int a[200001];
for (int i = 0; i < n; ++i) {
input >> op;
if(op != 3)
input >> el;
switch (op) {
case 1: {
// Inserare
++l;
++nr;
a[nr] = el;
heap[l] = nr;
pos[nr] = l;
int k = l;
while (k / 2 && a[heap[k]] < a[heap[k / 2]]) {
swap(heap[k], heap[k / 2]);
pos[heap[k]] = k;
pos[heap[k / 2]] = k / 2;
k = k / 2;
}
break;
}
case 2: {
// Stergere
a[el] = -1;
int k = pos[el];
while (k / 2 && a[heap[k]] < a[heap[k / 2]]) {
swap(heap[k], heap[k / 2]);
pos[heap[k]] = k;
pos[heap[k / 2]] = k / 2;
k = k / 2;
}
pos[heap[1]] = 0;
heap[1] = heap[l--];
pos[heap[1]] = 1;
if (l > 1) {
int k = 1;
int y = 0;
while (k != y) {
y = k;
if ((y * 2) <= l && a[heap[k]] > a[heap[y * 2]])
k = y * 2;
if ((y * 2) + 1 <= l && a[heap[k]] > a[heap[(y * 2) + 1]])
k = (y * 2) + 1;
swap(heap[k], heap[y]);
pos[heap[k]] = k;
pos[heap[y]] = y;
}
}
break;
}
case 3: {
// Minim
output << a[heap[1]] << "\n";
break;
}
}
}
return 0;
}