Pagini recente » Cod sursa (job #3227275) | Cod sursa (job #2395961) | Cod sursa (job #1056182) | Cod sursa (job #496298) | Cod sursa (job #2695799)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int qmax = 200000;
int p[qmax+1], pn;
int h[qmax+1], hn, hd[qmax+1], hdn;
void hswap ( int *h, int x, int y) {
int aux = h[x];
h[x] = h[y];
h[y] = aux;
return;
}
void hpercolate ( int *h, int x) {
int xf = x/2;
if ( xf!=0 && h[xf]>h[x] ) {
hswap(h, x, xf);
hpercolate(h, xf);
}
return;
}
void hsift( int *h, int hn, int x ) {
int xs = x*2;
if ( xs<=hn ) {
if ( xs+1<=hn && h[xs+1]<h[xs] ) {
++ xs;
}
if ( h[xs]<h[x] ) {
hswap(h, x, xs);
hsift(h, hn, xs);
}
}
return;
}
int main( ) {
int q;
fin >> q;
for ( int qi = 1; qi<=q; ++ qi ) {
int x;
fin >> x;
if ( x==1 ) {
int y;
fin >> y;
++ pn;
p[pn] = y;
++ hn;
h[hn] = y;
hpercolate(h, hn);
} else if ( x==2 ) {
int y;
fin >> y;
++hdn;
hd[hdn] = p[y];
hpercolate(hd, hdn);
} else {
while ( hdn>0 && h[1]==hd[1] ) {
hswap(h, 1, hn);
--hn;
hsift(h, hn, 1);
hswap(hd, 1, hdn);
--hdn;
hsift(hd, hdn, 1);
}
fout << h[1] << "\n";
}
}
return 0;
}