Pagini recente » Cod sursa (job #2283240) | Monitorul de evaluare | Cod sursa (job #663050) | Cod sursa (job #2944841) | Cod sursa (job #822235)
Cod sursa(job #822235)
#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 * y <= szHeap && A[H[x]] > A[H[2 * y]]) x = 2 * y;
if(2 * y + 1 <= szHeap && A[H[x]] > A[H[2 * y + 1]]) x = 2 * y + 1;
Pos[H[x]] = y; Pos[H[y]] = x;
swap(H[x], H[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[1]] = 0;
H[1] = H[szHeap--];
Pos[H[1]] = 1;
if(szHeap > 1) pop(1);
}
if(tip == 3) //be the bauss
printf("%d\n", A[H[1]]);
}
return 0;
}