Cod sursa(job #2197124)

Utilizator alisavaAlin Sava alisava Data 21 aprilie 2018 10:59:19
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.61 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[200005],a[200005],v[200005];
int n,m,obt,nr;

void Insert(int x)
{
    heap[++nr]=x;
    int y=nr;
    while(Dad(y)&&a[heap[y]]<=a[heap[Dad(y)]])
    {
        swap(heap[y],heap[Dad(y)]);
        v[heap[y]]=y;
        v[heap[Dad(y)]]=Dad(y);
        y=Dad(y);
    }
}
void Erase(int x)
{
    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(heap[x],heap[RightSon(x)]);
            v[heap[x]]=x;
            v[heap[RightSon(x)]]=RightSon(x);
            x=RightSon(x);
        }
        else
        {
            swap(heap[x],heap[LeftSon(x)]);
            v[heap[x]]=x;
            v[heap[LeftSon(x)]]=LeftSon(x);
            x=LeftSon(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;if(n==28) cout<<x<<" ";
                Insert(n);
                break;
            case 2:
                fin>>x;
                Erase(v[x]);
                break;
            case 3:
                fout<<a[heap[1]]<<"\n";
                break;
        }
    }


    return 0;
}