Cod sursa(job #654099)

Utilizator arnold23Arnold Tempfli arnold23 Data 29 decembrie 2011 16:11:42
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include <fstream>
#define siz 200010
#define hash 100000000

using namespace std;

int position[siz];
int heap[siz];
int c,n;
int table[hash];

void swapit(int &val1, int &val2)
{
    int temp=val1;
    val1=val2;
    val2=temp;
}

int swapp(int val1, int val2)
{
    int temp=heap[val1];
    heap[val1]=heap[val2];
    heap[val2]=temp;
    //swapit(position[val1],position[val2]);
    swapit(table[heap[val1]],table[heap[val2]]);

    return val2-val1;
}

void add(int val)
{
    ++c;
    heap[c]=val;

    int ind=c;
    while (ind>0 && heap[ind]<heap[ind/2])
    {
        swapp(ind,ind/2);
        ind=ind/2;
    }
}

void del(int pos)
{
    swapp(pos,c);
    --c;

    char ok=1;
    int ind=pos;
    int p;
    while (ind*2<=c && ok==1)
    {
        p=-1;
        if (heap[ind]>heap[ind*2]) p=swapp(ind,ind*2); else
        if (heap[ind]>heap[ind*2+1]) p=swapp(ind,ind*2+1);
        if (p==-1) ok=0; else ind=ind*2+p;
    }
    //table[heap[pos]]=ind;

}

int min()
{
    return heap[1];
}

int main()
{

    ifstream in("heapuri.in");
    ofstream out("heapuri.out");
    int i,x,k,d=0;

    c=0;
    in >> n;
    for (i=1;i<=n;++i)
    {
        in >> k;
        if (k==1)
        {
            in >> x;
            ++d;
            position[d]=x;
            table[x]=d;
            add(x);
        } else
        if (k==2)
        {
            in >> x;
            del(table[position[x]]);
        } else
        if  (k==3) out << min() << "\n";
    }

    return 0;
}