Cod sursa(job #2952874)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 10 decembrie 2022 09:52:03
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <bits/stdc++.h>
#define FILES freopen("heapuri.in" , "r" , stdin) , freopen("heapuri.out" , "w" , stdout)

using namespace std;

const int N = 2e5 + 10;

int n , x;
int heap[N] , a[N];
map < int , int > pos;

void upg(int node , int x)
{
    while(node != 1 && heap[node / 2] > x)
    {
        heap[node] = heap[node / 2];
        pos[heap[node]] = node;
        node /= 2;
    }

    heap[node] = x;
    pos[x] = node;
}

void rem(int node)
{
    int mn = 0;

    do
    {
        mn = 0;

        if(node * 2 <= n)
            mn = node * 2;

        if(node * 2 + 1 <= n)
            if(heap[mn] > heap[node * 2 + 1])
                mn = node * 2 + 1;

        if(mn) heap[node] = heap[mn] , pos[heap[mn]] = node;
        node = mn;
    }while(mn);

    --n;
}

signed main()
{
    FILES;

    int i;

    int q;
    cin >> q;

    int cnt = 0;

    while(q--)
    {
        int t;
        cin >> t;

        if(t == 1)
        {
            cin >> x;
            a[++cnt] = x;
            ++n;
            upg(n , x);
        }
        else if(t == 2)
        {
            cin >> x;
            rem(pos[a[x]]);
            pos[a[x]] = 0;
        }
        else if(t == 3)
        {
            cout << heap[1] << '\n';
        }
    }

    return 0;
}