Cod sursa(job #3179763)

Utilizator dimitriemihaiMihai Dimitrie dimitriemihai Data 4 decembrie 2023 10:06:03
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <bits/stdc++.h>
#define NM 200001

using namespace std;

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

int heap[NM], poz[NM], a[NM];
int nr, l, n;

void push (int x)
{
    while (x/2 && a[heap[x]] < a[heap[x/2]]){
        swap(heap[x], heap[x/2]);
        poz[heap[x/2]] = x/2;
        poz[heap[x]] = x;
        x/=2;
    }
}

void pop (int x)
{
    int y;

    while (x != y){
        y = x;

        if (y*2 <= l && a[heap[x]] > a[heap[y*2]])
            x = y*2;

        if (y*2 + 1 <= l && a[heap[x]] > a[heap[y*2 + 1]])
            x = y*2 + 1;

        swap(heap[x], heap[y]);
        poz[heap[x]] = x;
        poz[heap[y]] = y;
    }
}

int main()
{
    int i, j;
    int x;

    fin >> n;

    for (i=1 ; i<=n ; i++){
        fin >> j;

        if (j == 1){
            fin >> x;
            l++; nr++;
            heap[l] = nr;
            poz[nr] = l;
            a[nr] = x;

            push(l);
        }

        if (j == 2){
            fin >> x;
            a[x] = -1;
            push(poz[x]);

            poz[heap[1]] = 0;
            heap[1] = heap[l--];
            poz[heap[1]] = 1;
            pop(1);
        }

        if (j == 3){
            fout << a[heap[1]] << '\n';
        }
    }
    return 0;
}