Cod sursa(job #1487239)

Utilizator PetruZZatic Petru PetruZ Data 16 septembrie 2015 15:55:51
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <fstream>

#define nmax 200001

int N, l, nr, a[nmax], h[nmax], p[nmax];

using namespace std;

void push(int x)
{
	int aux;
	
	while(x/2 && a[h[x/2]]>a[h[x]])
	{
		aux=h[x];
		h[x]=h[x/2];
		h[x/2]=aux;
		
		p[h[x]]=x;
		p[h[x/2]]=x/2;
		x/=2;
	}
}

void pop(int x)
{
	int aux, y(0);
	
	while(x!=y)
	{
		y=x;
		if(y*2<=l && a[h[x]]>a[h[y*2]]) x=y*2;
		if(y*2+1<=l && a[h[x]]>a[h[y*2+1]]) x=y*2+1;
		
		aux=h[x];
		h[x]=h[y];
		h[y]=aux;
		
		p[h[x]]=x;
		p[h[y]]=y;
	}	
}

int main()
{
	ifstream cin("heapuri.in");
    ofstream cout("heapuri.out");
    
    ios_base::sync_with_stdio(0);
    
    int ist,x;
    
    cin >> N;
    
    for(int i(0); i<N; i++)
    {
    	cin >> ist;
    	
    	if(ist==1)
    	{
    		cin >> x;
    		l++;
    		nr++;
    		a[nr]=x;
    		h[l]=nr;
    		p[nr]=l;
    		push(l);
		}
		
		if(ist==2)
    	{
    		cin >> x;
    		a[x]=-1;
    		push(p[x]);
    		p[h[1]]=0;
    		h[1]=h[l--];
    		p[h[1]]=1;
    		if(l>1) pop(1);
		}
		
		if(ist==3) cout << a[h[1]]<< "\n";
    	
    	
	}
	
	
	
	
return 0;
}