Cod sursa(job #2287427)

Utilizator marian013Giugioiu Marian Constantin marian013 Data 21 noiembrie 2018 21:10:29
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <stdio.h>
using namespace std;
int v[200010], heap[200010], pos[200010];
int n,l,nr;
void push(int x){
    int aux;
    while(x/2 && v[heap[x]]<v[heap[x/2]])
    {
        aux = heap[x];
		heap[x] = heap[x/2];
		heap[x/2] = aux;
		pos[heap[x]] = x;
		pos[heap[x/2]] = x/2;
		x /= 2;
    }
}
void pop(int x)
{
	int aux, y=0;

	while(x!=y)
	{
		y=x;

		if(y*2<=l && v[heap[x]]>v[heap[y*2]])
           x = y*2;
		if(y*2+1<=l && v[heap[x]]>v[heap[y*2+1]])
		   x = y*2+1;
		aux = heap[x];
		heap[x] = heap[y];
		heap[y] = aux;

		pos[heap[x]] = x;
		pos[heap[y]] = y;
	}
}
int main()
{
    freopen("heapuri.in", "r", stdin);
	freopen("heapuri.out", "w", stdout);
	int c,x,i;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
    {
        scanf("%d",&c);
        if(c<3)
            scanf("%d",&x);
        if (c==1)
		{
			l++, nr++;
			v[nr] = x;
			heap[l] = nr;
			pos[nr] = l;

			push(l);
		}
		if (c==2)
		{
			v[x] = -1;
			push(pos[x]);

			pos[heap[1]] = 0;
			heap[1] = heap[l--];
			pos[heap[1]] = 1;
			if (l>1) pop(1);
		}
		if (c==3) printf("%d\n", v[heap[1]]);
    }
    return 0;
}