Cod sursa(job #2314594)

Utilizator AlexPascu007Pascu Ionut Alexandru AlexPascu007 Data 8 ianuarie 2019 20:11:21
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#define DIM 200000
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,cod,x,a[DIM],h[DIM],poz[DIM],c,p,i,l;
int main() {
	fin>>n;
	while (n--) {
		fin>>cod;
		if (cod==1) {
			fin>>x;
			a[++i]=x;
			h[++l]=i;
			poz[i]=l;
			c=l; p=c/2;
			while (p!=0) { //urc elementul in heap
				if (a[h[c]]<a[h[p]]) {
					swap(h[c],h[p]);
					poz[h[c]]=c;
					poz[h[p]]=p;
				}
				else
					break;
				c=p; p/=2;
			}
			continue;
		}
		if (cod==2) {
			fin>>x;
			a[x]=-1; //sterg elementul ducandu-l in radacina
			c=poz[x];
			p=c/2;
			while (p!=0) {
				if (a[h[c]]<a[h[p]]) {
					swap(h[c],h[p]);
					poz[h[c]]=c;
					poz[h[p]]=p;
				}
				else
					break;
				c=p; p/=2;
			}
			h[1]=h[l--];
			poz[h[1]]=1;
			p=1; c=2;
			while (c<=l) { //sterg radacina
				if (c<l&&a[h[c+1]]<a[h[c]])
					c++;
				if (a[h[p]]>a[h[c]]) {
					swap(h[c],h[p]);
					poz[h[c]]=c;
					poz[h[p]]=p;
				}
				else
					break;
				p=c; c*=2;
			}
			continue;
		}
		if (cod==3)
			fout<<a[h[1]]<<"\n";
	}
	return 0;
}