Cod sursa(job #1986601)

Utilizator gabriel.bjgGabriel b. gabriel.bjg Data 28 mai 2017 18:45:06
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
//#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <functional>
#include <queue>

using namespace std;

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

#define NR 200005

vector<int> heap, pos(NR, 0), nth;

void add_heap(int x)
{
	int p = heap.size();
	pos[x] = p;
	heap.push_back(x);
	nth.push_back(x);
	while (p > 0 && heap[p] < heap[p/2])
	{
		swap(pos[heap[p]], pos[heap[p / 2]]);
		swap(heap[p], heap[p / 2]);
		p /= 2;
	}
}

void remove_heap(int x)
{
	heap.erase(heap.begin() + pos[nth[x - 1]]);
	make_heap(heap.begin(), heap.end(), greater<int>());
}

int main()
{
	int n, i, op, x, current = 0;

	fin >> n;
	for (i = 0; i < n; ++i)
	{
		fin >> op;
		switch (op)
		{
			case 1:
				fin >> x;
				add_heap(x);
				break;
			case 2:
				fin >> x;
				remove_heap(x);
				break;
			case 3:
				fout << heap[0] << "\n";
				break;
		}
	}

	return 0;
}