Cod sursa(job #2295903)

Utilizator marian013Giugioiu Marian Constantin marian013 Data 4 decembrie 2018 00:22:20
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <stdio.h>
#include <algorithm>
using namespace std;
int v[200010], heap[200010], pos[200010];
int n,l,nr;
void push(int x){
    while(x/2 && v[heap[x]] < v[heap[x/2]])
    {
		swap(heap[x], heap[x/2]);
		pos[heap[x]] = x;
		pos[heap[x/2]] = x/2;
		x /= 2;
    }
}
void pop(int x){
	int 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;
		swap(heap[y], heap[x]);
		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;
}