Cod sursa(job #2809517)

Utilizator mateitudordmDumitru Matei mateitudordm Data 27 noiembrie 2021 10:06:40
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.23 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3")

using namespace std;

int up, st, dr, cfree;
struct heap
{
    int heap[200001], free, pos[200001], pfree[200001];
    int _up (int nod)
    {
        return nod / 2;
    }
    int _downst (int nod)
    {
        return 2 * nod;
    }
    int _downdr (int nod)
    {
        return 2 * nod + 1;
    }
    void upheap (int nod)
    {
        if (_up (nod) && heap[nod] < heap[_up (nod)])
        {
            up = _up (nod);
            swap (heap[nod], heap[up]);
            swap (pfree[nod], pfree[up]);
            pos[pfree[nod]] = nod, pos[pfree[up]] = up;
            upheap (up);
        }
    }
    void downheap (int nod)
    {
        int minn = _downst (nod);
        if (_downdr (nod) < free && heap[_downdr (nod)] < heap[minn])
            minn = _downdr (nod);
        if (minn < free && heap[nod] > heap[minn])
        {
            swap (heap[nod], heap[minn]);
            swap (pfree[nod], pfree[minn]);
            pos[pfree[nod]] = nod, pos[pfree[minn]] = minn;
            downheap (minn);
        }
    }
    int top()
    {
        return heap[1];
    }
    void add (int nod, int poz)
    {
        heap[free] = nod;
        pos[poz] = free;
        pfree[free] = poz;
        upheap (free);
        free++;
    }
    void del (int t)
    {
        free--;
        int apt = pos[t];
        swap (heap[apt], heap[free]);
        swap (pfree[apt], pfree[free]);
        pos[pfree[apt]] = apt, pos[pfree[free]] = free;
        upheap (apt);
        downheap (apt);
    }
};
heap v;

int main()
{
    ifstream cin ("heapuri.in");
    ofstream cout ("heapuri.out");
    int n, c, a, i, cnt = 0, j;
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >> n;
    v.free = 1;
    for (i = 0; i < n; i++)
    {
        cin >> c;
        if (c == 1)
        {
            cin >> a;
            cnt++;
            v.add (a, cnt);
        }
        else if (c == 2)
        {
            cin >> a;
            v.del (a);
        }
        else
            cout << v.top() << '\n';
        /*for (j = 1; j < v.free; j++)
            cout << v.heap[j] << " ";
        cout << endl;*/
    }
    return 0;
}