Cod sursa(job #1112631)

Utilizator alexalghisiAlghisi Alessandro Paolo alexalghisi Data 19 februarie 2014 21:48:40
Problema Heapuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <fstream>

#define DN 200005
#define INF (1<<30)
using namespace std;

int arb[DN+DN],v[DN],sz,poz[DN];

void heap_insert(int &x){

    arb[ ++sz ] = x;
    poz[ x ] = sz;
    for(int nod = sz; arb[nod/2] > x ;){

        swap(poz[ arb[nod/2] ] , poz[ arb[nod] ]);
        swap(arb[nod/2],arb[nod]);
        nod = nod/2;
    }
}

void heap_erase(int &x){

    poz[ arb[sz] ] = poz[x];
    swap(arb[ poz[x] ],arb[ sz ]);
    arb[sz] = INF;
    --sz;

    for(int nod = poz[x]; arb[nod] > min(arb[2*nod],arb[2*nod+1]) ;){

        if(arb[2*nod+1] > arb[2*nod]){

            swap(poz[ arb[nod] ],poz[ arb[2*nod] ]);
            swap(arb[nod],arb[2*nod]);
        }
        else{

            swap(poz[ arb[nod] ],poz[ arb[2*nod+1] ]);
            swap(arb[nod],arb[2*nod+1]);
        }
    }
}


int main()
{
    int m;
    ifstream f("heapuri.in");
    ofstream g("heapuri.out");
    f>>m;

    for(int i=1;i<=m;++i)
        arb[i]=INF;

    for(;m--;){

        int op;
        f>>op;
        if(op==1){

            int x;
            f>>x;
            v[ ++v[0] ] = x;
            heap_insert(x);
        }
        if(op==2){

            int x;
            f>>x;
            x=v[x];
            heap_erase(x);
        }
        if(op==3){
            g<<arb[1]<<"\n";
        }
    }

    return 0;
}