Cod sursa(job #3252660)

Utilizator ArthurrrfubinacaARthur Paun Arthurrrfubinaca Data 30 octombrie 2024 16:07:17
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 kb
#include <iostream>
#include <fstream>
#define Nmax 200001
using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int n,nr,lung,cod,x,position;
int poz[Nmax];
pair<int,int> H[Nmax+5];

void push_top(int x)
{
    while(x>1)
    {
        if(H[x].first<H[x/2].first)
        {
            swap(H[x],H[x/2]);
            swap(poz[H[x].second],poz[H[x/2].second]);
            x/=2;
        }else break;
    }
}

void push_down(int x ,int n)
{
    while(2*x<=n)
    {
        int p=2*x;
        if(p+1<=n && H[p+1].first<H[p].first)
            p++;
        if(H[x].first<H[p].first)
            return;
        else
        {
            swap(H[x],H[p]);
            swap(poz[H[x].second],poz[H[p].second]);
            x=p;
            //cout<<" ... " <<H[x]<< " " << H[p]<<endl;
        }
    }
}

int main()
{
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        //for(int j=1;j<=lung;j++)
            //cout<<H[j]<<" ";
        //cout<<endl;
        fin>>cod;
        if(cod==1)
        {
            fin>>x;
            nr++ , lung++;
            poz[nr]=lung;
            H[lung].first=x;
            H[lung].second=nr;
            push_top(lung);
        }
        else
        if(cod==2)
        {
            fin>>position;
            int p=poz[position];
            swap(H[p],H[lung]);
            swap(poz[H[p].second],poz[H[lung].second]);
            lung--;
            push_top(p);
            push_down(p,lung);
        }
        else
            fout<<H[1].first<<'\n';
    }
    return 0;
}