Cod sursa(job #2952876)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 10 decembrie 2022 09:53:46
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
typedef long long ll;
int n,x,h[400005],lg;
int where[400005],value[400005];
void put(int v)
{
    lg++;
    value[lg]=v;
    if(lg==1)
    {
        h[1]=lg;
        where[lg]=1;
        return;
    }
    int poz=lg;
    while(poz>1)
    {
        if(value[h[poz/2]]>v)
        {
            where[h[poz/2]]=poz;
            h[poz]=h[poz/2];
            poz/=2;
        }
        else
            break;
    }
    h[poz]=lg;
    where[lg]=poz;
}
void out(int poz)
{
    while(poz*2<=lg)
    {
        if(poz*2+1>lg||value[h[poz*2]]<value[h[poz*2+1]])
        {
            swap(h[poz],h[poz*2]);
            where[h[poz]]=poz;
            poz=poz*2;
            continue;
        }
        else
        {
            swap(h[poz],h[poz*2+1]);
            where[h[poz]]=poz;
            poz=poz*2+1;
            continue;
        }
    }
}
int main()
{
    int q;
    fin>>q;
    while(q--)
    {
        int tip;
        fin>>tip;
        if(tip==1)
        {
            int x;
            fin>>x;
            put(x);
            continue;
        }
        if(tip==2)
        {
            int x;
            fin>>x;
            out(where[x]);
            continue;
        }
        if(tip==3)
            fout<<value[h[1]]<<'\n';
    }
    return 0;
}