Cod sursa(job #2301901)

Utilizator OctavianVasileVasileOctavian OctavianVasile Data 13 decembrie 2018 17:23:22
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
const int MAXN = 2e3;
int g [MAXN + 1], poz [MAXN + 1], v [MAXN + 1];
int k, nr, t, type, x;
void swapx (int a, int b) {
    swap (g [a], g [b]);
    poz [g [a]] = a; poz [g [b]] = b;
}
void up (int nod) {
    while (nod != 1 && v [g [nod]] < v [g [nod / 2]]) {
        swapx (nod, nod / 2);
        nod = nod / 2;
    }
}
void func (int nod) {int nr = nod;
      if (2 * nod <= k && v [g [2 * nod]] < v [g [nr]])nr = 2 * nod;
      if (2 * nod + 1 <= k && v [g [2 * nod + 1]] < v [g [nr]])nr = 2 * nod + 1;
      if (nr != nod)swapx (nod, nr), func (nr);
}
void erasex (int x){
      if (x < k){
        g [x] = g [k --], poz [g [x]] = x;
        func (x), up (x);
      }
      else k --;
}
void add (int x){
      g [++ k] = x, poz [x] = k, up (k);
}
int main (){
    cin >> t;
    for (int i = 0; i < t; i ++){
        cin >> type;
        if (type == 1)cin >> x, v [++ nr] = x, add (nr);
        else if (type == 2)cin >> x, erasex (poz [x]);
        else cout << v [g [1]] << '\n';
    }
    return 0;
}