Cod sursa(job #3149464)

Utilizator David8406Marian David David8406 Data 8 septembrie 2023 18:52:59
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
	#include <iostream>
	#include <algorithm>
	#include <fstream>
	#include <vector>
	#include <queue>
	using namespace std;
	int tip,n,nr,x,cnt,deleted[200005];
	pair <int, int> v[200005];
	ifstream fin("heapuri.in");
	ofstream fout("heapuri.out");
	void sift(int nod) {
		if (2 * nod > nr) return;
		if (2 * nod <= nr && 2 * nod + 1 > nr) {
			if (v[nod].first > v[2 * nod].first) {
				swap(v[nod], v[2 * nod]);
				sift(2 * nod);
			}
			return;
		}
		if (v[nod].first < v[2 * nod].first && v[nod].first < v[2 * nod + 1].first) return;
		if (v[2 * nod] < v[2 * nod + 1]) {
			swap(v[nod], v[2 * nod]);
			sift(2 * nod);
		}
		else {
			swap(v[nod], v[2 * nod+1]);
			sift(2 * nod + 1);
		}
	}
	void percolate(int nod) {
		if (nod == 1) return;
		if (v[nod].first < v[nod / 2].first) {
			swap(v[nod], v[nod / 2]);
			percolate(nod / 2);
		}
	}
	pair<int,int> top() {
		return v[1];
	}
	void push(int n, int m) {
		nr++;
		v[nr].first = n;
		v[nr].second = m;
		percolate(nr);
	}
	void pop() {
		v[1] = v[nr];
		nr--;
		sift(1);
	}
	int main() {
		fin >> n;
		for (int i = 1; i <= n; i++) {
			fin >> tip;
			if (tip == 1) {
				cnt++;
				fin >> x;
				push(x, cnt);
			}
			else if (tip == 2) {
				fin >> x;
				deleted[x] = true;
			}
			else {
				while (deleted[top().second] == true) {
					pop();
				}
				fout << top().first << "\n";
			}
		}
	}