Cod sursa(job #2896441)

Utilizator NFJJuniorIancu Ivasciuc NFJJunior Data 29 aprilie 2022 23:11:57
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
#define cin f
#define cout g
const int Max = 2e5 + 1;
void nos()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

int k, heap[Max], arr[Max];
set < int > lazy;
void urca(int poz)
{
	while(poz != 1)
	{
		int tata = poz / 2;
		if(heap[tata] > heap[poz])
		{
			swap(heap[tata], heap[poz]);
			poz = tata;
		}
		else
			break;
	}
}

void push(int x)
{
	heap[++ k] = x;
	urca(k);
}

void coboara(int poz)
{
	if(poz * 2 > k)
		return;
	int left = heap[poz * 2], right = -1;
	if(poz * 2 + 1 <= k)
		right = heap[poz * 2 + 1];
	if(right == -1 or left < right)
	{
		if(left < heap[poz])
		{
			swap(heap[poz], heap[poz * 2]);
			coboara(poz * 2);
		}
	}
	else
	{
		if(right < heap[poz])
		{
			swap(heap[poz], heap[poz * 2 + 1]);
			coboara(poz * 2 + 1);
		}
	}
}

void pop(int poz)
{
	swap(heap[poz], heap[k]);
	k --;
	coboara(poz);
}

int main()
{
	nos();
	int n, m = 0; 
	cin >> n;
	for(int i = 1; i <= n; i ++)
	{
		int op; cin >> op;
		if(op == 1)
		{
			int x; cin >> x;
			arr[++ m] = x;
			push(x);
		}
		else if(op == 2)
		{
			int x; cin >> x;
			lazy.insert(arr[x]);
		}
		else
		{
			cout<<heap[1]<<'\n';
		}
		while(k > 0 and lazy.size() > 0 and heap[1] == *lazy.begin())
		{
			pop(0);
			lazy.erase(lazy.begin());
		}
	}
	return 0;
}