Pagini recente » Cod sursa (job #157636) | Cod sursa (job #679911) | Cod sursa (job #366134) | Cod sursa (job #1903067) | Cod sursa (job #236330)
Cod sursa(job #236330)
#include <cstdio>
using namespace std;
#define MAX 300000
int H[MAX], V[MAX], P[MAX];
int N, NH, x, c, Nr;
void swap( int &x, int &y ) { int a=x; x=y; y=a; }
void HPush( int x ) {
if ( x<=0 ) return ;
int t = (x-1)/2;
if ( V[H[t]] > V[H[x]] ) {
swap( H[t], H[x] );
P[H[t]] = t;
P[H[x]] = x;
HPush( t );
}
}
void HPop( int x ) {
if ( 2*x+1 > NH || x > NH ) return;
int f = 2*x+1;
if ( 2*x+2 <= NH && V[H[2*x+1]] > V[H[2*x+2]] )
f = 2*x+2;
swap( H[x], H[f] );
P[H[x]] = x;
P[H[f]] = f;
HPop(f);
}
void HDbg( int x, int y ) {
fprintf(stderr, "%d / %d :", x, y);
for ( int i=0; i<=NH; ++i )
fprintf(stderr, "%3d ", V[H[i]] );
fprintf(stderr, "\n");
}
int main() {
freopen( "heapuri.in", "r", stdin );
freopen( "heapuri.out", "w", stdout );
scanf("%d", &N);
NH = -1, Nr = -1;
while ( N-- ) {
scanf("%d", &c);
switch ( c ) {
case 1:
scanf("%d", &x);
P[++Nr] = ++NH;
H[NH] = Nr;
V[Nr] = x;
HPush( NH );
// HDbg(1,x);
break;
case 2:
scanf("%d", &x);
--x;
HPop( P[x] );
if ( P[x] != NH ) {
P[H[NH]] = P[x];
swap( H[P[x]], H[NH] );
HPush( P[x] );
}
P[x] = -1;
NH --;
// HDbg(2,x);
break;
case 3:
printf("%d\n", V[H[0]]);
// fprintf(stderr, "3\n");
break;
}
}
return 0;
}