Pagini recente » Cod sursa (job #1695135) | Cod sursa (job #2932741) | Cod sursa (job #2913738) | Cod sursa (job #985830) | Cod sursa (job #2695808)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int qmax = 200000;
int p[qmax+1];
int h[qmax+1], hd[qmax+1];
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 x ) {
int hn = h[0], 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, 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;
++ p[0];
p[p[0]] = y;
++ h[0];
h[h[0]] = y;
hpercolate(h, h[0]);
} else if ( x==2 ) {
int y;
fin >> y;
++hd[0];
hd[hd[0]] = p[y];
hpercolate(hd, hd[0]);
} else {
while ( hd[0]>0 && h[1]==hd[1] ) {
hswap(h, 1, h[0]);
--h[0];
hsift(h, 1);
hswap(hd, 1, hd[0]);
--hd[0];
hsift(hd, 1);
}
fout << h[1] << "\n";
}
}
return 0;
}