Pagini recente » Cod sursa (job #1183376) | Cod sursa (job #2912994) | Cod sursa (job #756844) | Cod sursa (job #1556827) | Cod sursa (job #1314976)
#include <fstream>
using namespace std;
ifstream is("heapuri.in");
ofstream os("heapuri.out");
int t, op, a, el, aux;
int n, h[200001], p[200001], nr[200001];
void Insert();
void Delete(int poz);
int main()
{
is >> t;
while ( t-- )
{
is >> op;
if ( op == 3 )
{
os << nr[h[1]] << "\n";
continue;
}
is >> a;
if ( op & 1 )
{
nr[++el] = a;
Insert();
}
else
Delete(p[a]);
}
is.close();
os.close();
return 0;
}
void Insert()
{
p[el] = ++n;
h[n] = el;
int up = n / 2, down = n;
while ( up && nr[h[down]] < nr[h[up]] )
{
aux = h[down];
h[down] = h[up];
h[up] = aux;
p[h[up]] = up;
p[h[down]] = down;
up /= 2;
down /= 2;
}
}
void Delete(int poz)
{
if ( poz == n )
{
--n;
return;
}
p[h[n]] = poz;
h[poz] = h[n--];
int up = poz, down = poz * 2;
while ( down <= n && ( nr[h[up]] > nr[h[down]] || nr[h[up]] > nr[h[down + 1]] ) )
{
if ( down < n && nr[h[down]] > nr[h[down + 1]] )
++down;
aux = h[down];
h[down] = h[up];
h[up] = aux;
p[h[up]] = up;
p[h[down]] = down;
up = down;
down *= 2;
}
}