Pagini recente » Cod sursa (job #2982747) | Cod sursa (job #1476414)
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int NMAX = 200505;
int ops, A[NMAX], r, pos[NMAX], H[NMAX], n;
void swapNodes(int x, int y) {
swap(H[x], H[y]);
swap(pos[H[x]], pos[H[y]]);
}
void insertHeap(int p) {
H[++n] = p; pos[p] = n;
p = n;
while (p > 1 && A[H[p / 2]] > A[H[p]]) {
swapNodes(p, p / 2);
p /= 2;
}
}
void deleteHeap(int p) {
p = pos[p];
swapNodes(p, n); n--;
while (p * 2 <= n) {
int bestP = p * 2;
if (p * 2 + 1 <= n && A[H[p * 2 + 1]] < A[H[bestP]])
bestP = p * 2 + 1;
if (A[H[p]] > A[H[bestP]]) {
swapNodes(p, bestP);
p = bestP;
} else {
break ;
}
}
}
void solve() {
scanf("%d", &ops);
int type, x;
while (ops--) {
scanf("%d", &type);
switch (type) {
case 1: {
scanf("%d", &x);
A[++r] = x;
insertHeap(r);
break;
}
case 2: {
scanf("%d", &x);
deleteHeap(x);
break;
}
case 3: {
printf("%d\n", A[H[1]]);
break;
}
}
}
}
int main() {
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
solve();
return 0;
}