Cod sursa(job #2334314)

Utilizator vladsirbu23Vlad Sirbu vladsirbu23 Data 2 februarie 2019 14:57:55
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <iostream>
#include <fstream>
#define f first
#define s second
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
pair <int,int> v[200010];
int N,cer,K=0,fv[200010],nr,aux,x;
int main()
{
    int i,j;
    fin>>N;
    for(i=1; i<=N; i++)
    {
        fin>>cer;
        if(cer==1)
        {
            fin>>x;
            K++;
            nr++;
            v[K].f=x;
            v[K].s=nr;
            aux=K;
            while(v[aux]<v[aux/2])
            {
                swap(v[aux],v[aux/2]);
                aux=aux/2;
            }
        }
        else
        {
            if(cer==3)
            {
                fout<<v[1].f<<'\n';
            }
            else
            {
                fin>>x;
                fv[x]=1;
                while(fv[v[1].s]==1)
                {
                    v[1]=v[K];
                    v[K].f=0;
                    v[K].s=0;
                    K--;
                    j=1;
                    while((v[j].f>min(v[2*j+1].f,v[2*j].f)&&(2*j+1)<=K)||(v[j].f>v[2*j].f&&2*j<=K))
                    {
                        if(v[2*j].f>v[2*j+1].f&&(2*j+1)<=K)
                        {
                            swap(v[2*j+1],v[j]);
                            j=2*j+1;
                        }
                        else
                        {
                            swap(v[2*j],v[j]);
                            j=2*j;
                        }
                    }
                }
            }
        }
    }
}