Cod sursa(job #3245147)

Utilizator Sasha_12454Eric Paturan Sasha_12454 Data 27 septembrie 2024 18:40:41
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#include <bits/stdc++.h>

const std :: string FILENAME = "heapuri";

std :: ifstream f (FILENAME + ".in");

std :: ofstream g (FILENAME + ".out");

const int NMAX = 2e5 + 5;

int n;

int cer;

int x;

int q;

int cnt;

int v[NMAX];//pozitia elemntului al i lea in heap

std :: pair <int, int> h[2 * NMAX];

int sus(int x)
{
    cnt ++;

    int ret = cnt;

    h[cnt].first = x;

    h[cnt].second = q;

    while(ret > 1 && h[ret].first < h[ret / 2].first)
    {
        std :: swap(v[h[ret].second], v[h[ret / 2].second]);

        std :: swap(h[ret], h[ret / 2]);

        ret /= 2;
    }

    return ret;
}

void jos(int x)
{
    std :: swap(v[h[x].second], v[h[cnt].second]);

    std :: swap(h[v[x]], h[cnt]);

    cnt --;

    int k = v[x];

    while(k * 2 <= cnt)
    {
        int son = k * 2;

        if(k * 2 + 1 <= cnt && h[k * 2].first > h[k * 2 + 1].first)
        {
            son = k * 2 + 1;
        }

        if(h[k].first <= h[son].first)
        {
            break;
        }

        std :: swap(v[h[k].second], v[h[son].second]);

        std :: swap(h[k], h[son]);

        k = son;
    }
}

int main()
{

    f >> n;

    for(int i = 1; i <= n; i ++)
    {
        f >> cer;

        if(cer == 1)
        {
            q ++;

            f >> x;

            v[q] = sus(x);
        }
        else if(cer == 2)
        {
            f >> x;

            jos(x);
        }
        else
        {
            g << h[1].first << '\n';
        }
    }

    return 0;
}