Cod sursa(job #1327545)

Utilizator refugiatBoni Daniel Stefan refugiat Data 26 ianuarie 2015 20:22:06
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include<iostream>
#include<fstream>
using namespace std;
#define nmax 200002
int nr,lg;
int v[nmax],heap[nmax],poz[nmax];
void up(int st)
{
    int a;
    while(st/2&&v[heap[st]]<v[heap[st/2]])
    {
        a=heap[st];
        heap[st]=heap[st/2];
        heap[st/2]=a;
        poz[heap[st/2]]=st/2;
        poz[heap[st]]=st;
        st=st/2;
    }
}
void down(int st)
{
    int fiu=-1;
    while(st!=fiu)
    {
        fiu=st;
        if(fiu*2<=lg&&v[heap[st]]>v[heap[fiu*2]])
            st=fiu*2;

        if(fiu*2+1<=lg&&v[heap[st]]>v[heap[fiu*2+1]])
            st=fiu*2+1;
        int aux=heap[st];
        heap[st]=heap[fiu];
        heap[fiu]=aux;
        poz[heap[st]]=st;
        poz[heap[fiu]]=fiu;
    }
}
int main()
{
    ifstream si;
    si.open("heapuri.in");
    ofstream so;
    so.open("heapuri.out");
    int o;
    si>>o;
    int i,f,x;
    for(i=0;i<o;++i)
    {
        si>>f;
        if(f==1)
        {
            si>>x;
            ++nr;
            ++lg;
            v[nr]=x;
            heap[lg]=nr;
            poz[nr]=lg;
            up(lg);
        }
        if(f==2)
        {
            si>>x;
            v[x]=-1;
            up(poz[x]);
            poz[heap[1]]=0;
            heap[1]=heap[lg];
            --lg;
            poz[heap[1]]=1;
            down(1);
        }
       // for(int j=1;j<=lg;++j)
         //   cout<<v[heap[j]]<<' ';
     //   cout<<endl;
        if(f==3)
            so<<v[heap[1]]<<'\n';
    }
}