Cod sursa(job #2699457)

Utilizator Mihai180315Mihai Smarandache Mihai180315 Data 24 ianuarie 2021 16:10:06
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <fstream>

using namespace std;
const int nmax = 50000;

int h[nmax + 1], u[nmax + 1], v[nmax + 1];
int h_size;

void heap_swap(int a, int b)
{
    swap(u[h[a]], u[h[b]]);
    swap(h[a], h[b]);
}
void heap_up(int p)
{
    int f = p / 2;
    while(v[h[p]] < v[h[f]]) {
        heap_swap(p, f);
        p = p / 2;
        f = f / 2;
    }
}
void heap_down(int p)
{
    int best, l, r;
    while(2 * p <= h_size) {
        l = 2 * p;
        best = l;
        r = 2 * p + 1;
        if(v[h[p]] > v[h[best]]) {
            heap_swap(p, best);
        } else {
            break;
        }
    }
    p = best;
}
void heap_insert(int x)
{
    ++h_size;
    h[h_size] = x;
    u[x] = h_size;
    heap_up(h_size);
}
void heap_erase(int x)
{
    int p = u[x];
    heap_swap(p, h_size);
    --h_size;
    heap_up(p);
    heap_down(p);
}
void heap_update(int x)
{
    int p = u[x];
    heap_up(p);
    heap_down(p);
}


int main()
{
    ifstream fin("heapuri.in");
    ofstream fout("heapuri.out");
    int n, tip, v_size = 0, x;
    fin >> n;
    for(int i = 1; i <= n; ++i) {
        fin >> tip;
        if(tip == 1) {
            fin >> x;
            ++v_size;
            v[v_size] = x;
            heap_insert(v_size);
        } else if(tip == 2) {
            fin >> x;
            heap_erase(x);
        }else if(tip == 3) {
            fout << v[h[1]] << "\n";
        }
    }
    fin.close();
    fout.close();
    return 0;
}