Cod sursa(job #2362704)

Utilizator AvramDanielAvram Daniel AvramDaniel Data 3 martie 2019 11:37:04
Problema Heapuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include<bits/stdc++.h>
using namespace std;
#define N 200010
#define M 1000000010
int arb[N*2],t,c, pos[N], timp[N], pp,L;

void heap_push(int x)
{
    if(x==1 || arb[x] > arb[x/2]) return;
    else
    {
        swap(arb[x], arb[x/2]);
        swap(pos[timp[x]],pos[timp[x/2]]);
        swap(timp[x],timp[x/2]);
        heap_push(x/2);
    }
}

void hash_echilibrate(int x)
{
    if(x*2>L) return;
    if(arb[x]> arb[x*2] )
    {
        swap(arb[x], arb[x*2]);
        swap(pos[timp[x]],pos[timp[x*2]]);
        swap(timp[x],timp[x*2]);
        hash_echilibrate(x*2);
        return;
    }
    if(x*2+1<=L && arb[x]> arb[x*2+1] )
    {
        swap(arb[x], arb[x*2+1]);
        swap(pos[timp[x]],pos[timp[x*2+1]]);
        swap(timp[x],timp[x*2+1]);
        hash_echilibrate(x*2+1);
        return;
    }
    return;
}

int main()
{
    ifstream cin("heapuri.in");
    ofstream cout("heapuri.out");
    int x;
    cin>>t;
    while(t--)
    {
        cin>>c;
        if(c==1)
        {
            cin>>x;
            L++;pp++;
            timp[L] = pp;
            arb[L] = x;
            pos[pp] = L;
            heap_push(L);
        }
        if(c==2)
        {
            cin>>x;
            arb[pos[x]] = M;
            hash_echilibrate(pos[x]);

        }
        if (c==3)
        {
            cout<<arb[1]<<'\n';
        }
    }

    return 0;
}