#include <stdio.h>
#include <stdlib.h>
int whichone(int a, int b)
{
if (a < b)
return 1;
return 2;
}
int max(int a, int b)
{
if (a > b)
return a;
return b;
}
void reorderHeapAdd(int **heap, int index)
{
if (index == 0)
return;
if ((*heap)[(index-1)/2] > (*heap)[index]){
int aux = (*heap)[(index-1)/2];
(*heap)[(index-1)/2] = (*heap)[index];
(*heap)[index] = aux;
reorderHeapAdd(heap, (index-1)/2);
}
}
void addToHeap(int **heap, int *numbers_in_heap, int number)
{
if (*heap == NULL)
{
*numbers_in_heap = 1;
(*heap)[0] = number;
}
else
{
(*numbers_in_heap)++;
//*heap = (int*) realloc(*heap, *numbers_in_heap * sizeof(int));
(*heap)[*numbers_in_heap-1] = number;
reorderHeapAdd(heap,(*numbers_in_heap)-1);
}
}
int findNumber(int *heap, int numbers_in_heap, int index, int number)
{
if (index == 0 && heap[0] == number)
return index;
if (heap[index] == number)
return index;
if (heap[index] > number)
return -1;
if (index*2 + 1 < numbers_in_heap)
{
if (index*2 + 2 < numbers_in_heap)
return max (findNumber (heap, numbers_in_heap, index*2 + 1, number), findNumber (heap, numbers_in_heap, index*2 + 2, number));
else
return findNumber (heap, numbers_in_heap, index*2 + 1, number);
}
return -1;
}
void reorderHeapRemove(int **heap, int index, int numbers_in_heap)
{
if (2 * index + 1 > numbers_in_heap - 1)
return;
if (2 * index + 2 > numbers_in_heap - 1)
{
if ((*heap)[2 * index + 1] < (*heap)[index])
{
int aux = (*heap)[2 * index + 1];
(*heap)[2 * index + 1] = (*heap)[index];
(*heap)[index] = aux;
}
return;
}
else
{
if ( whichone((*heap)[2 * index + 1], (*heap)[2 *index +2]) == 1 )
{
if ((*heap)[2 * index + 1] < (*heap)[index])
{
int aux = (*heap)[2 * index + 1];
(*heap)[2 * index + 1] = (*heap)[index];
(*heap)[index] = aux;
reorderHeapRemove (heap, 2 * index + 1, numbers_in_heap);
}
}
else
{
if ((*heap)[2 * index + 2] < (*heap)[index])
{
int aux = (*heap)[2 * index + 2];
(*heap)[2 * index + 2] = (*heap)[index];
(*heap)[index] = aux;
reorderHeapRemove (heap, 2 * index + 2, numbers_in_heap);
}
}
}
}
void removeFromHeap(int **heap, int *numbers_in_heap, int number)
{
if((*numbers_in_heap)==1 && (*heap)[0]==number)
{
(*numbers_in_heap)--;
(*heap)[0] = 0;
}
else
{
int i = findNumber(*heap, *numbers_in_heap, 0, number);
if (i != -1)
{
(*heap)[i] = (*heap)[*numbers_in_heap - 1];
(*heap)[*numbers_in_heap - 1] = 0;
(*numbers_in_heap)--;
//(*heap) = (int*) realloc((*heap), (*numbers_in_heap) * sizeof(int));
reorderHeapRemove(heap, i, *numbers_in_heap);
}
}
}
int main()
{
FILE *read = fopen("heapuri.in", "r");
FILE *write = fopen("heapuri.out", "w");
int *heap = NULL;
int numbers_in_heap = 0;
int n;
fscanf(read, "%d", &n);
heap = calloc (n, sizeof(int));
int *cronos = calloc(n, sizeof(int));
int cronoscounter = 0;
int comanda, numar;
int ok = 0;
int i = 0;
while (i < n)
{
if (ok == 0)
fscanf(read, "%d%d", &comanda, &numar);
else
{
fscanf(read, "%d", &numar);
ok = 0;
}
if (comanda == 1)
{
addToHeap(&heap, &numbers_in_heap, numar);
cronoscounter++;
cronos[cronoscounter] = numar;
i++;
}
else if(comanda == 2)
{
removeFromHeap(&heap, &numbers_in_heap, cronos[numar]);
i++;
}
else if(comanda == 3)
{
fprintf(write, "%d\n", heap[0]);
comanda = numar;
i++;
ok = 1;
}
}
// if (numbers_in_heap != 0)
// free(heap);
// free(cronos);
fclose(read);
fclose(write);
return 0;
}