Cod sursa(job #1669470)

Utilizator emanuel_rRamneantu Emanuel emanuel_r Data 30 martie 2016 19:02:43
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<cmath>
#include<queue>
#include<bitset>

using namespace std;

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

int n, Heap_Size;
int Heap[200005], Poz[200005], D[200005];

void Swap(int hx, int hy)
{
    swap(Heap[hx], Heap[hy]);
    swap(Poz[Heap[hx]], Poz[Heap[hy]]);
}

void UpHeap(int nod)
{
    int tata = nod/2;
    if(tata && D[Heap[nod]] < D[Heap[tata]]){
        Swap(tata, nod);
        UpHeap(tata);
    }
}

void DownHeap(int nod)
{
    int son = 2*nod;
    if(son + 1 <= Heap_Size && D[Heap[son+1]] < D[Heap[son]])
        son++;
    if(son <= Heap_Size && D[Heap[son]] < D[Heap[nod]]){
        Swap(nod, son);
        DownHeap(son);
    }
}

int main()
{
    int op, i, nrel = 0, x, pos;

    f>>n;
    for(i=1; i<=n; i++){
        f>>op;
        if(op == 1){
            f>>x;
            Poz[++nrel] = ++Heap_Size;
            Heap[Heap_Size] = nrel;
            D[nrel] = x;
            UpHeap(Heap_Size);
        }
        if(op == 2){
            f>>x;
            pos = Poz[x];
            Swap(Poz[x], Heap_Size--);
            UpHeap(pos); DownHeap(pos);
        }
        if(op == 3)
            g<<D[Heap[1]]<<"\n";
    }

    return 0;
}