Cod sursa(job #3305872)

Utilizator SimifilLavrente Simion Simifil Data 5 august 2025 20:20:33
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.36 kb
#include <iostream>
#include <algorithm>
#include <fstream>
#include <cmath>
using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

struct heapElement{
    int poz, val, born;
};

/*
poz - where in heap
val - value
born - when added
*/

const int maxn = 2*1e5;
heapElement heap[4*maxn+1];
int heapDim = 0, pozBorn[maxn+1], bornDim = 0;

void getMin()
{
    g << heap[1].val << "\n";
}

int parinte( int nod )
{
    return (nod - (nod%2)) / 2;
}

int copil( int nod )
{
    return nod*2;
}

void repairUpHeap( int x )
{
    int nod = x;
    while( parinte(nod) != 0 && heap[nod].val < heap[parinte(nod)].val )
    {
        //cout << "\n" << "Swap la " << nod << " " << parinte(nod) << "\n";
        heapElement fiu = heap[nod];
        heapElement tata = heap[parinte(nod)];

        heapElement a = heap[nod];
        heap[nod].born = heap[parinte(nod)].born;
        //heap[nod].poz = heap[parinte(nod)].poz;
        heap[nod].val = heap[parinte(nod)].val;
        heap[parinte(nod)].born = a.born;
        //heap[parinte(nod)].poz = a.poz;
        heap[parinte(nod)].val = a.val;
        
        
        //swap( heap[nod], heap[parinte(nod)] );
        pozBorn[ tata.born ] = fiu.poz;
        pozBorn[ fiu.born ] = tata.poz;
        nod = parinte(nod);
    }
}

void repairDownHeap( int x )
{
    int nod = x;
    while( copil(nod) <= heapDim && heap[nod].val > heap[copil(nod)].val )
    {
        //cout << "\n" << "Swap la " << nod << " " << copil(nod) << "\n";
        heapElement fiu = heap[nod];
        heapElement tata = heap[copil(nod)]; //se inverseaza sensul dar acelasi nume

        heapElement a = heap[nod];
        heap[nod].born = heap[copil(nod)].born;
        //heap[nod].poz = heap[copil(nod)].poz;
        heap[nod].val = heap[copil(nod)].val;
        heap[copil(nod)].born = a.born;
        //heap[copil(nod)].poz = a.poz;
        heap[copil(nod)].val = a.val;
        
        
        //swap( heap[nod], heap[copil(nod)] );
        pozBorn[ tata.born ] = fiu.poz;
        pozBorn[ fiu.born ] = tata.poz;
        nod = copil(nod);
    }
}

void add( int x )
{
    ++heapDim;
    ++bornDim;
    heap[heapDim].val = x;
    heap[heapDim].poz = heapDim;
    heap[heapDim].born = bornDim;
    pozBorn[bornDim] = heapDim;
    repairUpHeap( heapDim );
}

void sterge( int z )
{
    int unde = pozBorn[ z ];
    //cout << "\nAvem " << z << " " << pozBorn[ z ] << " " << heap[ unde ].val << "\n";
    heapElement capat = heap[heapDim];
    heap[ unde ].born = capat.born;
    heap[ unde ].val = capat.val;
    pozBorn[capat.born] = unde;
    --heapDim;
    repairUpHeap( unde );
    repairDownHeap( unde );
}

void afiseaza()
{
    cout << "\nSize: " << heapDim << "\n";
    int p = 1, i, j = 1;
    for( i = 1; i <= heapDim; ++i )
    {
        cout << "{" << heap[i].val << ", " << heap[i].born << ", " << heap[i].poz << "} ";
        if( i == j )
            cout << "\n", p*=2, j+=p;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    f.tie(nullptr);
    g.tie(nullptr);
    int m;
    f >> m;
    for( int i = 1; i <= m; ++i )
    {
        int c;
        f >> c;
        if( c == 1 )
        {
            int x;
            f >> x;
            add(x);
        }
        else if( c == 2 )
        {
            int x;
            f >> x;
            sterge(x);
            //cout << "Hopa";
        }
        else
            getMin();
        //afiseaza();
    }
    return 0;
}