Pagini recente » Cod sursa (job #2262914) | antr7 | Cod sursa (job #124633) | Cod sursa (job #2453025) | Cod sursa (job #1404744)
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <assert.h>
#define maxn 200010
int N, L, NR;
int A[maxn], Heap[maxn], Pos[maxn];
void push(int x)
{
int aux;
while (x / 2 && A[Heap[x]]<A[Heap[x / 2]])
{
aux = Heap[x];
Heap[x] = Heap[x / 2];
Heap[x / 2] = aux;
Pos[Heap[x]] = x;
Pos[Heap[x / 2]] = x / 2;
x /= 2;
}
}
void pop(int x)
{
int aux, y = 0;
while (x != y)
{
y = x;
if (y * 2 <= L && A[Heap[x]]>A[Heap[y * 2]]) x = y * 2;
if (y * 2 + 1 <= L && A[Heap[x]]>A[Heap[y * 2 + 1]]) x = y * 2 + 1;
aux = Heap[x];
Heap[x] = Heap[y];
Heap[y] = aux;
Pos[Heap[x]] = x;
Pos[Heap[y]] = y;
}
}
int main()
{
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
int i, x, cod;
scanf("%d ", &N);
//assert(1 <= N && N <= 200000);
for (i = 1; i <= N; i++)
{
scanf("%d ", &cod);
//assert(1 <= cod && cod <= 3);
if (cod < 3)
{
scanf("%d ", &x);
//assert(1 <= x && x <= 1000000000);
}
if (cod == 1)
{
L++, NR++;
A[NR] = x;
Heap[L] = NR;
Pos[NR] = L;
push(L);
}
if (cod == 2)
{
Pos[Heap[L]] = Pos[x];
Heap[Pos[x]] = Heap[L]; Heap[L--] = 0;
if (Heap[Pos[x]] < Heap[Pos[x] / 2] && Pos[x] / 2 > 0) push(Pos[x]);
else if ((Heap[Pos[x]] > Heap[Pos[x] / 2] && Pos[x] / 2 <= L) || (Heap[Pos[x] / 2 + 1] < Heap[Pos[x]] && Pos[x] / 2 + 1 <= L)) pop(Pos[x]);
}
if (cod == 3) printf("%d\n", A[Heap[1]]);
}
return 0;
}