Cod sursa(job #2730288)

Utilizator butasebiButa Gabriel-Sebastian butasebi Data 26 martie 2021 00:10:40
Problema Heapuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.23 kb
#include <bits/stdc++.h>
#define Nmax 200005
using namespace std;
struct heap_node
{
    int val, poz_in_vec;
}v[250005], aux;
queue <int> Q;
int w[200005];
/*
void heap_push(int poz, heap_node Node)
{
    if(v[poz].poz_in_vec == 0 || Node.val < v[poz].val)
    {
        if(v[poz].poz_in_vec != 0)
        {
            if(v[2 * poz].poz_in_vec == 0)heap_push(2 * poz, v[poz]);
            else if (v[2 * poz + 1].poz_in_vec == 0)heap_push(2 * poz + 1, v[poz]);
            else if(v[2 * poz].val > v[2 * poz + 1].val)heap_push(2 * poz, v[poz]);
            else heap_push(2 * poz + 1, v[poz]);
        }
        v[poz] = Node;
        w[v[poz].poz_in_vec] = poz;            // w[poz] = the node in heap where we have the poz-th elem
    }
    else
    {
        if(v[2 * poz].poz_in_vec == 0)heap_push(2 * poz, Node);
        else if (v[2 * poz + 1].poz_in_vec == 0)heap_push(2 * poz + 1, Node);
        else if(v[2 * poz].val > v[2 * poz + 1].val)heap_push(2 * poz, Node);
        else heap_push(2 * poz + 1, Node);
    }
}
*/
void lift(int poz)
{
    if(v[poz / 2].val > v[poz].val && poz / 2 > 0)
    {
        swap(v[poz], v[poz / 2]);
        if(v[poz / 2].poz_in_vec != 0)swap(w[v[poz].poz_in_vec], w[v[poz / 2].poz_in_vec]);
        lift(poz / 2);
    }
}
void heap_push(heap_node Node, int ct)
{
        while(!Q.empty() && v[Q.front()].poz_in_vec != 0)
            Q.pop();
        if(Q.empty())
        {
            for(int i = 1; i <= Nmax; i ++)
                if(v[i].poz_in_vec == 0)
                {
                    Q.push(i);
                    break;
                }
        }
        int first_free = Q.front();
        v[first_free] = Node;
        w[ct] = first_free;
        if(v[2 * first_free].poz_in_vec == 0)Q.push(2 * first_free);
        if(v[2 * first_free + 1].poz_in_vec == 0)Q.push(2 * first_free + 1);
        lift(first_free);
}
void remove_heap_node(int poz)
{
    if(v[2 * poz].poz_in_vec == 0 && v[2 * poz + 1].poz_in_vec == 0)
    {
        v[poz].poz_in_vec = 0;
    }
    else if(v[2 * poz].poz_in_vec == 0)
    {
        v[poz] = v[2 * poz + 1];
        w[v[poz].poz_in_vec] = poz;
        remove_heap_node(2 * poz + 1);
    }
    else if(v[2 * poz + 1].poz_in_vec == 0)
    {
        v[poz] = v[2 * poz];
        w[v[poz].poz_in_vec] = poz;
        remove_heap_node(2 * poz);
    }
    else if(v[2 * poz].val < v[2 * poz + 1].val)
    {
        v[poz] = v[2 * poz];
        w[v[poz].poz_in_vec] = poz;
        remove_heap_node(2 * poz);
    }
    else
    {
        v[poz] = v[2 * poz + 1];
        w[v[poz].poz_in_vec] = poz;
        remove_heap_node(2 * poz + 1);
    }
}
int n, i, op, ct, x, pozz;
int main()
{
    ifstream f("heapuri.in");
    ofstream g("heapuri.out");
    Q.push(1);
    f >> n;
    for(i = 1; i <= n; i ++)
    {
        f >> op;
        if(op == 1)
        {
            f >> x;
            ct = ct + 1;
            aux.val = x;
            aux.poz_in_vec = ct;
            heap_push(aux, ct);
        }
        if(op == 2)
        {
            f >> pozz;
            remove_heap_node(w[pozz]);
        }
        if(op == 3)
        {
            g << v[1].val << "\n";
        }
    }
    return 0;
}