Cod sursa(job #1880155)

Utilizator trutruvasilicaHuhurez Marius trutruvasilica Data 15 februarie 2017 16:21:27
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <fstream>
#include <bitset>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
bitset<200001>uz;
int NrVal,Heap[200001],val[200001],pos[200001],LungHeap;
void pushUp(int x)
{
    while(x/2 &&val[Heap[x/2]]>val[Heap[x]])
    {
        swap(Heap[x/2],Heap[x]);
        pos[Heap[x]]=x;
        pos[Heap[x/2]]=x/2;
        x/=2;
    }
}
void popDown(int x)
{
    int y=0;
    while(x!=y)
    {
        y=x;
        if(y*2<=LungHeap && val[Heap[x]]>val[Heap[y*2]]) x=y*2;
        if(y*2+1<=LungHeap && val[Heap[x]]>val[Heap[y*2+1]]) x=y*2+1;
        swap(Heap[x],Heap[y]);
        pos[Heap[x]]=x;
        pos[Heap[y]]=y;
    }
}
int main()
{
    int n,a,b,i;
    fin>>n;
    for(i=1;i<=n;i++)
    {
        fin>>a;
        if(a==1)
        {
            fin>>b;
            NrVal++;LungHeap++;
            val[NrVal]=b;
            Heap[LungHeap]=NrVal;
            pos[NrVal]=LungHeap;
            pushUp(LungHeap);
        }
        else if(a==2)
        {
            fin>>b;
            val[b]=-1;
            pushUp(pos[b]);
            pos[Heap[1]]=0;
            Heap[1]=Heap[LungHeap];
            LungHeap--;
            pos[Heap[1]]=1;
            if(LungHeap>1) popDown(1);
        }
        else if(a==3)
        {
            fout<<val[Heap[1]]<<"\n";
        }
    }
}