Cod sursa(job #1328028)

Utilizator refugiatBoni Daniel Stefan refugiat Data 27 ianuarie 2015 21:03:24
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 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 x;
    while(st/2&&v[heap[st]]<v[heap[st/2]])
    {
        x=heap[st];
        heap[st]=heap[st/2];
        heap[st/2]=x;
        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,fel,x;
    for(i=0;i<o;++i)
    {
        si>>fel;
        if(fel==1)
        {
            si>>x;
            ++nr;
            ++lg;
            v[lg]=x;
            heap[nr]=lg;
            poz[lg]=nr;
        }
        if(fel==2)
        {
            si>>x;
            v[x]=-1;
            up(poz[x]);
            poz[heap[1]]=0;
            heap[1]=heap[lg];
            --lg;
            poz[heap[1]]=1;
            down(1);
        }
        if(fel==3)
            so<<v[heap[1]]<<'\n';
    }
}