Cod sursa(job #2475651)

Utilizator hoprixVlad Opris hoprix Data 17 octombrie 2019 11:53:42
Problema Heapuri Scor 0
Compilator cpp-32 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

#define Nmax 2000005

int N, Heap[Nmax], Val[Nmax], Pos[Nmax], i;

bool cmp(int x, int y) {
    return x < y;
}

void Swap(int x, int y) {
    swap(Heap[x], Heap[y]);
}
void Up(int k) {
    int f = k / 2;
    if(f && cmp(Val[Heap[k]], Val[Heap[f]])) {
        Swap(k, f);
        Up(f);
    }
}

void Down(int k) {
    int s = k * 2;
    if(s + 1 <= N && cmp(Val[Heap[s]], Val[Heap[s + 1]]))s++;
    if(s <= N && cmp(Val[Heap[s]], Val[Heap[k]])) {
        Swap(k, s);
        Down(s);
    }
}

void Insert(int x) {
    i++;
    Val[i] = x;
    N++;
    Heap[N] = i;
    Pos[i] = N;
    Up(N);
}

void Erase(int x) {
    Swap(x, N);
    N--;
    Down(x);
}

int main() {
    int M;
    fin >> M;
    int operation, number;
    for(; M; M--) {
        fin >> operation;
        if(operation == 1)fin >> number, Insert(number);
        if(operation == 2)fin >> number, Erase(number);
        if(operation == 3)fout << Val[Heap[1]] << "\n";
    }
}