Cod sursa(job #1973833)

Utilizator nicu_serteSerte Nicu nicu_serte Data 26 aprilie 2017 00:23:05
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define nmax 200005
int v[nmax], heap[nmax], l, poz[nmax], n;
void heapUp(int k)
{
    while(k/2 && v[heap[k/2]]>v[heap[k]])
    {
        swap(heap[k/2], heap[k]);
        poz[heap[k/2]]=k/2;
        poz[heap[k]]=k;
        k/=2;
    }
}
void heapDown(int k)
{
    int fiu;
    fiu=2*k;
    while(fiu<=l)
    {
        if(fiu+1<=l)
            if(v[heap[fiu+1]]<v[heap[fiu]])
                fiu++;
        if(v[heap[k]]>v[heap[fiu]])
        {
            swap(heap[k], heap[fiu]);
            poz[heap[k]]=k;
            poz[heap[fiu]]=fiu;
        }
        else return;
        k=fiu;
        fiu=2*k;
    }
}
void sterge(int x)
{
    int k;
    k=poz[x];
    heap[k]=heap[l];
    l--;
    if(k/2 && v[heap[k/2]]>v[heap[k]])
        heapUp(k);
    else heapDown(k);
}
void add(int x)
{
    l++;
    n++;
    v[n]=x;
    heap[l]=n;
    poz[n]=l;
    heapUp(l);
}
int main()
{
    int n, op, x;
    fin>>n;
    while(n)
    {
        n--;
        fin>>op;
        if(op==1)
        {
            fin>>x;
            add(x);
        }
        else if(op==2)
        {
            fin>>x;
            sterge(x);
        }
        else if(op==3)
            fout<<v[heap[1]]<<'\n';
    }
    fin.close();
    fout.close();
    return 0;
}