Cod sursa(job #2859657)

Utilizator ciobyCiobanu Vlasie cioby Data 1 martie 2022 18:31:25
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include    <iostream>
#include    <fstream>
#define nmax 350000
using namespace std;

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


int heap[nmax];
int val[nmax];
int poz[nmax];
int n, l;

void push(int x)
{
	int tata = x / 2;
	if (tata && val[heap[x]] < val[heap[tata]])
	{
		swap(heap[x], heap[tata]);
		poz[heap[x]] = x;
		poz[heap[tata]] = tata;
		push(tata);
	}
}

void pop(int x)
{
	int fiu = x * 2;
	if (fiu + 1 <= n && val[heap[fiu + 1]] < val[heap[fiu]])
	{
		fiu++;
	}
	if (fiu <= n && val[heap[fiu]] < val[heap[x]]) {
		swap(heap[fiu], heap[x]);
		poz[heap[x]] = x;
		poz[heap[fiu]] = fiu;
		pop(fiu);
	}
}

void citire()
{
	int q, x, y;
	fin >> q;
	for (int i=1;i<=q;i++)
	{
		fin >> x;
		if (x == 1)
		{
			int y;
			fin >> y;
			n++;
			l++;
			val[n] = y;
			heap[l] = n;
			poz[n] = l;
			push(y);
		}
		if (x == 2)
		{
			int y;
			fin >> y;
			//push(poz[y]);
			poz[heap[1]] = 0;
			heap[1] = heap[l];
			l--;
			poz[heap[1]] = 1;
			if (l > 1) pop(1);
		}
		if (x == 3) {
			fout << val[heap[1]] << '\n';
		}
	}
}

int main()
{
	citire();
}