Cod sursa(job #2294603)

Utilizator ApostolIlieDanielApostol Daniel ApostolIlieDaniel Data 2 decembrie 2018 16:40:09
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>

using namespace std;

FILE *fin = fopen ("heapuri.in", "r"), *fout = fopen ("heapuri.out", "w");

const int MAXN = 2e5;

int yep[MAXN + 1], poz[MAXN + 1], v[MAXN + 1];
int sz, nr;

void bambo (int x, int y) {
  swap (yep[x], yep[y]);
  poz[yep[x]] = x;
  poz[yep[y]] = y;
}

void up (int nod) {
  while (nod != 1 && yep[nod] < yep[nod / 2]) {
    bambo (nod, nod / 2);
    nod = nod / 2;
  }
}

void chromosomes (int nod) {
  int hump = nod;
  if (2 * nod <= sz && yep[2 * nod] < yep[hump])
    hump = 2 * nod;
  if (2 * nod + 1 <= sz && yep[2 * nod + 1] < yep[hump])
    hump = 2 * nod;
  if (hump != nod) {
    bambo (nod, hump);
    chromosomes (hump);
  }
}

void erase (int x) {
  if (x < sz) {
    yep[x] = yep[sz--];
    poz[yep[x]] = x;
    chromosomes (x);
    up (x);
  }
  else {
    sz--;
  }
}

void add (int x) {
  yep[++sz] = x;
  poz[x] = sz;
  up (sz);
}

int main() {
  int t, type, x;
  fscanf (fin, "%d", &t);
  sz = 0;
  nr = 0;
  while (t--) {
    fscanf (fin, "%d", &type);
    if (type == 1) {
      fscanf (fin, "%d", &x);
      v[++nr] = x;
      add (x);
    }
    else if (type == 2) {
      fscanf (fin, "%d", &x);
      x = v[x];
      erase (poz[x]);
    }
    else {
      fprintf (fout, "%d\n", yep[1]);
    }
  }
  fclose (fin);
  fclose (fout);
  return 0;
}