Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>

struct Node {
  int index, val;
  bool operator<(Node n) { return this->val > n.val; }
  bool operator>(Node n) { return this->val < n.val; }
  Node() { this->index = 0; }
};

class Min_heap {
private:
  Node **n; //?????????????????????
  int N;    //????°????index
  void swap(int a_index, int b_index) {
    Node *temp = n[b_index];
    n[b_index] = n[a_index];
    n[a_index] = temp;
    n[a_index]->index = a_index;
    n[b_index]->index = b_index;
  }

public:
  Min_heap(int size) {
    this->n = new Node *[size + 1];
    std::fill_n(n, size + 1, (Node *)NULL);
    this->N = 0;
  }
  void clear() {
    for (int i = 0; i < N; i++) {
      n[i]->index = 0;
      n[i] = NULL;
    }
  }
  bool isEmpty() { return N == 0; }
  void push(Node &e) {
    int i = ++N;
    n[i] = &e;
    e.index = i;
    while (i > 1) {
      if (*n[i / 2] > *n[i]) {
        swap(i / 2, i);
        i = i / 2;
      } else {
        break;
      }
    }
  }
  void remove(Node &a) {
    int index = a.index;
    swap(index, N);
    n[N]->index = 0;
    n[N--] = NULL;
    int i = index;
    while (i < N) {
      if (i * 2 + 1 <= N) {
        if (*n[i * 2] < *n[i * 2 + 1]) {
          if (*n[i] > *n[i * 2]) {
            swap(i, i * 2);
            i = i * 2;
            continue;
          }
        } else {
          if (*n[i] > *n[i * 2 + 1]) {
            swap(i, i * 2 + 1);
            i = i * 2 + 1;
            continue;
          }
        }
      } else if (i * 2 <= N) {
        if (*n[i] > *n[i * 2]) {
          swap(i, i * 2);
          i = i * 2;
          continue;
        }
      }
      break;
    }
  }
  Node *pop() {
    Node *p = n[1];
    remove(*n[1]);
    return p;
  }
};

int main(void) {
  char str[10];
  Min_heap que = Min_heap(2000000);
  Node *V = new Node[2000000];
  int i = 0;
  while (std::scanf("%s", str), str[2] != 'd') {
    if (str[0] == 'i') {
      int k;
      std::scanf("%d", &k);
      V[i].val = k;
      que.push(V[i]);
    } else {
      std::printf("%d\\n", que.pop()->val);
    }
    i++;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

struct Node {
  int index, val;
  bool operator<(Node n) { return this->val > n.val; }
  bool operator>(Node n) { return this->val < n.val; }
  Node() { this->index = 0; }
};

class Min_heap {
private:
  Node **n; //?????????????????????
  int N;    //????°????index
  void swap(int a_index, int b_index) {
    Node *temp = n[b_index];
    n[b_index] = n[a_index];
    n[a_index] = temp;
    n[a_index]->index = a_index;
    n[b_index]->index = b_index;
  }

public:
  Min_heap(int size) {
    this->n = new Node *[size + 1];
    std::fill_n(n, size + 1, (Node *)NULL);
    this->N = 0;
  }
  void clear() {
    for (int i = 0; i < N; i++) {
      n[i]->index = 0;
      n[i] = NULL;
    }
  }
  ~Min_heap() {
    clear();
    delete n;
  }
  bool isEmpty() { return N == 0; }
  void push(Node &e) {
    int i = ++N;
    n[i] = &e;
    e.index = i;
    while (i > 1) {
      if (*n[i / 2] > *n[i]) {
        swap(i / 2, i);
        i = i / 2;
      } else {
        break;
      }
    }
  }
  void remove(Node &a) {
    int index = a.index;
    swap(index, N);
    n[N]->index = 0;
    n[N--] = NULL;
    int i = index;
    while (i < N) {
      if (i * 2 + 1 <= N) {
        if (*n[i * 2] < *n[i * 2 + 1]) {
          if (*n[i] > *n[i * 2]) {
            swap(i, i * 2);
            i = i * 2;
            continue;
          }
        } else {
          if (*n[i] > *n[i * 2 + 1]) {
            swap(i, i * 2 + 1);
            i = i * 2 + 1;
            continue;
          }
        }
      } else if (i * 2 <= N) {
        if (*n[i] > *n[i * 2]) {
          swap(i, i * 2);
          i = i * 2;
          continue;
        }
      }
      break;
    }
  }
  Node *pop() {
    Node *p = n[1];
    remove(*n[1]);
    return p;
  }
};

int main(void) {
  char str[10];
  Min_heap que = Min_heap(2000000);
  Node *V = new Node[2000000];
  int i = 0;
  while (std::scanf("%s", str), str[2] != 'd') {
    if (str[0] == 'i') {
      int k;
      std::scanf("%d", &k);
      V[i].val = k;
      que.push(V[i]);
    } else {
      std::printf("%d\\n", que.pop()->val);
    }
    i++;
  }

  return 0;
}