Cod sursa(job #3342907)

Utilizator tedicTheodor Ciobanu tedic Data 26 februarie 2026 02:28:38
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.83 kb
#include <bits/stdc++.h>

using namespace std;
struct date{
int val, ind;
}heap[200005];
int poznod[200005]; ///in ce nod se afla numerele in functie de ordinea lor cronologica
int parinte(int nod)
{
    return nod/2;
}
void swap_nodes(int nod1, int nod2)
{
    swap(heap[nod1], heap[nod2]);
    swap(poznod[heap[nod1].ind], poznod[heap[nod2].ind]);
}
int fiu_st(int nod)
{
    return 2*nod;
}
int fiu_dr(int nod)
{
    return 2*nod+1;
}
void compar_sus(int poz)
{
    while(poz>=1 && heap[poz].val<heap[parinte(poz)].val)
    {
        swap_nodes(poz, parinte(poz));
        poz=parinte(poz);
    }
}
void compar_jos(int poz, int &heapsize)
{
    int fiu=0;
    if(fiu_st(poz)<=heapsize)
    {
        fiu=fiu_st(poz);
        if(fiu_dr(poz)<=heapsize && heap[fiu_dr(poz)].val<heap[fiu_st(poz)].val)
            fiu=fiu_dr(poz);
    }
    if(!fiu)
        return;
    if(heap[fiu].val<heap[poz].val)
    {
        swap_nodes(fiu, poz);
        compar_jos(fiu, heapsize);
    }
}
void insert_nod(int val, int indice, int &heapsize)
{
    heapsize++;
    heap[heapsize]={val, indice};
    poznod[indice]=heapsize;
    compar_sus(heapsize);
}
void delete_nod(int poz, int &heapsize)
{
    int nod=poznod[poz];
    swap_nodes(nod, heapsize);
    heapsize--;
    compar_sus(nod);
    compar_jos(nod, heapsize);
}
int main()
{
    ifstream cin("heapuri.in");
    ofstream cout("heapuri.out");
    int q,cnt=0, heapsize=0;
    cin>>q;
    while(q--)
    {
        int tip, x;
        cin>>tip;
        if(tip==1)
        {
            cin>>x;
            cnt++;
            insert_nod(x, cnt, heapsize);
        }
        if(tip==2)
        {
            cin>>x;
            delete_nod(x, heapsize);
        }
        if(tip==3)
            cout<<heap[1].val<<'\n';
    }
    return 0;
}