Cod sursa(job #3305904)

Utilizator SimifilLavrente Simion Simifil Data 6 august 2025 00:02:21
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.81 kb
#include <iostream>
#include <algorithm>
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");

struct myHeap{
    long long val;
    int ind, added;
};

const int maxn = 2e6;
myHeap heap[4*maxn+10];
int where[maxn+10], heapSize, whereSize;

void mySwap( int a, int b )
{
    myHeap ax = heap[a];
    myHeap bx = heap[b];
    
    heap[ bx.ind ].added = ax.added;
    heap[ bx.ind ].val = ax.val;

    heap[ ax.ind ].added = bx.added;
    heap[ ax.ind ].val = bx.val;

    where[ ax.added ] = bx.ind;
    where[ bx.added ] = ax.ind;
}

int parinte( int x )
{
    int node = x;
    if( node%2 == 1 )
        --node;
    return node/2;
}

int copil( int node )
{
    if( (2*node)+1 <= heapSize )
    {
        if( heap[2*node].val < heap[(2*node)+1].val )
            return 2*node;
        else
            return (2*node)+1;
    }
    else
        return 2*node;
}

void moveHeapUp( int x )
{
    int node = x;
    while( parinte(node) >= 1 && heap[node].val < heap[parinte(node)].val )
    {
        mySwap( node, parinte(node) );
        node = parinte(node);
    }
    //return node;
}

void moveHeapDown( int x )
{
    int node = x;
    while( copil(node) <= heapSize && heap[node].val > heap[copil(node)].val )
    {
        //g << node << " " << copil(node) << "DAAAAA\n";
        int newNode = copil(node);
        mySwap( node, copil(node) );
        node = newNode;
    }
    //g << node << " " << copil(node) << "NUUUUUUU\n";
    //return node;
}

void heapAdd( long long x )
{
    ++heapSize;
    ++whereSize;
    heap[heapSize].added = whereSize;
    heap[heapSize].ind = heapSize;
    heap[heapSize].val = x;
    where[whereSize] = heapSize;
    moveHeapUp( heapSize );
}

void heapDelete( int y )
{
    int poz = where[y];
    //g << "Dam swap la " << where[y] << " " << heap[where[y]].val << " cu " << where[ heap[heapSize].added ] << " " << heap[heapSize].val << "\n";
    mySwap( where[y], heapSize );
    --heapSize;
    moveHeapUp(poz);
    moveHeapDown(poz);
}

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

void afiseaza()
{
    g << "\nSize: " << heapSize << "\n";
    int p = 1, i, j = 1;
    for( i = 1; i <= heapSize; ++i )
    {
        g << "{" << heap[i].val << ", " << heap[i].added << ", " << heap[i].ind << "} ";
        if( i == j )
            g << "\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 )
        {
            long long z;
            f >> z;
            heapAdd( z );
        }
        else if( c == 2 )
        {
            int z;
            f >> z;
            heapDelete( z );
        }
        else
        {
            getMin();
        }
        //afiseaza();
    }


    return 0;
}