Cod sursa(job #2916740)

Utilizator RobertlelRobert Robertlel Data 1 august 2022 11:34:48
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

int heap[200005],poz[200005],i,n,c,z,x,val[200005];

bool cmp(int a,int b)
{
    return a<b;
}

void upheap(int x)
{
    int father=x/2;
    if(father&&cmp(heap[x],heap[father]))
        {  swap(heap[x],heap[father]);
            upheap(father);
        }
}

void ins(int x)
{
    n++;
    heap[n]=x;
    poz[n]=x;
    val[x]=n;
    upheap(n);
}

void downheap(int x)
{
    int son=x*2;
    if(son+1<=n&&cmp(heap[son+1],heap[son]))
        son=son+1;
    if(son<=n&&cmp(heap[son],heap[x]))
      {
          swap(heap[son],heap[x]);
          downheap(son);
      }

}

void pop(int x)
{
    swap(heap[x],heap[n]);
    n--;
    downheap(x);
}

int search_in_heap(int x)
{
    int st=0,dr=n,mij=0;;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(heap[mij]<x)
           st++;
           if(heap[mij]>x)
            dr--;
           if(heap[mij]==x)
            return mij;
    }
}

int main()
{
     f>>z;
     for(int i=1;i<=z;i++)
     {
         f>>c;
         if(c==1)
         {
             f>>x;
             ins(x);
         }
         if(c==2)
         {
             f>>x;
             pop(search_in_heap(poz[x]));

         }
         if(c==3)
         {
             g<<heap[1]<<'\n';
         }
     }
    return 0;
}