Cod sursa(job #2292280)

Utilizator richard26Francu Richard richard26 Data 29 noiembrie 2018 12:01:14
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
#define N_max	200010

using namespace std;

long long heap[N_max], ap[N_max], poz[N_max];

void minheap(int n)
{
	while (heap[n] < heap[n / 2] && n > 1)
	{
		swap(heap[n], heap[n / 2]);
		ap[poz[n]] = n / 2;
		ap[poz[n/2]] = n;
		swap(poz[n], poz[n / 2]);
		n /= 2;

	}
}


void erase(int n, int ppoz)
{
	swap(heap[n], heap[ppoz]);
	ap[poz[ppoz]] = ppoz;
	swap(poz[ppoz], poz[n]);
	int i = ppoz;
	while (heap[i] > heap[i * 2] && i * 2 < n) 
	{

		swap(heap[i], heap[i * 2]);
		ap[poz[i]] = i * 2;
		ap[poz[i * 2]] = i;
		swap(poz[i], poz[i * 2]);
		i *= 2;
	}
}
int main()
{
	ifstream f("heapuri.in");
	ofstream g("heapuri.out");

	int N, c;
	f>>N;
	
	int n = 0;
	int j = 0;
	for (int i = 1; i <= N; i++)
	{
		f>>c;
		if(c == 3) g<<heap[1]<<"\n";
			else{
				long long x;
				f>>x;
				if(c == 1){
					n++;
					j++;
					ap[n] = n;
					poz[n] = j;
					heap[n] = x;
					minheap(n);
				}
				if( c == 2){
					erase(n, ap[x]);
					n--;
				}
			}
	}

	return 0;
}