Cod sursa(job #1823672)

Utilizator Bodo171Bogdan Pop Bodo171 Data 6 decembrie 2016 18:43:49
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
#include <iostream>
#include <fstream>
using namespace std;
const int nmax=200005;
int p[nmax],v[nmax],heap[nmax];
int poz,oldpoz,nr,L,x,n,i,tip,mn;
bool ok;
void up(int pozitie)
{
    poz=pozitie;ok=1;
    while(ok)
    {
        ok=0;
        if(v[heap[poz]]<v[heap[poz/2]])
        {
            swap(heap[poz],heap[poz/2]);
            p[heap[poz/2]]=poz/2;
            p[heap[poz]]=poz;
            poz/=2;ok=1;
        }
    }
}
void down()
{
    poz=1;ok=1;
    while(ok)
    {
        ok=0;oldpoz=poz;
        mn=(1<<30);
        if(2*poz<=L&&v[heap[2*poz]]<mn) mn=v[heap[2*poz]];
        if(2*poz+1<=L&&v[heap[2*poz+1]]<mn) mn=v[heap[2*poz+1]];
        if(mn<v[heap[poz]])
        {
         ok=1;
         if(v[heap[2*poz]]==mn) poz=2*poz;
         else poz=2*poz+1;
         swap(heap[poz],heap[oldpoz]);
         p[heap[oldpoz]]=oldpoz;
         p[heap[poz]]=poz;
        }
    }
}
int main()
{
    ifstream f("heapuri.in");
    ofstream g("heapuri.out");
    f>>n;
    v[0]=-(1<<30);
    for(i=1;i<=n;i++)
    {
        f>>tip;
        if(tip==1)
        {
            f>>x;
            nr++;
            v[nr]=x;
            L++;heap[L]=nr;
            p[nr]=L;
            up(L);
            continue;
        }
        if(tip==2)
        {
            f>>x;
            v[x]=-1;
            up(p[x]);
            heap[1]=heap[L];L--;
            p[heap[1]]=1;
            if(L>1)down();
            continue;
        }
        if(tip==3)
        {
            g<<v[heap[1]]<<'\n';
        }
    }
    return 0;
}