Cod sursa(job #2388872)

Utilizator andrei42Oandrei42O andrei42O Data 26 martie 2019 17:02:35
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
const int N = 200010;
int n, m, k, tip, h[N], ph[N],val[N];
void heap_swap(int, int),heap_down(int),heap_up(int);
int main()
{
    f >> m;
    for(; m; m--)
    {
        f >> tip;
        if(tip==1)
        {
            k++;
            f>> val[k];
            h[++n]=k;
            ph[k]=n;
            heap_up(n);
        }
        if(tip==2)
        {
            int p;
            f>>p;
            p=ph[p];
            heap_swap(p,n);
            n--;
            heap_up(p);
            heap_down(p);
        }
        if(tip==3)
            g<<val[h[1]]<<'\n';
    }
    return 0;
}

void heap_up(int fiu)
{
    int tata=fiu/2;
    if(!tata)return;
    if(val[h[fiu]]<val[h[tata]])
    {
        heap_swap(tata,fiu);
        heap_up(tata);
    }
}
void heap_down(int tata)
{
    int fiu=2*tata;
    if(fiu>n)return;
    if(fiu<n)if(val[h[fiu+1]]<val[h[fiu]])fiu++;
    if(val[h[fiu]]<val[h[tata]]){heap_swap(tata,fiu);heap_down(fiu);}
}
void heap_swap(int a,int b)
{
    swap(h[a],h[b]);ph[h[a]]=a;ph[h[b]]=b;
}