Cod sursa(job #2175763)

Utilizator Tudor_CandeaCandea Tudor Tudor_Candea Data 16 martie 2018 18:54:20
Problema Heapuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include <fstream>
#include <climits>

using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");

int h[200010];
int ct[200010];
int ct1[200010];

int tata(int nod)
{
    return nod/2;
}
int fiu_d(int nod)
{
    return 2*nod+1;
}
int fiu_s(int nod)
{
    return 2*nod;
}

void percolate(int k)
{
    int c=h[k];
    while(k>1 and c<h[tata(k)])
    {
        h[k]=h[tata(k)];
        int k2=k;
        ct[h[k]]=tata(k);
        ct[h[tata(k)]]=k2;
        k=tata(k);
    }
    h[k]=c;
    ct[h[k]]=k;
}

void shift(int nr, int k)
{
    k=ct[k];
    int fiu=0, con=h[k];
    h[k]=INT_MAX;
    do
    {
        if(fiu_s(k)<=nr and h[fiu]<=h[k])
        {
            fiu=fiu_s(k);

            if(fiu_d(k)<=nr and h[fiu_s(k)]>h[fiu_d(k)])
                fiu=fiu_d(k);
            if(h[fiu]>=h[k])
                fiu=0;
        }
        else
            fiu=0;

        if(fiu)
        {
            swap(h[k], h[fiu]);
            swap(ct[h[k]], ct[con]);
            k=fiu;
        }
    }while(fiu);
}
int main()
{
   int n, k=1;
   fin>>n;
   for(int i=1;i<=n;i++)
   {
        int a, b;
        fin>>a;
        if(a==1)
        {
            fin>>b;
            h[k]=b;
            ct[b]=k;
            ct1[k]=b;
            percolate(k);
            k++;
        }
        if(a==2)
        {
            fin>>b;
            shift(k-1 , ct1[b]);
        }
        if(a==3)
            fout<<h[1]<<'\n';

   }
   return 0;
}

/** mERGE DAR DOAR 40p*/