Cod sursa(job #3163632)

Utilizator JungwonPreppy Girl Jungwon Data 31 octombrie 2023 18:35:33
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,ceva,x,op,heapSize,k,poz[200005];
struct heapp
{
    int nr,ind;
} heap[200005];
void up(int p)
{
    while(p>1 and heap[p].nr<heap[p/2].nr)
    {
        swap(heap[p],heap[p/2]);
        swap(poz[heap[p].ind],poz[heap[p/2].ind]);
        p=p/2;
    }
}
void down(int p)
{

    while(p*2<=heapSize)
    {
        int copil=p*2;
        if(copil+1<=heapSize and heap[copil].nr>heap[copil+1].nr)
            copil++;
        if(heap[p].nr>heap[copil].nr)
        {
            swap(heap[p],heap[copil]);
            swap(poz[heap[p].ind],poz[heap[copil].ind]);
            p=copil;
        }
        else
            break;
    }
}
void del(int p)
{
    swap(heap[heapSize],heap[p]);
    swap(poz[heap[heapSize].ind],poz[heap[p].ind]);
    heapSize--;
    up(p);
    down(p);
}
void add(int x)
{
    heap[++heapSize].nr=x;
    heap[heapSize].ind=++ceva;
    poz[ceva]=heapSize;
    up(heapSize);
}
int main()
{
    f>>n;
    for(int i=1; i<=n; i++)
    {
        f>>op;
        if(op==1)
        {
            f>>x;
            add(x);
        }
        if(op==2)
        {
            f>>x;
            del(poz[x]);
        }
        if(op==3)
            g<<heap[1].nr<<'\n';
    }
    return 0;
}