Cod sursa(job #3236652)

Utilizator luc3lexaAlexandrescu Luca luc3lexa Data 30 iunie 2024 00:13:40
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

const int nmax = 2e5+10;
typedef int heap[nmax];
heap h;
int n = 0,numar_q,operation,value;
int deleted[nmax],numbers1[nmax];
map<int,int> mp;

inline int father(int nod){
	return nod/2;
};

inline int left_son(int nod){
	return nod*2;
};

inline int right_son(int nod){
	return nod*2 + 1;
}

inline int minimum_value(heap h){
	return h[1];
}

void sift(heap h,int n,int k){
	int son = 0;
	do{
		if(left_son(k) <= n){
			son = left_son(k);
			if(right_son(k) <= n && h[right_son(k)] < h[left_son(k)]){
				son = right_son(k);
			};
			if(h[son] >= h[k]){
				son = 0;
			}
		};
		if(son){
		 	swap(h[son],h[k]);
			k = son;
		}
	}while(son);
};

void percolate(heap h,int n,int k){
	int value = h[k];
	while(k > 1 && h[father(k)] > value){
		h[k] = h[father(k)];
		k = father(k);
	};
	h[k] = value;
}
void erase_root(heap h,int &n){
	h[1] = h[n--];
	percolate(h,n,1);
}

void insert(heap h,int &n,int value){
	h[++n] = value;
	percolate(h,n,n);
}
int main(){
	fin >> numar_q;
	int index = 0;
	for(int i = 1; i <=numar_q; i++){
		fin >> operation;
		if(operation == 1){
		    fin >> value;
			insert(h,n,value);
			numbers1[++index] = value;
		}else if(operation == 2){
		    fin >> value;
		    mp[numbers1[value]] = 1;
		}else{
		    while(mp[minimum_value(h)]){
				erase_root(h,n);
		    }
			fout << minimum_value(h) << '\n';
		}
	};
	return 0;
}