Cod sursa(job #1414261)

Utilizator darrenRares Buhai darren Data 2 aprilie 2015 14:27:25
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <cstring>
#include <fstream>
#include <algorithm>

using namespace std;

int N;
int heap[200002], nod[200002], where[200002];

void add(int val, int times)
{
	heap[++heap[0]] = val;
	nod[heap[0]] = times;
	where[times] = heap[0];
	
	int now = heap[0], nxt = now / 2;
	while (nxt >= 1)
	{
		if (heap[now] < heap[nxt])
		{
			swap(heap[now], heap[nxt]);
			swap(nod[now], nod[nxt]);
			where[nod[now]] = now;
			where[nod[nxt]] = nxt;
			
			now = nxt;
			nxt /= 2;
		}
		else
			break;
	}
}
void rem(int val)
{
	swap(heap[where[val]], heap[heap[0]]);
	swap(nod[where[val]], nod[heap[0]]);
	--heap[0];
	
	where[nod[where[val]]] = where[val];
	
	int now = where[val], nxt = now * 2;
	while (nxt <= heap[0])
	{
		if (nxt < heap[0] && heap[nxt] > heap[nxt + 1])
			++nxt;
		if (heap[now] > heap[nxt])
		{
			swap(heap[now], heap[nxt]);
			swap(nod[now], nod[nxt]);
			where[nod[now]] = now;
			where[nod[nxt]] = nxt;
			
			now = nxt;
			nxt *= 2;
		}
		else
			break;
	}
}

int main()
{
	ifstream fin("heapuri.in");
	ofstream fout("heapuri.out");
	
	fin >> N;
	
	int times = 0;
	for (int i = 1, type; i <= N; ++i)
	{
		fin >> type;
		if (type == 1)
		{
			++times;
			
			int v;
			fin >> v;
			
			add(v, times);
		}
		else if (type == 2)
		{
			int t;
			fin >> t;
			
			rem(t);
		}
		else
			fout << heap[1] << '\n';
	}
	
	fin.close();
	fout.close();
}