Cod sursa(job #3321088)

Utilizator vlad7654vladimir manescu vlad7654 Data 8 noiembrie 2025 10:26:07
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>
using namespace std;
const int NMAX=200010;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int N, L, NR;
int A[NMAX], Heap[NMAX], Pos[NMAX];

void push(int x){
	while(x/2 && A[Heap[x]]<A[Heap[x/2]]){
        swap(Heap[x],Heap[x/2]);
		Pos[Heap[x]] = x;
		Pos[Heap[x/2]] = x/2;
		x /= 2;
	}
}

void pop(int x){
	int aux, y = 0;
	while(x != y){
		y=x;
		if(y*2<=L and A[Heap[x]]>A[Heap[y*2]]){
            x=y*2;
		}
		if(y*2+1<=L and A[Heap[x]]>A[Heap[y*2+1]]){
            x = y*2+1;
		}
        swap(Heap[x],Heap[y]);
		Pos[Heap[x]] = x;
		Pos[Heap[y]] = y;
	}
}

int main(){
	fin>>N;
	for(int i=1, cod, x; i<=N; i++){
		fin>>cod;
		if(cod<3) {
			fin>>x;
		}
		if(cod == 1){
			L++, NR++;
			A[NR] = x;
			Heap[L] = NR;
			Pos[NR] = L;
			push(L);
		}
		if(cod == 2){
			A[x] = -1;
			push(Pos[x]);
			Pos[Heap[1]] = 0;
			Heap[1] = Heap[L--];
			Pos[Heap[1]] = 1;
			if(L>1){
                pop(1);
			}
		}

		if(cod == 3){
            fout<<A[Heap[1]]<<'\n';
		}
	}
}