Cod sursa(job #2617734)

Utilizator VladAlexandruAnghelache Vlad VladAlexandru Data 22 mai 2020 18:19:02
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.1 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
struct nod
{
    int x,y;
};

vector<nod> heap;
int nr=0;
int parinte(int x)
{
    return (x-1)/2;
}
int fiu_st(int x)
{
    return 2*(x+1)-1;
}
int fiu_dr(int x)
{
    return 2*(x+1);
}
void insrt(nod n)
{
    heap.push_back(n);
    int i=++nr;
    heap[i-1].x=i;
    i--;
    while(i>0&&heap[parinte(i)].y>heap[i].y)
    {
        nod aux=heap[i];
        heap[i]=heap[parinte(i)];
        heap[parinte(i)]=aux;
        i=parinte(i);
    }
}

void heapify(int i)
{
    int l = fiu_st(i);
    int r = fiu_dr(i);
    if (l < heap.size() && heap[l].y < heap[i].y)
        {
            nod aux=heap[i];
            heap[i]=heap[l];
            heap[l]=aux;
            heapify(l);
        }
    else if (r < heap.size() && (heap[r].y < heap[i].y&&heap[r].y<heap[l].y))
       {
            nod aux=heap[i];
            heap[i]=heap[r];
            heap[r]=aux;
            heapify(r);
       }
}


nod del_min()
{
    nod rez;
    if(heap.size()==1)
    {
        rez=heap[0];
        heap.erase(heap.begin());
        return rez;
    }
    if(heap.size()>1)
    {rez = heap[0];
    heap[0] = heap[heap.size()-1];

    heap.erase(heap.end()-1);

    heapify(0);

    return rez;}
}


void del(int i)
{

    heap[i].y = INT_MIN;
    while (i != 0 && heap[parinte(i)].y > heap[i].y)
    {
       nod aux=heap[i];
        heap[i]=heap[parinte(i)];
        heap[parinte(i)]=aux;
       i = parinte(i);
    }
    del_min();
}



int main()
{
    int n;
    fin>>n;
    int i=0;
    while(n--)
    {

        int op;
        fin>>op;
        if(op==1)
        {
            nod x;
            fin>>x.y;

            insrt(x);

        }
        else if(op==2)
        {
            int x;
            fin>>x;
            int i=0;
            while(x!=heap[i].x&&i<heap.size())
                i++;
            del(i);
        }
        else
        {
            fout<<heap[0].y<<"\n";
        }

    }
    return 0;
}