Cod sursa(job #2202542)

Utilizator cezar.dimoiuDimoiu Cezar Gabriel cezar.dimoiu Data 9 mai 2018 00:37:36
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.61 kb
#include <bits/stdc++.h>

using namespace std; 
const int KMaxN = 10000;
const int KInf = numeric_limits<int>::max() / 2;
int n;
int size;
int heap[KMaxN + 1];
int kth_to_heap[KMaxN + 1];
int heap_to_kth[KMaxN + 1];

inline void swap(int i, int j)
{
	kth_to_heap[heap_to_kth[i]] = j;
	kth_to_heap[heap_to_kth[j]] = i;
	swap(heap_to_kth[i], heap_to_kth[j]);
	swap(heap[i], heap[j]);
}

void heap_up(int i)
{
	while (i != 1 && heap[i] < heap[i / 2]) {
		swap(i, i / 2);
		i /= 2;
	}
}

void heap_init()
{
	for (int  i = 0; i < KMaxN; ++i)
		heap[i] = KInf;
}

void heap_insert(int x)
{
	++size; ++n;
	heap[size] = x;
	kth_to_heap[n] = size;
	heap_to_kth[size] = n;
	heap_up(size);
}  

void heap_down(int i)
{
	while (heap[i] != KInf) {
		if (heap[i] > heap[2 * i]) {
			if (heap[2*i] < heap[2 *i + 1]) {
				swap(i, 2 * i);
				i = 2 * i + 1;
			} else {
				swap(i, 2 * i + 1);
				i = 2 * i + 1;
			}
		} else if (heap[i] > heap[2 * i + 1]) {
			swap(i, 2 * i + 1);
			i = 2 * i + 1;
		} else break;
	}	
}

void heap_erase(int k)
{
	int i = kth_to_heap[k];
	swap(i, size);
	heap[size] = KInf;	
	--size;
	if (i != 1 && heap[i] < heap[i / 2]) {
		heap_up(i);
	} else {
		heap_down(i);
	}
}

int heap_top()
{
	return heap[1];
}





int main()
{
	freopen("heapuri.in", "rt", stdin);
	freopen("heapuri.out", "wt", stdout);

	int m;
	scanf("%d", &m);
	heap_init();

	for (int i = 0; i < m; ++i) {
		int op, x;
		scanf("%d", &op);
		switch(op) {
			case 1: 
					scanf("%d", &x);
					heap_insert(x);
					break;
			case 2:
					scanf("%d", &x);
					heap_erase(x);
					break;
			case 3:
					printf("%d\n", heap_top());
					break;
		}
	}
	return 0;

}