Pagini recente » Istoria paginii runda/redsnow_1 | Cod sursa (job #2485840) | Cod sursa (job #1111566) | Diferente pentru home intre reviziile 408 si 902 | Cod sursa (job #1473340)
#include <stdio.h>
#define MAX 200005
int h[MAX], added[MAX], loc[MAX], add;
int pos;
inline void swap (int x, int y) {
loc[h[x]] = y;
loc[h[y]] = x;
int temp = h[x];
h[x] = h[y];
h[y] = temp;
}
int push (int x) {
int p = pos;
++pos;
h[p] = x;
added[x] = p;
int aux = (p-1)/2;
while (p && h[aux] > h[p]) {
swap(aux, p);
p = aux;
aux = (aux - 1) / 2;
}
return p;
}
void pop (int x) {
int p = loc[added[x]];
--pos;
swap(p, pos);
int ok = 0;
while (!ok) {
int aux1 = 2 * p + 1;
int aux2 = aux1+1;
if (aux2 < pos) {
int minp = (h[aux1] < h[aux2]) ? aux1 : aux2;
if (h[minp] < h[p]) {
swap(minp, p);
p = minp;
} else {
ok = 1;
}
} else if (aux1 < pos) {
if (h[aux1] < h[p]) {
swap(aux1, p);
p = aux1;
} else {
ok = 1;
}
} else {
ok = 1;
}
}
}
int main(void) {
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int code;
scanf("%d", &code);
if (code == 3) {
printf("%d\n", h[0]);
} else {
int x;
scanf("%d", &x);
if (code == 1) {
added[pos] = x;
loc[x] = push(x);
} else {
pop(x-1);
}
}
/*
printf("test: ");
for (int i = 0; i < pos; ++i) {
printf("%d ", h[i]);
}
printf("\n");
*/
}
fclose(stdin);
fclose(stdout);
return 0;
}