Pagini recente » Cod sursa (job #960200) | Cod sursa (job #1493271) | Cod sursa (job #1982815) | Cod sursa (job #2381302) | Cod sursa (job #1152772)
/*
Keep It Simple!
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include<stdio.h>
#define MaxN 200001
int A[MaxN], N, Heap[MaxN], Number_Of_Heap_Elements, Number_Of_Elements;
int InHeapLocation[MaxN];
void GoDown(int node)
{
int sw = 1, x = -1;
while (node < Number_Of_Heap_Elements && sw)
{
x = node, sw = 0;
if (2 * node <= Number_Of_Heap_Elements && A[Heap[node]] > A[Heap[2 * node]]) x = 2 * node, sw = 1;
if (2 * node + 1 <= Number_Of_Heap_Elements && A[Heap[2 * node]] > A[Heap[2 * node + 1]] && A[Heap[node]] > A[Heap[2 * node + 1]]) x = 2 * node + 1, sw = 1;
int aux = Heap[x];
Heap[x] = Heap[node];
Heap[node] = aux;
InHeapLocation[Heap[node]] = node;
InHeapLocation[Heap[x]] = x;
node = x;
}
}
void GoUp(int node)
{
while (node / 2 > 0 && A[Heap[node]] < A[Heap[node / 2]])
{
int aux = Heap[node / 2];
Heap[node / 2] = Heap[node];
Heap[node] = aux;
InHeapLocation[Heap[node]] = node;
InHeapLocation[Heap[node / 2]] = node / 2;
node /= 2;
}
GoDown(node);
}
void AddToHeap(int position, int value)
{
Heap[++Number_Of_Heap_Elements] = position;
A[position] = value;
InHeapLocation[position] = Number_Of_Heap_Elements;
GoUp(Number_Of_Heap_Elements);
}
void Remove(int who)
{
Heap[InHeapLocation[who]] = -1;
GoUp(InHeapLocation[who]);
Heap[1] = Heap[Number_Of_Heap_Elements];
InHeapLocation[Heap[Number_Of_Heap_Elements]] = 1;
Number_Of_Heap_Elements--;
InHeapLocation[who] = -1;
GoDown(1);
}
int main()
{
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
scanf("%d", &N);
int type, x;
for (int i = 1; i <= N; i++)
{
scanf("%d", &type);
if (type == 3)
printf("%d\n", A[Heap[1]]);
else if (type == 1)
{
scanf("%d", &x);
AddToHeap(++Number_Of_Elements, x);
}
else if (type == 2)
{
scanf("%d", &x);
Remove(x);
}
}
}