Cod sursa(job #783439)

Utilizator GodiesVlad Voicu Godies Data 2 septembrie 2012 20:33:12
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
/* Vlad Voicu 2012 */
/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */

#include <cstdio>
#include <string>
#include <iostream>

int v[200001];
int ord[200001];
int p[200001];
int size;
int o;

void exchange(int* nr1, int* nr2) {
	int aux = *nr1;
	*nr1 = *nr2;
	*nr2 = aux;
}

void insereaza(int nr) {
	int i;
	v[++size] = nr;
	p[++p[0]] = size;
	ord[size] = p[0];
	i = size;
	while ((i / 2) >= 1 && v[i/2] > v[i]) {
		exchange(&v[i], &v[i/2]);
		exchange(&ord[i], &ord[i/2]);
		exchange(&p[ord[i]], &p[ord[i/2]]);
		i = i / 2;
	}
}

void sterge(int poz) {
	int i = poz;
	if (poz*2 <= size && v[poz] > v[2*poz]) {
		i = 2*poz;
	}

	if (poz*2+1 <= size && v[i] > v[2*poz+1]) {
		i = 2*poz+1;
	}

	if (i != poz) {
		exchange(&v[poz], &v[i]);
		exchange(&ord[poz], &ord[i]);
		exchange(&p[ord[poz]], &p[ord[i]]);
		sterge(i);
	}
}

void compute() {
	int n, operatie;
	int i, nr;
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &operatie);
		if (operatie == 1) {
			scanf("%d", &nr);
			insereaza(nr);
		}
		if (operatie == 2) {
			scanf("%d", &nr);
			int aux = p[nr];
			exchange(&v[p[nr]], &v[size]);
			int aux2 = ord[size];
			exchange(&ord[p[nr]], &ord[size]);
			exchange(&p[nr], &p[aux2]);
			size--;
			sterge(aux);
		}
		if (operatie == 3) {
			printf("%d\n", v[1]);
		}
	}
}

int main() {
  freopen("heapuri.in", "r", stdin);
  freopen("heapuri.out", "w", stdout);
	compute();
	return 0;
}