Cod sursa(job #2577297)

Utilizator Iulia25Hosu Iulia Iulia25 Data 8 martie 2020 22:36:31
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>

using namespace std;

ifstream cin ("heapuri.in");
ofstream cout ("heapuri.out");
int poz[200005];
struct heap {
	int intrat,val;
} h[200005];
int t,k, n;
void inserare(int x) {
	h[n+1].val=x;
	t++;
	h[n+1].intrat=t;
	n++;
	poz[t]=n;
	int p = n;
	bool promovare=true;
	while(promovare==true) {
		if(h[p].val<h[p/2].val && p > 1) {
			heap aux=h[p];
			h[p]=h[p/2];
			h[p/2]=aux;
			int auxpoz=poz[h[p].intrat];
			poz[h[p].intrat]=poz[h[p/2].intrat];
			poz[h[p/2].intrat]=auxpoz;
			p/=2;
		} else
			promovare=false;
	}
}
void stergere(int timp) {
	int pozdesters=poz[timp];
	poz[h[n].intrat] = pozdesters;
	h[pozdesters]=h[n];
	h[n]= {0,0};
	n--;
	bool cernere=true;
	int i=pozdesters;
	while(cernere==true) {
		if((h[i*2].val<h[i*2+1].val && i*2+1<=n || i*2+1>n && i*2<=n) && h[i*2].val<h[i].val) {
			swap(h[i*2],h[i]);
			swap(poz[h[i*2].intrat],poz[h[i].intrat]);
			i=i*2;
		} else if((h[i*2+1].val<h[i*2].val && i*2+1<=n) && h[i*2+1].val<h[i].val) {
			swap(h[i*2+1],h[i]);
			swap(poz[h[i*2+1].intrat],poz[h[i].intrat]);
			i=i*2+1;
		} else
			cernere=false;
	}
}
int main()	{
	int n,tip,x,timp;
	cin>>n;
	for(int i=1; i<=n; ++i) {
		cin>>tip;
		if(tip==1) {
			cin>>x;
			inserare(x);
		} else if(tip==2) {
			cin>>timp;
			stergere(timp);
		} else
			cout<<h[1].val<<'\n';
	}
	return 0;
}