Pagini recente » Cod sursa (job #1457435) | Cod sursa (job #1770660) | Cod sursa (job #1687832) | Cod sursa (job #2227395) | Cod sursa (job #1456020)
#include <cstdio>
#include <algorithm>
#define NMAX 200007
using namespace std;
int h[NMAX], v[NMAX], poz[NMAX];
int left(int x)
{
return 2*x;
}
int right(int x)
{
return 2*x+1;
}
int father(int x)
{
return x/2;
}
int top()
{
return v[h[1]];
}
void upheap(int x)
{
while(father(x) > 0 && v[h[x]] < v[h[father(x)]])
{
swap(h[x], h[father(x)]);
swap(poz[h[x]], poz[h[father(x)]]);
x = father(x);
}
}
void downheap(int x)
{
int best = x;
if(left(x) <= h[0])
if(v[h[left(x)]] < v[h[best]]) best = left(x);
if(right(x) <= h[0])
if(v[h[right(x)]] < v[h[best]]) best = right(x);
while(best != x)
{
swap(h[best], h[x]);
swap(poz[h[best]], poz[h[x]]);
x = best;
if(left(x) <= h[0])
if(v[h[left(x)]] < v[h[best]]) best = left(x);
if(right(x) <= h[0])
if(v[h[right(x)]] < v[h[best]]) best = right(x);
}
}
void h_insert(int x)
{
v[0]++;
h[0]++;
v[v[0]] = x;
h[h[0]] = v[0];
poz[v[0]] = h[0];
upheap(h[0]);
}
int h_erase(int x)
{
swap(h[x], h[h[0]]);
swap(poz[h[x]], poz[h[h[0]]]);
h[0]--;
downheap(x);
}
FILE *fin, *fout;
int n, p, k;
int main()
{
fin = freopen("heapuri.in", "r", stdin);
fout = freopen("heapuri.out", "w", stdout);
scanf("%d", &n);
for(int i = 1; i<=n; i++)
{
scanf("%d", &p);
if(p == 1)
{
scanf("%d", &k);
h_insert(k);
}
if(p == 2)
{
scanf("%d", &k);
h_erase(poz[k]);
}
if(p == 3)
{
printf("%d\n", top());
}
}
fclose(fin);
fclose(fout);
return 0;
}