Pagini recente » Cod sursa (job #803162) | Cod sursa (job #3249278) | Cod sursa (job #1804886) | Cod sursa (job #2238701) | Cod sursa (job #1324092)
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = 2000000;
int h[2*MAXN + 10], N = 0, x, nr, v[2*MAXN+10], order[2*MAXN+10]
;
int leftSon(int k)
{
return 2*k;
}
int rightSon(int k)
{
return 2*k+1;
}
int father(int k)
{
return k/2;
}
void cerne(int n, int k)
{
while(1)
{
int nextNode;
if( 2*k > N )
return;
if( 2*k == N )
nextNode = 2*k;
else
{
if( h[leftSon(k)] <= h[rightSon(k)] )
nextNode = leftSon(k);
else nextNode = rightSon(k);
}
if( h[k] <= h[nextNode] )
return;
swap( h[k], h[nextNode] );
swap( v[k], v[nextNode] );
order[ v[k] ] = k;
order[ v[nextNode] ] = nextNode;
k = nextNode;
}
}
void promoveaza(int n, int k)
{
while( k > 1 && h[ father(k) ] > h[k] )
{
swap( h[ father(k) ], h[k] );
swap( v[ father(k) ], v[k] );
order[ v[ father(k) ] ] = father(k);
order[ v[k] ] = k;
//v[nr] = father(k);
//v[ father(k) ] = k;
k = father(k);
}
}
void adaugaNod(int &N, int value)
{
h[++N] = value;
v[N] = ++nr;
order[ v[N] ] = N;
promoveaza(N+1,N);
}
void sterge(int &N, int k)
{
h[k] = h[N];
v[k] = v[N];
order[ v[k] ] = k;
N--;
promoveaza(N,k);
cerne(N,k);
}
void citire()
{
freopen("heapuri.in","r",stdin);
scanf("%d",&N);
for(int i = 1; i <= N; i++)
scanf("%d",&h[i]);
}
void buildHeap()
{
for(int i = N/2; i >= 1; i--)
cerne(N,i);
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int q;
scanf("%d",&q);
for(int i = 1; i <= q; i++)
{
int caz;
scanf("%d",&caz);
if( caz == 1 )
{
int x;
scanf("%d",&x);
adaugaNod(N,x);
}
if( caz == 2 )
{
int x;
scanf("%d",&x);
sterge(N,order[x]);
}
if( caz == 3 )
printf("%d\n",h[1]);
}
return 0;
}