Cod sursa(job #2745115)

Utilizator raresmocanuRares Mihai Mocanu raresmocanu Data 25 aprilie 2021 21:40:39
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>
using namespace std;


int A[200010], heap[200010], Pos[200010];
    int n,c, L, NR;

void push(int x)
{

	while (x/2 && A[heap[x]]<A[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 && A[heap[x]]>A[heap[y*2]]) x = y*2;
		if (y*2+1<=L && A[heap[x]]>A[heap[y*2+1]]) x = y*2+1;

        swap(heap[x],heap[y]);
		Pos[heap[x]] = x;
		Pos[heap[y]] = y;
	}
}


int main()
{
    ifstream fin("heapuri.in");
    ofstream fout("heapuri.out");
    fin>>n;
    int x;
    for(int i=0;i<n;i++)
    {
        fin>>c;
        if(c==3) fout<<A[heap[1]]<<'\n';
        else{
        fin>>x;
        if(c==2)
        {
            A[x]=-1;
        push(Pos[x]);;
        Pos[heap[1]]=0;
        heap[1]=heap[L--];
        Pos[heap[1]]=1;
        if(L>1)pop(1);
        }
        if(c==1)
        {
            L++;NR++;
            A[NR]=x;
            heap[L]=NR;
            Pos[NR] = L;
			push(L);
        }
        }
    }
    return 0;
}