Pagini recente » Cod sursa (job #289195) | Cod sursa (job #288611) | Cod sursa (job #791278) | Cod sursa (job #2768204) | Cod sursa (job #715782)
Cod sursa(job #715782)
#include <cstdio>
#include <string.h>
#define nMax 200010
using namespace std;
int n;
int heap[2 * nMax];
int poz[nMax];
int pozInHeap[nMax];
void swap (int &a, int &b){
int aux = a;
a = b;
b = aux;
}
void adaugaHeap (int x, int pozitie)
{
heap[++ n] = x;
poz[pozitie] = n;
pozInHeap[n] = pozitie;
int y = n;
while (heap[y >> 1] > heap[y]){
swap (heap[y >> 1], heap[y]);
swap (poz[pozInHeap[y >> 1]], poz[pozitie]);
swap (pozInHeap[y >> 1], pozInHeap[y]);
y >>= 1;
}
}
void stergeHeap (int pozitie)
{
swap (heap[n], heap[poz[pozitie]]);
int ceva = poz[pozitie];
int altceva = pozInHeap[n];
swap (poz[pozitie], poz[pozInHeap[n]]);
pozInHeap[ceva] = altceva;
heap[n] = 999999;
int ok = 1;
int y = ceva;
int min;
while (ok){
ok = 0;
min = y << 1;
if (heap[min] > heap[min + 1]){
min ++;
}
if (heap[y] > heap[min]){
swap (heap[y], heap[min]);
swap (poz[y], poz[min]);
swap (pozInHeap[y], pozInHeap[min]);
ok = 1;
y = min;
}
}
n --;
}
void citire()
{
int N = nMax * 2;
for (int i = 0; i < N; ++ i){
heap[i] = 999999;
}
heap[0] = -999999999;
int k;
scanf ("%d", &k);
int i = 1;
while (k -- ){
int caz;
scanf ("%d", &caz);
if (caz == 3){
printf ("%d\n", heap[1]);
continue;
}
int x;
scanf ("%d", &x);
if (caz == 1){
adaugaHeap (x, i ++);
continue;
}
stergeHeap (x);
}
}
int main()
{
freopen ("heap.in", "r", stdin);
freopen ("heap.out", "w", stdout);
citire();
return 0;
}