Cod sursa(job #2339974)

Utilizator MarinPeptenaruMarin Vasile Peptenaru MarinPeptenaru Data 9 februarie 2019 16:55:53
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int nx = 200002;
int h[nx],pozv[nx],pozh[nx],x,op,nh,nv,t;
void sus (int x)
{
    if(x==1) return;
     if(h[x]>=h[x/2]) return;
    swap(h[x],h[x/2]);
    swap(pozh[pozv[x]],pozh[pozv[x/2]]);
    swap(pozv[x],pozv[x/2]);
    sus(x/2);
}
void jos (int x)
{
    int fiu = 0;
    if(2*x<=nh)
    {
        fiu = 2*x;
        if(fiu+1<=nh && h[fiu+1]<h[fiu])
            fiu++;
        if(h[x]<=h[fiu])
            fiu = 0;
    }
    if(fiu)
    {
        swap(h[x],h[fiu]);
        swap(pozh[pozv[x]],pozh[pozv[fiu]]);
        swap(pozv[x],pozv[fiu]);
        jos(fiu);
    }
}
void push(int x)
{
    ++nh;
    ++nv;
    h[nh]=x;
    pozv[nh]=nv;
    pozh[nv]=nh;
    sus(nh);
}
void pop(int x)
{
    int poz = pozh[x];
    swap(h[poz],h[nh]);
    nh--;
    sus(poz);
    jos(poz);
}
int main()
{
    for(in>>t;t;t--)
    {
        in>>op;
        if(op==1)
        {
            in>>x;
            push(x);
        }
        else if(op==2)
        {
            in>>x;
            pop(x);
        }
        else if(op==3)
            out<<h[1]<<'\n';
    }
    return 0;
}