Cod sursa(job #2427533)

Utilizator PrekzursilAndrei Visalon Prekzursil Data 31 mai 2019 23:13:44
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <bits/stdc++.h>
#define NM 200005
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int k,poz,heap[NM][3],pozitii[NM];
void UpHeap(int poz)
{   int Father=poz/2;
    if(Father && heap[poz][1]<heap[Father][1])
    {   swap(heap[poz],heap[Father]);
        swap(pozitii[heap[poz][2]],pozitii[heap[Father][2]]);
        UpHeap(Father);
    }
}
void DownHeap(int poz)
{   int son=poz*2;
    if(son+1<=k && heap[son+1][1]<heap[son][1]) son++;
    if(son<=k && heap[son][1]<heap[poz][1])
    {   swap(heap[son],heap[poz]);
        swap(pozitii[heap[poz][2]],pozitii[heap[son][2]]);
        DownHeap(son);
    }
}
void InsertHeap(int x)
{   k++;
    heap[k][1]=x;
    heap[k][2]=poz;
    pozitii[poz]=k;
    UpHeap(k);
}
void DeleteHeap(int x)
{   int poz=pozitii[x];
    heap[poz][1]=heap[k][1];
    heap[poz][2]=heap[k][2];
    pozitii[heap[k][2]]=poz;
    k--;
    int Father=poz/2;
    if(poz>1 && heap[poz][1]<heap[Father][1]) UpHeap(poz);
        else DownHeap(poz);
}
int main()
{   int n;
    f>>n;
    while(n--)
    {   int t;
        f>>t;
        if(t==1)
        {   poz++;
            int x;
            f>>x;
            InsertHeap(x);
        }
        else
            if(t==2)
            {   int x;
                f>>x;
                DeleteHeap(x);
            }
            else g<<heap[1][1]<<'\n';
    }
    return 0;
}