Cod sursa(job #2524201)

Utilizator baltoi.teodorTeodor Baltoi baltoi.teodor Data 15 ianuarie 2020 10:40:28
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.64 kb
#include <bits/stdc++.h>
#define NMAX 210002
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
long long heap[NMAX];
int pozi[NMAX],ordine[NMAX],teste,cerinta,x, n,i,nr;
void sift(int n, int k)
{
int son=10,pozitie=ordine[k],aux=heap[k];
while(son)
    {
        son=0;
        if(k*2<=n)
        {
            son=k*2;
        }
            if(heap[k*2+1]<heap[k*2] && k*2+1<=n )
                son=k*2+1;
        if(son && aux>heap[son])
        {
            pozi[ordine[son]]=k;
            heap[k]=heap[son];
            ordine[k]=ordine[son];
            k=son;
        }
        else
            son=0;
    }
heap[k]=aux;
ordine[k]=pozitie;
pozi[pozitie]=k;
}
void percolate(int n, int k)
{
    int aux=heap[k],pozitie=ordine[k];
while(k>1 && aux<heap[k/2])
    {
        pozi[ordine[k/2]]=k;
        heap[k]=heap[k/2];
        ordine[k]=ordine[k/2];
        k=k/2;
    }
heap[k]=aux;
ordine[k]=pozitie;
pozi[pozitie]=k;
}
void cut(int &n ,int k)
{
 heap[pozi[k]]=heap[n];
 pozi[ordine[n]]=pozi[k];
    ordine[pozi[k]]=ordine[n];
    n--;
    percolate(n,pozi[k]);
    sift(n,pozi[k]);
}
void insert_heap(int nod)
{
    heap[++n]=nod;
    nr++;
    pozi[nr]=n;
    ordine[n]=nr;
    percolate(n,n);
}
int main()
{
    fin>>teste;
    while(teste)
    {
        teste--;
        fin>>cerinta;
        if(cerinta==1)
        {
            fin>>x,insert_heap(x);
        }
        else
        {
            if(cerinta==2)
            {
                fin>>x,cut(n,x);
            }
            else
                fout<<heap[1]<<'\n';
        }
    }
}