Pagini recente » Cod sursa (job #2971745) | Cod sursa (job #2872466) | Cod sursa (job #1871312) | Cod sursa (job #1693941) | Cod sursa (job #559567)
Cod sursa(job #559567)
#include <algorithm>
#include <fstream>
using namespace std;
const char Input[] = "heapuri.in";
const char Output[] = "heapuri.out";
const int Dim = 200001;
int N;
int h[Dim], m[Dim], p[Dim];
void UpH( int nod ) {
int aux;
while( nod > 1 ) {
aux = nod >> 1;
if( m[h[nod]] < m[h[aux]] ) {
swap( h[nod], h[aux] );
p[h[nod]] = nod;
p[h[aux]] = aux;
nod = aux;
}
else
return;
}
}
void DownH( int nod ) {
int aux;
while( nod < h[0] ) {
if( (nod << 1) <= h[0] ) {
aux = nod << 1;
if( m[h[aux + 1]] < m[h[aux]] )
++aux;
}
else
return;
if( m[h[nod]] > m[h[aux]] ) {
swap( h[nod], h[aux] );
p[h[nod]] = nod;
p[h[aux]] = aux;
nod = aux;
}
else
return;
}
}
void HPush( int x ) {
h[++h[0]] = x;
p[x] = h[0];
UpH( h[0] );
}
void HPop( int nod ) {
h[nod] = h[h[0]--];
p[h[nod]] = nod;
DownH( nod );
}
int main() {
ifstream fin( Input );
ofstream fout( Output );
int x, tp;
fin >> N;
while( N-- ) {
fin >> tp;
if( tp == 1 ) {
fin >> x;
m[++m[0]] = x;
HPush( m[0] );
}
else if( tp == 2 ) {
fin >> x;
HPop( p[x] );
}
else
fout << m[h[1]] << "\n";
}
fin.close();
fout.close();
return 0;
}