Cod sursa(job #3252826)

Utilizator rockoanaOana Pasca rockoana Data 31 octombrie 2024 12:06:20
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.9 kb
/*
                    __
                   /\ \
 _ __   ___     ___\ \ \/'\     ___      __      ___     ___      __
/\`'__\/ __`\  /'___\ \ , <    / __`\  /'__`\  /' _ `\ /' _ `\  /'__`\
\ \ \//\ \L\ \/\ \__/\ \ \\`\ /\ \L\ \/\ \L\.\_/\ \/\ \/\ \/\ \/\ \L\.\_
 \ \_\\ \____/\ \____\\ \_\ \_\ \____/\ \__/.\_\ \_\ \_\ \_\ \_\ \__/.\_\
  \/_/ \/___/  \/____/ \/_/\/_/\/___/  \/__/\/_/\/_/\/_/\/_/\/_/\/__/\/_/


*/

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
using i64 = int64_t;

class heap {
 private:
  struct strct {
    int val, pos;
  };
  strct heap[800008];
  int pos[200004];
  int len = 0;

  void swamp(int x, int y) {
    swap(heap[x], heap[y]);
    swap(pos[heap[x].pos], pos[heap[y].pos]);
  }

  void up(int p) {
    while (p > 1 && heap[p].val < heap[p / 2].val) {
      swamp(p, p / 2);
      p = p / 2;
    }
  }
  void down(int p) {
    while (p * 2 <= len) {
      int t = p * 2;
      if (p * 2 + 1 < len && heap[t].val > heap[t + 1].val) {
        t++;
      }
      if (heap[t].val < heap[p].val) {
        swamp(t, p);
        p = t;
      } else
        break;
    }
  }

 public:
  int min() { return heap[1].val; }

  void insert(int val, int i) {
    len++;
    heap[len].val = val;
    heap[len].pos = i;
    pos[i] = len;
    up(len);
  }

  void erase(int i) {
    if (pos[i] == len) {
      len--;
    } else {
      int tmp = pos[i];
      swamp(pos[i], len);
      len--;
      up(tmp);
      down(tmp);
    }
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // #ifdef LOCAL
  ifstream cin{"heapuri.in"};
  ofstream cout{"heapuri.out"};
  // #endif

  int n;
  cin >> n;

  heap h;
  int c, x;
  int idx = 1;
  for (int q = 0; q < n; q++) {
    cin >> c;
    if (c == 1) {
      cin >> x;
      h.insert(x, idx);
      idx++;
    } else if (c == 2) {
      cin >> x;
      h.erase(x);
    } else {
      cout << h.min() << endl;
    }
  }

  return 0;
}