Cod sursa(job #2839637)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 26 ianuarie 2022 11:52:51
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;

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

const int V = 200005;
int h[V],v[V],pos[V],cnt;

void upheap(int p)
{
    while (v[h[p]] < v[h[p / 2]])
    {
        swap(h[p],h[p / 2]);
        pos[h[p]] = p;
        pos[h[p / 2]] = p / 2;
        p /= 2;
    }
}

void downheap(int p)
{
    int fs = 2 * p,fd = 2 * p + 1,fu = p;
    if (fs <= cnt and v[h[fs]] < v[h[fu]])
        fu = fs;
    if (fd <= cnt and v[h[fd]] < v[h[fu]])
        fu = fd;
    if (fu != p)
    {
        pos[h[p]] = fu;
        pos[h[fu]] = p;
        swap(h[p],h[fu]);
        downheap(fu);
    }
}

int main()
{
    int n,x,y,p = 0;
    in >> n;
    for (int i = 1; i <= n; i++)
    {
        in >> x;
        if (x == 3)
            out << v[h[1]] << '\n';
        else if (x == 1)
        {
            in >> y;
            v[++p] = y;
            h[++cnt] = p;
            pos[p] = cnt;
            upheap(cnt);
        }
        else
        {
            in >> y;
            if (pos[y] != cnt)
            {
                swap(h[cnt],h[pos[y]]);
                swap(pos[h[cnt]],pos[y]);
            }
            cnt--;
            downheap(pos[y]);
        }
    }
    return 0;
}