Cod sursa(job #514478)

Utilizator vladtarniceruVlad Tarniceru vladtarniceru Data 18 decembrie 2010 19:54:46
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
# include <fstream>
# define  MAX  200100
  using namespace std;
    ifstream f ("heapuri.in");
	ofstream g ("heapuri.out");
    int n;
	int elem[MAX], v[MAX];
	inline int dad (int son){
		return son >> 1;
	}
	inline int son1 (int dad){
		return dad << 1;
	}
	inline int son2 (int dad){
		return (dad << 1) + 1;
	}
	void HEAP (int poz){
		for (;;){
			int x = dad (poz), y = son2 (x);
			if (v[x] >= v[poz] && v[poz] <= v[y] && x > 0 && y > 0){//daca v[x] poate fi tata
				int aux = v[x];
				v[x] = v[poz];
				v[poz] = aux;
				poz = x;
			}
			else{
				break ;//nu mai poate inainta
			}
		}
	}
	void HEAP2 (int poz){
		for (;;){
			int x = son1 (poz), y = son2 (poz);
			if (v[x] <= v[y] && x <= v[0]){
				int aux = v[x];
			    v[x] = v[poz];
			    v[poz] = aux;
				poz = x;
			}
			else
			if (v[x] > v[y] && y <= v[0]){
				int aux = v[y];
			    v[y] = v[poz];
			    v[poz] = aux;
				poz = y;
			}
			else break ;
		}
	}
    int main (){
		f >> n;
		for (int i = 1; i <= n; ++i) v[i] = 2000000000;
		for (int i = 1; i <= n; ++i){
			int cit, nr;
			f >> cit;
			if (cit == 1){
				int var;
				f >> nr;
				v[++v[0]] = nr;
				HEAP (v[0]);
				elem[++elem[0]] = nr;
			}
			else
			if (cit == 2){
				f >> nr;
				int pozitie = elem[nr], pozlast;
				for (int j = 1; j <= n; ++j)
					if (v[j] == pozitie){
						pozlast = j;
						break ;
					}
				v[pozlast] = 2000000000;
				HEAP2 (pozlast);
			}
			else g << v[1] << '\n';
		}
		g.close ();
		return 0;
	}