Cod sursa(job #2500211)

Utilizator vergilius_beberindeie virgil vergilius_be Data 27 noiembrie 2019 14:20:00
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include <fstream>
using namespace std;
ifstream cin ("heapuri.in");
ofstream cout ("heapuri.out");
long long n,m,heap[200100],heapz[210000],l,frecv[200010];
void percol (int poz)
{ long long w=heap[poz],w1=heapz[poz];
while(4>3)
    {
    if( heap[poz/2]>w && poz>1 )
    frecv[heapz[poz/2]]=poz,heap[poz]=heap[poz/2],heapz[poz]=heapz[poz/2],poz/=2;
    else
    {
        heap[poz]=w; heapz[poz]=w1; frecv[w1]=poz; break;
    }
    }
}
void sift (int poz)
{
    long long y,w=heap[poz],w1=heapz[poz];
    while(5>1)
        {
           // if(poz/2+1<=m)
           // {
                if(heap[poz*2]<heap[poz*2+1]) y=poz*2; else y=poz*2+1;
           // }
            //else y=poz*2;
        if (y<=m && w>heap[y])
        {
            frecv[heapz[y]]=poz; heap[poz]=heap[y]; heapz[poz]=heapz[y]; poz=y;
        }
        else
            {
                heap[poz]=w; heapz[poz]=w1; frecv[w1]=poz; break;
            }
        }
}
void read ()
{
    long long p,x;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>p;
        if(p==1)
        {
            cin>>x;
            heap[++m]=x; l++; heapz[m]=l; frecv[l]=m;
            percol(m);
        }
        else if(p==2)
        {
            cin>>x;
            heap[frecv[x]]=heap[m];  frecv[heapz[m]]=frecv[x]; heapz[frecv[x]]=heapz[m];   m--;
            percol(frecv[x]);
            sift(frecv[x]);
        }
    else cout<<heap[1]<<"\n";
    }
}
int main()
{
    read();
    cin.close();
    cout.close();
    return 0;

}