Pagini recente » Cod sursa (job #3153567) | Cod sursa (job #174537) | Cod sursa (job #1286836) | Cod sursa (job #868628) | Cod sursa (job #755502)
Cod sursa(job #755502)
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
#define nmax 200010
int H[nmax], pos[nmax], v[nmax], N, hp, M;
int father(int K) { return (K >> 1); }
int leftSon(int K) { return (K << 1); }
int rightSon(int K) { return ((K << 1) + 1); }
void swap(int *a, int *b)
{
int aux;
aux = *a;
*a = *b;
*b = aux;
}
void sift(int K)
{
int son;
do
{
son = 0;
if(leftSon(K) <= hp)
{
son = leftSon(K);
if(rightSon(K) <= hp && v[H[rightSon(K)]] < v[H[son]]) son = rightSon(K);
if(v[H[son]] >= v[H[K]]) son = 0;
}
if(son)
{
swap(&pos[H[K]], &pos[H[son]]);
swap(&H[K], &H[son]);
K = son;
}
}while(son);
}
void percolate(int K)
{
while((K > 1) && (v[H[father(K)]] > v[H[K]]))
{
swap(&pos[H[K]], &pos[H[father(K)]]);
swap(&H[K], &H[father(K)]);
K = father(K);
}
}
void Cut(int K)
{
pos[H[hp]] = K;
swap(&H[K], &H[hp]);
hp--;
if((K > 1) && (v[H[K]] < v[H[father(K)]])) percolate(K);
else sift(K);
}
void Insert(int x)
{
hp++; N++;
v[N] = x;
H[hp] = N;
pos[N] = hp;
percolate(hp);
}
int MaxValue() { return v[H[1]]; }
int main()
{
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
int type, i, x;
scanf("%i", &M);
while(M --)
{
scanf("%i", &type);
if(type == 1)
{
scanf("%i", &x);
Insert(x);
}
if(type == 2)
{
scanf("%i", &x);
Cut(pos[x]);
}
if(type == 3)
{
printf("%i\n", MaxValue());
}
}
scanf("%i", &i);
return 0;
}