Cod sursa(job #2859668)

Utilizator ciobyCiobanu Vlasie cioby Data 1 martie 2022 18:55:01
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include    <iostream>
#include    <fstream>
#define nmax 250000
using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int heap[nmax];
int poz[nmax];
int val[nmax];
int i, n;

void schimba(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 promovare(int x)
{
    int tata = x / 2;
    if (tata && cmp(val[heap[x]], val[heap[tata]])) {
        schimba(x, tata);
        promovare(tata);
    }
}

void insert(int x)
{
    i++;
    val[i] = x;
    n++;
    heap[n] = i;
    poz[i] = n;
    promovare(n);
}

void cernere(int x)
{
    int fiu = x * 2;
    if (fiu + 1 <= n && cmp(val[heap[fiu + 1]], val[heap[fiu]]))
    {
        fiu += 1;
    }
    if (fiu <= n && cmp(val[heap[fiu]], val[heap[x]]))
    {
        schimba(fiu, x);
        cernere(fiu);
    }
}

void pop(int x)
{
    schimba(x, n);
    n--;
    cernere(x);
}

void citire()
{
    int cer, x, y, c;
    fin >> c;
    for (int i = 1; i <= c; i++)
    {
        fin >> cer;
        if (cer == 1) {
            fin >> x;
            insert(x);
        }
        if (cer == 2) {
            fin >> x;
            pop(poz[x]);
        }
        else if (cer==3){
            fout << val[heap[1]] << '\n';
        }
    }
}

int main()
{
    citire();
}