Cod sursa(job #2916737)

Utilizator RobertlelRobert Robertlel Data 1 august 2022 11:03:57
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 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;

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;
    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)
{
    for(int i=1;i<=n;i++)
    {
        if(heap[i]==x)
            return i;
    }
}

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;
}