Cod sursa(job #2446812)

Utilizator vlad.ulmeanu30Ulmeanu Vlad vlad.ulmeanu30 Data 10 august 2019 19:43:29
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.81 kb
#include <bits/stdc++.h>
#define lll long long
#define pii pair<int,int>
#define pll pair<lll,lll>
#define fi first
#define se second
#define vi vector<int>
#define pb push_back
#define sz(a) (int)(a).size()
#define inf (INT_MAX/2-1)
#define infl (1LL<<61)
#define aaa system("pause");
#define dbg(x) cerr<<(#x)<<' '<<(x)<<'\n',aaa
#define dbga(x,n) cerr<<(#x)<<"[]: ";for(int _=0;_<=n;_++) cerr<<x[_]<<" ";cerr<<'\n'; aaa
#define maxn 200000

using namespace std;

int wh[maxn+5];

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

namespace shp {
  pii heap[8*maxn+5];
  int ne, ii;///ii = insert index
  ///fi=val se=index intrare
  void swapheap (int p, int q) {
    wh[shp::heap[q].se] = p;
    wh[shp::heap[p].se] = q;
    swap (shp::heap[p], shp::heap[q]);
  }
  void upheap (int p) {
    if (p == 1) return;
    if (shp::heap[p/2] > shp::heap[p]) {
      shp::swapheap (p/2, p);
      shp::upheap (p/2);
    }
  }
  void insert (int x) {
    shp::heap[++shp::ne] = {x, shp::ii};
    wh[shp::ii++] = shp::ne;
    shp::upheap (shp::ne);
  }
  void downheap (int p) {
    if (p > ne) return;
    int m = p*2;
    if (shp::heap[m] > shp::heap[m+1]) m++;
    if (shp::heap[p] < shp::heap[m]) return;
    shp::swapheap (p, m);
    shp::downheap (m);
  }
  void erase (int ind) {
    int p = wh[ind];
    shp::swapheap (p, shp::ne);
    shp::heap[shp::ne--] = {inf, 0};
    shp::downheap (p);
  }
  int minheap () { return shp::heap[1].fi; }
}

int main () {
  int q; fin >> q;
  int op, a;
  for (int i = 0; i <= 8*maxn; i++) shp::heap[i] = {inf,0};
  while (q--) {
    fin >> op;
    if (op < 3) fin >> a;
    if (op == 1) shp::insert(a);
    else if (op == 2) shp::erase(a-1);
    else fout << shp::minheap() << '\n';
  }
  fin.close(); fout.close();
  return 0;
}