Cod sursa(job #2194449)

Utilizator MaligMamaliga cu smantana Malig Data 13 aprilie 2018 13:11:04
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.76 kb
#if 0
#include <bits/stdc++.h>
#else
#include "includes.h"
#endif

using namespace std;

#if 1
#define pv(x) cout<<#x<<" = "<<x<<"; ";cout.flush()
#define pn cout<<endl
#else
#define pv(x)
#define pn
#endif

#ifdef ONLINE_JUDGE
#define in cin
#define out cout
#else
ifstream in("heap.in");
ofstream out("heap.out");
#endif

using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
#define pb push_back
#define mp make_pair
const int NMax = 3e3 + 5;
const ll inf_ll = 1e18 + 5;
const int inf_int = 1e9 + 5;
const int mod = 100003;
using zint = int;

int nrOrd,N;
int idx[NMax];

pair<int, int> heap[NMax];

void swapHeap(int x, int y) {
	swap(idx[heap[x].second], idx[heap[y].second]);
	swap(heap[x], heap[y]);
}

#define fs (pos << 1)
#define ss (fs + 1)
#define dad (pos >> 1)
void percolate(int pos) {
	while (pos != 1 && heap[dad].first > heap[pos].first) {
		swapHeap(pos, dad);
		pos = dad;
	}
}

void sift(int pos) {
	int son = 0;
	do {
		son = 0;
		if (fs <= N) {
			son = fs;
		}

		if (ss <= N && heap[ss].first < heap[fs].first) {
			son = ss;
		}

		if (son && heap[son].first < heap[pos].first) {
			swapHeap(son, pos);
			pos = son;
		}
		else {
			son = 0;
		}
	} while (son);
}

int main() {
	cin.sync_with_stdio(false);
	cin.tie(0);

	int M;
	in >> M;
	while (M--) {
		int tip, x;
		in >> tip;

		switch (tip) {
		case 1: {
			in >> x;
			heap[++N].first = x;
			heap[N].second = ++nrOrd;
			idx[nrOrd] = N;
			percolate(N);

			break;
		}
		case 2: {
			in >> x;
			int pos = idx[x];
			swapHeap(N, pos);
			--N;

			percolate(pos);
			sift(pos);

			break;
		}
		case 3: {
			out << heap[1].first << '\n';
			break;
		}
		}
	}

	return 0;
}