Cod sursa(job #2545826)

Utilizator cezarus30cezarus30 cezarus30 Data 13 februarie 2020 16:08:12
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>



using namespace std;



ifstream in("heapuri.in");

ofstream out("heapuri.out");



const int MAX = 200001;

int v[MAX], h[MAX], poz[MAX], x, y, n, nh, j;



void schimb(int p, int q)

{

    swap(h[p], h[q]);

    poz[h[p]] = p;

    poz[h[q]] = q;

}



void urca(int p)

{

    while(p > 1 && v[h[p]] < v[h[p/2]])

    {

        schimb(p, p/2);

        p /= 2;

    }

}



void adauga(int p)

{

    h[++nh] = p;

    poz[p] = nh;

    urca(nh);

}



void coboara(int p)

{

    int fs = 2*p, fd = 2*p+1, bun = p;

    if(fs <= nh && v[h[fs]] < v[h[bun]])

    {

        bun = fs;

    }

    if(fd <= nh && v[h[fd]] < v[h[bun]])

    {

        bun = fd;

    }

    if(bun != p)

    {

        schimb (bun, p);

        coboara(bun);

    }

}



void sterge(int p)

{

    schimb(p, nh--);

    urca(p);

    coboara(p);

}



int main()

{

    in >> n;

    for(int i = 1; i <= n; i++)

    {

        in >> x;

        if(x == 1)

        {

            in >> y;

            v[++j] = y;

            adauga(j);

        }

        if(x == 2)

        {

            in >> y;

            sterge(poz[y]);

        }

        if(x == 3)

        {

            out << v[h[1]] << "\n";

        }

    }

    return 0;

}