Cod sursa(job #1074768)

Utilizator Iustin_BulimarFMI Iustin Bulimar Iustin_Bulimar Data 7 ianuarie 2014 22:35:12
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");

int n, t, x, p[200001], l, nr;
struct heap
{
    int v, nr;
};
heap h[200001];

void schimba(int i, int j)
{
    swap(h[i], h[j]);
    p[h[i].nr]=i;
    p[h[j].nr]=j;
}
void push(int x, int l, int nr)
{
    int i=l;
    h[i].v=x;
    h[i].nr=nr;
    p[nr]=i;
    while(h[i/2].v>h[i].v && i>1)
    {
        schimba(i/2, i);
        i/=2;
    }
}

int minim(int i)
{
    if(i*2>l) return 0;
    else
    {
        if(i*2+1>l) return i*2;
        if(h[i*2].v<h[i*2+1].v) return i*2;
        return i*2+1;
    }
}

void pop(int i)
{
    int j;
    schimba(i, l--);
    j=minim(i);
    while(h[i/2].v>h[i].v && i>1)
    {
        schimba(i/2, i);
        i/=2;
    }
    while(i<=l && h[i].v>h[j].v && j)
    {
        schimba(i, j);
        i=j;
        j=minim(i);
    }
}
int main()
{
    for(cin>>n; n>0; n--)
    {
        cin>>t;
        if(t==3) cout<<h[1].v<<'\n';
        else
        {
            cin>>x;
            if(t==1) push(x, ++l,++nr);
            else pop(p[x]);
        }
    }
    return 0;
}