Cod sursa(job #1213354)

Utilizator andreas.chelsauAndreas Chelsau andreas.chelsau Data 27 iulie 2014 21:55:16
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
#include <iostream>
#include <stdio.h>
using namespace std;
int a[200000 + 100],heapSize,poz[200000 + 100],heapPoz[200000 + 100];
const int maxim = 1000000000 + 1;
void swap(int i,int j){
	int aux = a[i];
	a[i] = a[j];
	a[j] = aux;
	aux = poz[i];
	poz[i] = poz[j];
	poz[j] = aux;
	heapPoz[poz[i]] = i;
	heapPoz[poz[j]] = j;
}
void min_heapify(int n){
	int l = (n << 1) + 1, r = ((n + 1) << 1),min = 0;

	if(l < heapSize && a[l] < a[n])
		min = l;
	else
		min = n;
	if(r < heapSize && a[r] < a[min])
		min = r;
	if(min != n){
		swap(n,min);
		min_heapify(min);
	}
}

void maintain_min_heap(){
	int l = heapSize - 1;
	while(l){
		l = (l & 1) == 0 ? (l >> 1) - 1 : (l >> 1);
		min_heapify(l);	
	}
}/*
void maintain_min_heap(int l){
	while(l){
		l = (l & 1) == 0 ? (l >> 1) - 1 : (l >> 1);
		min_heapify(l);	
	}
}*/
void delete_element(int x){
	a[heapPoz[x - 1]] = maxim;
	maintain_min_heap();
}
void add(int elt){
	a[heapSize++] = elt;
}
int main(){
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	int n,c = 0;
	scanf("%d",&n);
	for(int i = 0; i < n; i++){
		int query,elt;
		scanf("%d",&query);
		if(query == 1){
			scanf("%d",&elt);
		//	if(elt == 41)
		//		cout<<"dbg";
		//	if(c == 28)
		//		cout<<"A";
			poz[heapSize] = heapSize;
			heapPoz[heapSize] = heapSize;
			add(elt);
			maintain_min_heap();
			//min_heapify(((heapSize - 1) >> 1));
		}
		if(query == 2){
			scanf("%d",&elt);
	//		if(elt == 28)
	//			cout<<"A";
			delete_element(elt);
		}
		if(query == 3)			
			printf("%d\n",a[0]);
	}
	return 0;
}