Cod sursa(job #2466418)

Utilizator iulianarsenoiuArsenoiu Iulian iulianarsenoiu Data 2 octombrie 2019 09:35:15
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include <fstream>

using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int i,x,c,loc[200005],n,m,poz;
pair <int,int> h[200005];
void HeapUp(int k)
{
    if(k>1)
    {
        int t=k/2;
        if(h[t].first>h[k].first)
        {
            loc[n]=t;
            loc[h[t].second]=k;
            swap(h[t],h[k]);
            HeapUp(t);
        }
    }
}
void HeapDown(int k)
{
    int st=0,dr=0;
    if(2*k<=n)
    {
        st=2*k;
        if(2*k+1<=n)
            dr=2*k+1;
        if(h[st].first<h[dr].first || dr==0)
        {
            if(h[k].first>h[st].first)
            {
                swap(h[k],h[st]);
                loc[h[k].second]=st;
                loc[h[st].second]=k;
                HeapDown(st);
            }
        }
        else if(h[k].first>h[dr].first)
        {
            swap(h[k],h[dr]);
            loc[h[k].second]=dr;
            loc[h[dr].second]=k;
            HeapDown(dr);
        }
    }
}
int main()
{
    f>>m;
    for(i=1; i<=m; i++)
    {
        f>>c;
        if(c==1)
        {
            f>>x;
            n++;
            h[n].first=x;
            h[n].second=n;
            loc[n]=n;
            HeapUp(n);
        }
        else if(c==2)
        {
            f>>x;
            poz=loc[x];
            h[poz]=h[n];
            h[n].first=h[n].second=0;
            n--;
            HeapDown(poz);
        }
        else if(c==3)
        {
            g<<h[1].first<<'\n';
        }
    }
    return 0;
}