Pagini recente » Cod sursa (job #2771020) | Cod sursa (job #2931531) | Cod sursa (job #901621) | Cod sursa (job #1669276) | Cod sursa (job #1357650)
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 200010
int N, L, NR;
int A[maxn], Heap[maxn], Pos[maxn];
void push(int x)
{
while (x/2 && A[Heap[x]]<A[Heap[x/2]])
{
swap(Heap[x], Heap[x/2]);
Pos[Heap[x]] = x;
Pos[Heap[x/2]] = x/2;
x /= 2;
}
}
void pop(int x)
{
int 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;
swap(Heap[x],Heap[y]);
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);
for (i=1; i<=N; i++)
{
scanf("%d ", &cod);
if (cod < 3)
scanf("%d ", &x);
if (cod == 1)
{
L++, NR++;
A[NR] = x;
Heap[L] = NR;
Pos[NR] = L;
push(L);
}
if (cod == 2)
{
A[x] = -1;
push(Pos[x]);
Pos[Heap[1]] = 0;
Heap[1] = Heap[L--];
Pos[Heap[1]] = 1;
if (L>1) pop(1);
}
if (cod == 3) printf("%d\n", A[Heap[1]]);
}
return 0;
}