Cod sursa(job #2839697)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 26 ianuarie 2022 12:43:59
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 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 swapp(int a,int b)
{
    swap(h[a],h[b]);
    pos[h[a]] = a;
    pos[h[b]] = b;
}

void upheap(int p)
{
    while (v[h[p]] < v[h[p / 2]])
    {
        swapp(p,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)
    {
        swapp(p,fu);
        downheap(fu);
    }
}

void sterge(int p)
{
    if (p == cnt)
        cnt--;
    else
    {
        swapp(p,cnt--);
        upheap(p);
        downheap(p);
    }
}

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 >> v[++p];
            pos[p] = p;
            h[++cnt] = p;
            upheap(cnt);
        }
        else
        {
            in >> y;
            sterge(pos[y]);
        }
    }
    return 0;
}