Pagini recente » Cod sursa (job #2447162) | Cod sursa (job #2190270) | Cod sursa (job #2579105) | Cod sursa (job #436500)
Cod sursa(job #436500)
#include <cstdio>
#include <algorithm>
using namespace std;
#define NMAX 200001
int N;
int NR = 0;
int V[NMAX];
int L = 0;
int Heap[NMAX];
int P[NMAX];
void PUSH(int x)
{
while(x / 2)
{
if(V[Heap[x / 2]] > V[Heap[x]]){
swap(Heap[x / 2], Heap[x]);
P[Heap[x]] = x;
P[Heap[x / 2]] = x / 2;
x = x / 2;
}
else
break;
}
}
void POP()
{
int x = 1, y;
while(2 * x <= L)
{
y = 2 * x;
if(y + 1 <= L && V[Heap[2 * x + 1]] < V[Heap[2 * x]])
y++;
swap(Heap[y], Heap[x]);
P[Heap[x]] = x;
P[Heap[y]] = y;
x = y;
}
Heap[x] = Heap[L];
P[Heap[x]] = x;
}
void rezolva()
{
scanf("%d",&N);
for(int i = 1 ; i <= N ; i++)
{
int iden, x;
scanf("%d",&iden);
switch(iden){
case(1):{
scanf("%d",&x);
V[++NR] = x;
Heap[++L] = NR;
P[NR] = L;
if(L > 1)
PUSH(L);
break;
}
case(2):{
scanf("%d",&x);
V[x] = -1;
PUSH(P[x]);
POP();
L--;
break;
}
case(3):{
printf("%d\n",V[Heap[1]]);
}
}
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
rezolva();
return 0;
}