Cod sursa(job #2197080)

Utilizator alisavaAlin Sava alisava Data 21 aprilie 2018 09:46:38
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <bits/stdc++.h>
#define Dad(x) (x/2)
#define RightSon(x) (x*2+1)
#define LeftSon(x) (x*2)

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int hey;

int heap[500005],a[200005],v[500005];
int n,m,obt,nr;
void Insert(int x)
{
    heap[++nr]=x;
    v[x]=nr;
    int y=nr;
    while(Dad(x)&&a[heap[y]]<a[heap[Dad(y)]])
    {
        swap(v[heap[y]],v[heap[Dad(y)]]);
        swap(heap[y],heap[Dad(y)]);
        y=Dad(y);
    }
}
void Erase(int x)
{
    swap(heap[x],heap[nr--]);
    heap[nr+1]=0;
    while((RightSon(x)<=nr&&a[heap[RightSon(x)]]<=a[heap[x]])||
          (LeftSon(x)<=nr&&a[heap[LeftSon(x)]]<=a[heap[x]]))
    {
        if(a[heap[RightSon(x)]]>=a[heap[LeftSon(x)]])
        {
            swap(v[heap[x]],v[heap[LeftSon(x)]]);
            swap(heap[x],heap[LeftSon(x)]);
            x=LeftSon(x);
        }
        else
        {
            swap(v[heap[x]],v[heap[RightSon(x)]]);
            swap(heap[x],heap[RightSon(x)]);
            x=RightSon(x);
        }

    }



}

int main()
{
    int x,obt;
    fin>>m;
    a[0]=1000000000;
    for(int i=1;i<=m;i++)
    {
        fin>>obt;
        switch(obt)
        {
            case 1:
                fin>>x;
                a[++n]=x;
                Insert(n);
                break;
            case 2:
                fin>>x;
                Erase(v[x]);
                break;
            case 3:
                fout<<a[heap[1]]<<"\n";
                break;
        }
    }


    return 0;
}