Cod sursa(job #1460839)

Utilizator hunter_ionutzzzFarcas Ionut hunter_ionutzzz Data 14 iulie 2015 04:44:42
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include<fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int v[200001],poz[200001],heap[200001],i,aux,nr,n;
inline void downheap(int x)
{int fiu;
    while (x <= nr)
    {   fiu = x;
        if ((x << 1) <= nr)
        {   fiu = x << 1;
            if (fiu + 1 <= nr && v[heap[fiu+1]] < v[heap[fiu]])
                ++fiu;
        }
        else
            break;
        if (v[heap[x]] < v[heap[fiu]])
        {   heap[fiu] = heap[fiu] + heap[x] - (heap[x] = heap[fiu]);
            poz[heap[fiu]] = fiu;
            poz[heap[x]] = x;
            x = fiu;
        }
        else
            break;
    }
}
inline void upheap(int x)
{int tata;
    while (x > 1)
    {   tata = x >> 1;
        if (v[heap[tata]] > v[heap[x]])
        {   heap[x] = heap[x] + heap[tata] - (heap[tata] = heap[x]);
            poz[heap[tata]] = tata;
            poz[heap[x]] = x;
            x = tata;
        }
        else
            x = 1;
    }
}
int main()
{   fin >> n;
    for (i=1;i<=n;++i)
    {   fin >> aux;
        if (aux == 1)
        {   fin >> v[++nr];
            poz[nr] = nr;
            heap[nr] = nr;
            upheap(nr);
        }
        else
            if (aux == 3)
                fout << v[heap[1]] << '\n';
            else
            {   fin >> aux;
                downheap(poz[aux]);
            }

    }
    return 0;
}