Pagini recente » Cod sursa (job #800131) | Cod sursa (job #2167313) | Cod sursa (job #908900) | Cod sursa (job #711108) | Cod sursa (job #2761107)
//https://www.geeksforgeeks.org/heap-data-structure/
#include <bits/stdc++.h>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
long long x, y, n, m, p[100000], nr=1;
struct heap
{
long sub, poz;
}h[100];
void coborare(long long k)
{
long long fiu = k;
if(2 * k <= n)
{
if(h[fiu].sub > h[2 * k].sub)
fiu = 2 * k;
if(2 * k + 1 <= n && h[fiu].sub > h[2 * k + 1].sub)
fiu = 2 * k + 1;
if(fiu != k)
{
p[h[fiu].poz] = k;
p[h[k].poz] = fiu;
heap z = h[fiu];
h[fiu] = h[k];
h[k] = z;
coborare(fiu);
}
}
}
void urcare(long long k)
{
long long fiu = k;
if(k > 1 && h[fiu].sub < h[k / 2].sub)
{
p[h[fiu].poz] = k / 2;
p[h[k / 2].poz] = fiu;
heap z = h[fiu];
h[fiu] = h[k / 2];
h[k / 2] = z;
fiu = k / 2;
urcare(fiu);
}
}
void inserare(long long y)
{
h[++n].sub = y;
h[n].poz = nr;
nr++;
p[h[n].poz] = nr;
urcare(n);
}
void stergere(long long y)
{
long k = p[y];
h[k] = h[n];
p[h[n].poz] = k;
n--;
if(k > 1 && h[k].sub < h[k / 2].sub)
urcare(k);
else
coborare(k);
}
void functie()
{
long long i;
in >> m;
n = 0;
for(i = 1; i <= m; i++)
{
in >> x;
if(x == 1)
{
in >> y;
inserare(y);
}
else if(x == 2)
{
in >> y;
stergere(y);
}
else if(x == 3)
out << h[1].sub << "\n";
}
}
int main()
{
functie();
in.close();
out.close();
return 0;
}