Cod sursa(job #2809754)

Utilizator DajaMihaiDaja Mihai DajaMihai Data 27 noiembrie 2021 15:48:25
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
#include <fstream>
#include <stdio.h>

using namespace std;

struct ffff {
    int val;
    int poz;
};

ffff heap [200002];
int n, cnt = 1, nr, lpos, i;

int parent (int n){
    return (n - 1) / 2;
}

int lchild (int n){
    return n * 2 + 1;
}
int rchild (int n){
    return n * 2 + 2;
}


void upHeap(int i) {
  while (i && heap[parent(i)] > heap[i]) {
    swap(heap[parent(i)], heap[i]);
    i = parent(i);
  }
}


void propagDown( int i ) {
    int minn = i;
    if ( lchild( i ) < lpos && heap[lchild( i )].val < heap[minn].val )
        minn = lchild( i );
    if ( rchild( i ) < lpos && heap[rchild( i )].val < heap[minn].val )
        minn = rchild( i );
    if ( minn != i ) {
        swap( heap[i], heap[minn] );
        propagDown( minn );
    }
}

int removeMin() {
    ffff x = heap[0];
    heap[0] = heap[-- lpos];
    propagDown( 0 );
}
int main()
{
    ifstream in ("lupu.in");
    ofstream out ("lupu.out");


    int p;
    in >> n;
    for (i = 0; i < n; i ++){
        in >> p;
        if (p == 1){
            in >> nr;
            ord [cnt] = nr; cnt ++;
            heap [lpos] = nr; upHeap(lpos); lpos ++;
        }
        else if (p == 3)
            out << heap[0] << '\n';
        else if (p == 2){
            in >> nr;
            int x = findStuff(ord[nr]);
            extract (x);
        }
    }
    return 0;
}