Cod sursa(job #2355786)

Utilizator radurotaruRotaru Radu Stefan radurotaru Data 26 februarie 2019 12:26:46
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.47 kb
#include <fstream>

using namespace std;


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

int st[1000001],v[200001],poz[200001],index,x,n,t;
void actualizare(int r)
{
    while(r > 1)
    {
        if(st[r] < st[r/2])
            {
                poz[st[r]]=r/2;
                poz[st[r/2]]=r;
                swap(st[r], st[r/2]);
            }
        else
            break;
        r=r/2;
    }
}
void actualizare_1(int r)
{
        if((st[2*r] < st[r]) && 2*r<=index)
        {
            poz[2*r]=r;
            poz[r]=2*r;
            swap(st[r], st[2*r]);
            actualizare_1(2*r);
        }
        else if((st[2*r+1] < st[r]) && 2*r+1<=index)
        {
            poz[2*r+1]=r;
            poz[r]=2*r+1;
            swap(st[r], st[2*r+1]);
            actualizare_1(2*r+1);
        }
}
int main()
{
    f>>n;
    for(int i=1; i<=n; i++)
    {
        f>>x;
        if(x==1)
        {
            f>>t;
            index++;
            v[index]=t;
            st[index]=t;
            poz[t]=index;
            actualizare(index);
            actualizare_1(index);
        }
        if(x==2)
        {
            f>>t;
            st[poz[v[t]]]=st[index];
            poz[st[index]]=poz[v[t]];
            poz[v[t]]=0;
            index--;
            actualizare(poz[st[index+1]]);
            actualizare_1(poz[st[index+1]]);
        }
        if(x==3)
            g<<st[1]<<'\n';
    }

    return 0;
}