Cod sursa(job #2506184)

Utilizator AnduRazvanMindrescu Andu AnduRazvan Data 7 decembrie 2019 17:41:56
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.43 kb
#include <iostream>
#include<fstream>
#define N 200005
#define INF 2000000000

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

int n,a[N],ct;
int intrat[N], curent[N];
int nrintrari;

void inserare(int x)
{
    int i,cct;
    a[ct] = x;
    cct = ct;
    while(a[cct]<a[cct >> 1] && (cct >> 1) >=1)
    {
        swap(a[cct],a[cct >> 1]);
        swap(curent[cct],curent[cct >> 1]);
        swap(intrat[curent[cct >> 1]], intrat[curent[cct]]);
        cct = cct >> 1;
    }
}

void sterge(int poz)
{
    int poz2 = intrat[poz];

    swap(a[poz2], a[ct]);
    swap(curent[poz2], curent[ct]);
    swap(intrat[curent[poz2]], intrat[curent[ct]]);
    ct--;
    if(a[poz2] < a[poz2 >> 1])
        while(a[poz2] < a[poz2 >> 1] && (poz2 >> 1) >= 1)
        {
            swap(a[poz2], a[poz2 >> 1]);
            swap(curent[poz2], curent[poz2 >> 1]);
            swap(intrat[curent[poz2]], intrat[curent[poz2 >> 1]]);

        }
    else
         while((a[poz2] > a[poz2 << 1] && poz2 << 1 <= ct)||
              (a[poz2] > a[(poz2 << 1) + 1] && (poz2 << 1) + 1 <= ct))
        {

            if(a[poz2] > a[poz2 << 1] && (poz2 << 1) <= ct &&
              ((poz2 << 1) + 1 > ct || ((poz2 << 1) + 1 <= ct && a[poz2 << 1] < a[(poz << 1) + 1]))

               )
            {
                swap(a[poz2], a[poz2 << 1]);
                swap(curent[poz2], curent[poz2 << 1]);
                swap(intrat[curent[poz2]], intrat[curent[poz2 << 1]]);

                poz2 = poz2 << 1;

            }
            else if(a[poz2]>a[(poz2 << 1) + 1] && (poz2 << 1)  + 1<= ct)
            {
                swap(a[poz2], a[(poz2 << 1) + 1]);
                swap(curent[poz2], curent[(poz2 << 1) + 1]);
                swap(intrat[curent[poz2]], intrat[curent[(poz2 << 1) + 1]]);

                poz2 = (poz2 << 1) + 1;
            }
        }
}

void read()
{
    int i, task, x, ok;
    fin >> n;
    for(i=1; i <= n; ++i)
     {
         fin >> task;
         if(task == 1)
         {
             fin >> x;
             nrintrari++;
             ct++;
             intrat[nrintrari]=ct;
             curent[ct] = nrintrari;
             inserare(x);
         }
         else if(task==2)
         {
             fin>>x;
             sterge(x);
         }
         else if(task==3)
            fout<<a[1]<<endl;
     }
}
int main()
{
    read();

    return 0;
}