Cod sursa(job #2270379)

Utilizator PredescuSebastianIonPredescu Sebastian Ion PredescuSebastianIon Data 27 octombrie 2018 10:46:09
Problema Heapuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <bits/stdc++.h>
#define MAX 2000000000

using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
struct heapuri
{
    int poz,val;
}h[200003];
int n,m,i,x,poz,cerinta,p[200003];
void heapup(int x)
{
    if(x>=1)
    {
        if(h[x].val<h[x/2].val)
        {
            swap(h[x],h[x/2]);
            p[h[x].poz]=x;
            p[h[x/2].poz]=x/2;
            heapup(x/2);
        }
    }
}
void heapdown(int x)
{
    if(x*2+1<=n && (h[x].val>h[x*2].val || h[x].val>h[x*2+1].val))
    {
        if(h[x].val>h[x*2].val)
        {
            swap(h[x],h[x*2]);
            p[h[x].poz]=x;
            p[h[x*2].poz]=x*2;
            heapdown(x*2);
        }
        else if(h[x].val>h[x*2+1].val)
        {
            swap(h[x],h[x*2+1]);
            p[h[x].poz]=x;
            p[h[x*2+1].poz]=x*2+1;
            heapdown(x*2+1);
        }
    }
    else if(x*2<=n && h[x].val>h[x*2].val)
    {
        swap(h[x],h[x*2]);
        p[h[x].poz]=x;
        p[h[x*2].poz]=x*2;
        heapdown(x*2);
    }
}
int main()
{
    f>>m;
    for(i=1;i<=m;i++)
    {
        f>>cerinta;
        if(cerinta==1)
        {
            f>>x;
            n++;
            h[n].val=x;
            h[n].poz=n;
            p[h[n].poz]=n;
            heapup(n);
        }
        else if(cerinta==2)
        {
            f>>poz;
            h[p[poz]].val=MAX;
            heapdown(p[poz]);
        }
        else g<<h[1].val<<'\n';
    }
    return 0;
}