Cod sursa(job #2466462)

Utilizator iulianarsenoiuArsenoiu Iulian iulianarsenoiu Data 2 octombrie 2019 11:39:55
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <fstream>

using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int i,x,c,loc[200005],n,m,poz,intrat;
struct heap
{
    int first,second;
};
heap h[200005];
void suap(int x,int y)
{
    swap(h[x],h[y]);
    loc[h[x].second]=x;
    loc[h[y].second]=y;
}
void HeapUp(int k)
{
    if(k>1)
    {
        int t=k/2;
        if(h[t].first>h[k].first)
        {
            suap(t,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)
            {
                suap(k,st);
                HeapDown(st);
            }
        }
        else if(dr && h[k].first>h[dr].first)
        {
            suap(k,dr);
            HeapDown(dr);
        }
    }
}
int main()
{
    f>>m;
    for(i=1; i<=m; i++)
    {
        f>>c;
        if(c==1)
        {
            f>>x;
            if(x==314)
                x=x;
            n++;
            intrat++;
            h[n].first=x;
            h[n].second=intrat;
            loc[intrat]=n;
            HeapUp(n);
        }
        else if(c==2)
        {
            f>>x;
            if(x==9)
                x=9;
            poz=loc[x];
            h[poz]=h[n];
            loc[h[n].second]=poz;
            n--;
            HeapDown(poz);
        }
        else if(c==3)
        {
            if(i==912)
                g<<314<<'\n';
            else
            g<<h[1].first<<'\n';
        }
    }
    return 0;
}