Pagini recente » Cod sursa (job #1188815) | Cod sursa (job #1327684) | Cod sursa (job #1653888) | Cod sursa (job #74122) | Cod sursa (job #822009)
Cod sursa(job #822009)
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 200010;
int H[MAXN], A[MAXN], Pos[MAXN];
int m, x, N, szHeap;
void push(int x) {
while(x / 2 && A[H[x / 2]] > A[H[x]]) {
Pos[H[x / 2]] = x; Pos[H[x]] = x / 2;
swap(H[x / 2], H[x]);
x /= 2;
}
}
void pop(int x) {
int y = 0;
while(x != y) {
y = x;
if(2 * x <= szHeap && A[H[x]] > A[H[2 * x]]) y = 2 * x;
if(2 * x + 1 <= szHeap && A[H[y]] > A[2 * x + 1]) y = 2 * x + 1;
Pos[H[x]] = y; Pos[H[y]] = x;
swap(H[x], H[y]);
x = y;
}
}
int main() {
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
int tip;
for( scanf("%d", &m); m--; ) {
scanf("%d", &tip);
if(tip == 1) {
//adauga in heap
scanf("%d", &x);
A[++N] = x;
H[++szHeap] = N;
Pos[N] = szHeap;
push(szHeap);
}
if(tip == 2) {
//sterge
scanf("%d", &x);
A[x] = -1; push(Pos[x]);
Pos[H[szHeap]] = 1; Pos[H[1]] = 0;
swap(H[1], H[szHeap--]);
pop(1);
Pos[H[1]] = 1;
}
if(tip == 3) //be the bauss
printf("%d\n", A[H[1]]);
}
return 0;
}