Pagini recente » Cod sursa (job #704090) | Cod sursa (job #2901047) | Cod sursa (job #2775354) | Cod sursa (job #1273153) | Cod sursa (job #2263347)
#include <bits/stdc++.h>
#define N 200000
using namespace std;
int heap[N + 1];
int pos[N + 1], cr[N + 1];
int k = 0;
void sw( int ps, int ps2 ) {
int aux;
aux = heap[ps];
heap[ps] = heap[ps2];
heap[ps2] = aux;
aux = cr[ps];
cr[ps] = cr[ps2];
cr[ps2] = aux;
aux = pos[cr[ps]];
pos[cr[ps]] = pos[cr[ps2]];
pos[cr[ps2]] = aux;
}
void urca( int ps ) {
while ( ps > 1 && heap[ps] < heap[ps / 2] ) {
sw( ps, ps / 2 );
ps /= 2;
}
}
void coboara( int ps ) {
if ( ps * 2 + 1 <= k && heap[ps] > heap[ps * 2] && heap[ps] > heap[ps * 2 + 1] ) {
if ( heap[ps * 2] < heap[ps * 2 + 1] ) {
sw( ps, ps * 2 );
coboara( ps * 2 );
} else {
sw( ps, ps * 2 + 1 );
coboara( ps * 2 + 1 );
}
} else if ( ps * 2 <= k && heap[ps] > heap[ps * 2] ) {
sw( ps, ps * 2 );
coboara( ps * 2 );
} else if ( ps * 2 + 1 <= k && heap[ps] > heap[ps * 2 + 1] ) {
sw( ps, ps * 2 + 1 );
coboara( ps * 2 + 1 );
}
}
void adauga( int x ) {
heap[k] = x;
urca( k );
}
void sterge( int ps ) {
if ( ps == k ) {
k--;
} else {
sw( ps, k );
k--;
urca( ps );
coboara( ps );
}
}
int main() {
FILE *fin, *fout;
int n, i, j = 1, cer, x;
fin = fopen( "heapuri.in", "r" );
fout = fopen( "heapuri.out", "w" );
fscanf( fin, "%d", &n );
for ( i = 0; i < n; i++ ) {
fscanf( fin, "%d", &cer );
if ( cer == 1 ) {
fscanf( fin, "%d", &x );
k++;
cr[k] = j;
pos[j] = k;
j++;
adauga( x );
} else if ( cer == 2 ) {
fscanf( fin, "%d", &x );
sterge( pos[x] );
} else {
fprintf( fout, "%d\n", heap[1] );
}
}
fclose( fin );
fclose( fout );
return 0;
}