Cod sursa(job #1732706)

Utilizator andreitulusAndrei andreitulus Data 22 iulie 2016 13:28:08
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include<fstream>
#include<stdio.h>
#define nmax 200004
using namespace std;

int poz[nmax], heap[nmax], v[nmax], n, nh, nr;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

// x[i] --> Heap care retine pozitia elementului i din vectorul v
// v[i] --> al i-lea element
// poz[i] --> pozitia elementului i in Heap



void swap(int i, int j)
{
	int aux;

	aux = heap[i];
	heap[i] = heap[j];
	heap[j] = aux;

	poz[heap[i]] = i;
	poz[heap[j]] = j;
}




void heapup(int k)
{
	int f;

	if (k <= 1)
		return;

	f = k / 2;

	if (v[heap[k]] < v[heap[f]])
	{
		swap(f, k);
		heapup(f);
	}
}




void heapdw(int k)
{
	int i = 2 * k;

	if (i <= nh)
	{

		if (i + 1 <= nh && v[heap[i + 1]] < v[heap[i]])
			i++;

		if (v[heap[i]] < v[heap[k]])
		{
			swap(i, k);
			heapdw(i);
		}
	}
}




void add(int a)
{
	nr++; v[nr] = a;
	nh++; heap[nh] = nr;
	poz[nr] = nh;
	heapup(nh);
}




void del(int a)
{
	int p;
	p = poz[a];
	swap(p, nh);
	nh--;
	if (p < nh)
		heapdw(p);
}





void solve()
{
	int i, q, w, p;

	fin >> n;

	for (i = 1; i <= n; i++)
	{
		fin >> q >> w;

		if (q == 1)
		{
			add(w);
		}
		else
		
		if (q == 2)
		{
			del(w);
		}
		else

		if (q == 3)
		{
			fout << v[heap[1]] << "\n";
		}
	}

}





int main()
{
	solve();

	fin.close();
	fout.close();

	return 0;
}