Cod sursa(job #2327378)

Utilizator usureluflorianUsurelu Florian-Robert usureluflorian Data 24 ianuarie 2019 17:53:00
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f ("heapuri.in");
ofstream g ("heapuri.out");
const int nmax=2e5+3;
int n,t,nr,v[nmax],usu[nmax],poz[nmax],x,cod;
void push(int x)
{
	while(x/2&&v[usu[x]]<v[usu[x/2]])
	{
		swap(usu[x],usu[x/2]);
		poz[usu[x]]=x;
		poz[usu[x/2]]=x/2;
		x/=2;
	}
}
void pop(int x)
{
	int y=0;
	while(x!=y)
	{
		y=x;
		if(y*2<=t&&v[usu[x]]>v[usu[y*2]]) x=y*2;
		if(y*2+1<=t&&v[usu[x]]>v[usu[y*2+1]]) x=y*2+1;
		swap(usu[x],usu[y]);
		poz[usu[x]]=x;
		poz[usu[y]]=y;
	}
}
int main()
{
    ios::sync_with_stdio(false);
	f>>n;
	for(int i=1;i<=n;++i)
	{
		f>>cod;
		if(cod<3) f>>x;
		if(cod==1)
		{
			++t;
			++nr;
			v[nr]=x;
			usu[t]=nr;
			poz[nr]=t;
			push(t);
		}
		if(cod==2)
		{
			v[x]=-1;
			push(poz[x]);
			poz[usu[1]]=0;
			usu[1]=usu[t--];
			poz[usu[1]]=1;
			if(t>1) pop(1);
		}
		if(cod==3) g<<v[usu[1]]<<'\n';
	}
	return 0;
}