Cod sursa(job #3126524)

Utilizator Botnaru_VictorBotnaru Victor Botnaru_Victor Data 6 mai 2023 18:24:09
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <bits/stdc++.h>

using namespace std;

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


const int MN = 200005;
const int INF = 0x3f3f3f3f;

//valorile numerelor (in ordine cronologica)
int v[MN], nr;
bool isDeleted[MN];

// tine minte o pozitie din v
int h[MN], hk;


//x este o pozitie in heap
void upheap(int x)
{
    if( v[h[x]] < v[h[x/2]])
    {
        swap(h[x], h[x/2]);

        upheap(x/2);

    }
}

void downheap(int x)
{
    if(x*2>hk) return;
    int minCopil = x*2;

    if(x*2+1<=hk && v[h[minCopil]] > v[h[x*2+1]]) minCopil= x*2+1;

    if( v[h[x]] > v[h[minCopil]])
    {
        swap( h[x], h[minCopil]);
        downheap(minCopil);
    }
}

void deleteTop()
{
    swap( h[1], h[hk]);
    hk--;
    downheap(1);
}

void addElement(int val)
{
    nr++;
    v[nr] = val;
    hk++;
    h[hk] = nr;

    upheap(hk);
}

void deleteElement(int x)
{
    isDeleted[x]=1;
}


int main()
{
    int n;
    int q,a;

    f>>n;

    for(int i=1;i<=n;i++)
    {
        f>>q;
        if(q==1)
        {
            f>>a;
            addElement(a);
        }
        if(q==2)
        {
            f>>a;
            deleteElement(a);
        }
        if(q==3)
        {
            while(isDeleted[h[1]])
            {
                deleteTop();
            }

            g<<v[h[1]]<<'\n';
        }
    }

    return 0;
}