Cod sursa(job #1921955)

Utilizator gabrielamoldovanMoldovan Gabriela gabrielamoldovan Data 10 martie 2017 15:29:14
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <iostream>
#include <fstream>

#define nmax 200005

using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

int a[nmax], pos[nmax], heap[nmax];
int n, nr, l;

void push(int x)
{
    while(x/2 && a[heap[x]]<a[heap[x/2]])
    {
        swap(heap[x], heap[x/2]);
        pos[heap[x]]=x;
        pos[heap[x/2]]=x/2;
        x/=2;
    }
}

void pop(int x)
{
    int y=0;
    while(x!=y)
    {
        y=x;
        if(y*2<=l && a[heap[x]]>a[heap[y*2]]) x=y*2;
        if(y*2+1<=l && a[heap[x]]>a[heap[y*2+1]]) x=y*2+1;
        swap(heap[x], heap[y]);
        pos[heap[x]]=x;
        pos[heap[y]]=y;
        x/=2;
    }
}

void citire()
{
    int p, k, t;
    f>>p;
    while(p>0)
    {
        f>>t;
        if(t==1)
        {
            f>>k;
            ++l;
            ++nr;
            a[nr]=k;
            heap[l]=nr;
            pos[nr]=l;
            push(l);
        }
        else
        {
            if(t==2)
            {
                f>>k;
                a[k]=-1;
                push(pos[k]);
                pos[heap[1]]=0;
                heap[1]=heap[l--];
                pos[heap[1]]=1;
                if(l>1) pop(1);
            }
            else g<<a[heap[1]]<<"\n";
        }
        p--;
    }
}

int main()
{
    citire();
    return 0;
}