Cod sursa(job #2945652)

Utilizator AndreibatmanAndrei Croitoriu Andreibatman Data 23 noiembrie 2022 23:12:33
Problema Heapuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int heap[200010],op,x,cnt,p[200010],pos,n,i,sch;
set<int>s;
void inserare(int val,int pozitie)
{
    heap[++cnt]=val;
    p[cnt]=pozitie;
    pos=cnt;
    while(pos>1 && heap[pos]<heap[pos/2])
    {
        swap(heap[pos],heap[pos/2]);
        swap(p[pos],p[pos/2]);
        pos/=2;
    }
}
void stergere()
{
    heap[1]=heap[cnt];
    p[1]=p[cnt];
    heap[cnt]=p[cnt]=0;
    cnt--;
    pos=1;
    while(pos*2<=cnt)
    {
        sch=pos;
        if(heap[pos*2]<heap[pos])
            sch=pos*2;
        if(pos*2+1<=cnt && heap[pos*2+1]<heap[pos])
            sch=pos*2+1;
        if(sch==pos)
            return;
        swap(heap[pos],heap[sch]);
        swap(p[pos],p[sch]);
        pos=sch;
    }
}
int main()
{
    fin>>n;
    while(n--)
    {
        fin>>op;
        if(op==1)
        {
            i++;
            fin>>x;
            inserare(x,i);
        }
        else if(op==2)
        {
            fin>>x;
            s.insert(x);
            while(s.size() && s.find(p[1])!=s.end())
                stergere();
        }
        else fout<<heap[1]<<'\n';
    }
    return 0;
}