Cod sursa(job #3153685)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 30 septembrie 2023 18:07:24
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>
#define st(x) (2 * x)
#define dr(x) (2 * x + 1)
#define tata(x) (x / 2)

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int t, op, x, n, h[200002];
int poz[200002], val[200002], el;

static inline void inv(int x, int y) {
    swap(h[x], h[y]);
    swap(poz[h[x]], poz[h[y]]);
}

static inline void add(int x) {
    int t = tata(x);
    if(t > 0 && val[h[x]] < val[h[t]]) {
        inv(x, t);
        add(t);
    }
}

static inline void del(int x) {
    int f = x * 2;
    if(f + 1 <= n && val[h[f + 1]] < val[h[f]]) f++;

    if(f <= n && val[h[f]] < val[h[x]]) {
        inv(f, x);
        del(f);
    }
}

static inline void Adauga(int x) {
    n++;
    el++;

    h[n] = el;
    poz[el] = n;
    val[el] = x;

    add(n);
}

static inline void Sterge(int x) {
    inv(x, n);
    n--;

    del(x);
}

int main() {
    h[0] = 1e9;
    fin >> t;
    while(t--) {
        fin >> op;
        if(op == 1) {
            fin >> x;
            Adauga(x);
        }
        else if(op == 2) {
            fin >> x;
            Sterge(poz[x]);
        }
        else fout << val[h[1]] << "\n";
    }

	return 0;
}