Pagini recente » Cod sursa (job #315712) | Cod sursa (job #2099091) | Cod sursa (job #2082192) | Cod sursa (job #354773) | Cod sursa (job #1029060)
#include <cstdio>
#define NMAX 200005
int n, cod, x, heap[NMAX], poz_heap[NMAX]; /// numarul de operatii, codul operatiei, val., heapul, poz in heap a elementului adaugat
int nr;
void change_variable(int &a, int &b)
{
int aux = b;
b = a;
a = aux;
}
void push(int x)
{
heap[++nr] = x;
poz_heap[nr] = nr;
int aux_nr = nr;
while(heap[aux_nr/2] != 0 && x < heap[aux_nr/2])
{
change_variable(heap[aux_nr], heap[aux_nr/2]);
change_variable(poz_heap[aux_nr], poz_heap[aux_nr/2]);
aux_nr /= 2;
}
}
void pop(int x)
{
heap[heap_poz[x]] = heap[heap_poz[nr]];
heap_poz[x] = heap_poz[nr];
int aux_nr = heap_poz[x];
while(heap[aux_nr/2] != 0 && x < heap[aux_nr/2])
{
change_variable(heap[aux_nr], heap[aux_nr/2]);
change_variable(poz_heap[aux_nr], poz_heap[aux_nr/2]);
aux_nr /= 2;
}
/* while(heap[aux_nr*2] != 0 && x > heap[aux_nr*2])
{
change_variable(heap[aux_nr], heap[aux_nr/2]);
change_variable(poz_heap[aux_nr], poz_heap[aux_nr/2]);
aux_nr /= 2;
}*/
}
int main()
{
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out","w", stdout);
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%d", &cod);
switch(cod)
{
case 1:
scanf("%d", &x);
push(x);
break;
case 2:
scanf("%d", &x);
pop(x);
break;
case 3:
printf("%d", heap[1]);
break;
}
}
return 0;
}