Pagini recente » Cod sursa (job #1830228) | Cod sursa (job #994837) | Cod sursa (job #1183130) | Cod sursa (job #1522533) | Cod sursa (job #251052)
Cod sursa(job #251052)
#include <stdio.h>
#define Nmax 200100
int h[Nmax], a[Nmax], p[Nmax], nr, n;
void up(int x)
{
if (x>1)
{
if (a[h[x]] < a[h[x/2]])
{
int tmp;
tmp = h[x];
h[x] = h[x/2];
h[x/2] = tmp;
p[h[x]] = x;
p[h[x/2]] = x/2;
up(x/2);
}
}
}
void ins(int A)
{
a[++n] = A;
h[++nr] = n;
p[n] = nr;
up(nr);
}
void down(int poz)
{
int min = poz;
if (2*poz <= nr) if (a[h[2*poz]] < a[h[min]]) min = 2*poz;
if (2*poz+1 <= nr) if (a[h[2*poz+1]] < a[h[min]]) min = 2*poz+1;
if (poz!=min)
{
int tmp;
tmp = h[min];
h[min]=h[poz];
h[poz]=tmp;
p[h[min]]=min;
p[h[poz]]=poz;
down(poz);
}
}
void del(int k)
{
a[h[p[k]]] = -1;
up(p[k]);
h[1]=h[--nr];
down(1);
}
void solve()
{
int op;
scanf("%d", &op);
if (op == 1)
{
int A;
scanf("%d", &A);
ins(A);
}
if (op == 2)
{
int A;
scanf("%d", &A);
del(A);
}
if (op == 3)
{
printf("%d\n", a[h[1]]);
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int t;
scanf("%d", &t);
while (t--) solve();
return 0;
}