Cod sursa(job #2756609)

Utilizator AndreiAlexandru2k3Ciucan Andrei Alexandru AndreiAlexandru2k3 Data 1 iunie 2021 19:43:36
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

const int NMAX = 200005;
int N, Heap[NMAX], Val[NMAX], poz[NMAX], i;

inline void Swap(int x, int y)
{
    swap(Heap[x], Heap[y]);
    swap(poz[Heap[x]], poz[Heap[y]]);
}

bool cmp(int x, int y)
{
    return x < y;
}

void UpHead(int x)
{
    int tata = x / 2;
    if(tata && cmp(Val[Heap[x]], Val[Heap[tata]]))
    {
        Swap(x, tata);
        UpHead(tata);
    }
}

void DownHeap(int x)
{
    int fiu = 2 * x;
    if(fiu + 1 <= N && cmp(Val[Heap[fiu + 1]], Val[Heap[fiu]]))
        fiu++;
    if(fiu <= N && cmp(Val[Heap[fiu]], Val[Heap[x]]))
    {
        Swap(fiu, x);
        DownHeap(fiu);
    }
}

void Insert(int x)
{
    i += 1;
    Val[i] = x;
    //
    N += 1;
    Heap[N] = i;
    poz[i] = N;
    //
    UpHead(N);
}

void Erase(int x)
{
    Swap(x, N);
    N -= 1;
    DownHeap(x);
}

void citire()
{
    int q, c;
    f >> q;
    while(q--)
    {
        f >> c;
        if(c == 1)
        {
            int x;
            f >> x;
            Insert(x);
        }
        else if(c == 2)
        {
            int x;
            f >> x;
            Erase(poz[x]);
        }
        else
            g << Val[Heap[1]] << '\n';
    }
}

int main()
{
    citire();
    return 0;
}