Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#define MAX 2000200
#define INT_MIN -2000000300
using namespace std;
typedef long long ll;

class priority_queue {
private:
  int index;
  int Array[MAX];

  int heapIncreaseKey(int i, ll key) {
    if (key < Array[i])
      return -1;
    Array[i] = key;
    while (i > 1 && Array[getParent(i)] < Array[i]) {
      ll h = Array[i];
      Array[i] = Array[getParent(i)];
      Array[getParent(i)] = h;
      i = getParent(i);
    }
  }

  void maxHeapify(int i) {
    ll l = getLeft(i);
    ll r = getRight(i);
    ll largest;
    if (l <= index && Array[l] > Array[i])
      largest = l;
    else
      largest = i;
    if (r <= index && Array[r] > Array[largest])
      largest = r;
    if (largest != i) {
      ll h;
      h = Array[i];
      Array[i] = Array[largest];
      Array[largest] = h;
      maxHeapify(largest);
    }
  }

public:
  priority_queue() { index = 0; }

  void Insert(ll key) { // maxHeapInsert
    ++index;
    Array[index] = INT_MIN;
    heapIncreaseKey(index, key);
  }

  ll heapExtractMax() {
    if (index < 1)
      return -1;
    ll max = Array[1];
    Array[1] = Array[index];
    index--;
    maxHeapify(1);
    return max;
  }

  int getParent(int i) { return i / 2; }

  int getLeft(int i) { return 2 * i; }

  int getRight(int i) { return 2 * i + 1; }
};

int main() {
  string op;
  ll val;
  priority_queue pque = priority_queue();

  while (true) {
    cin >> op;
    if (op == "insert") {
      scanf("%lld", &val);
      pque.Insert(val);
    } else if (op == "extract") {
      printf("%lld\\n", pque.heapExtractMax());
    } else if (op == "end")
      break;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

struct node {
  bool active = true;
  int k;
  node *next = nullptr;
  node *prev = nullptr;
};

class PriorityQueue {
private:
  node *max = nullptr;
  std::vector<node *> S;

public:
  void insert(int k);
  int extractMax();
};

void PriorityQueue::insert(int k) {
  node *n = new node();
  n->k = k;
  node *target = max;
  node *target_pre = nullptr;
  while (target != nullptr) {
    if (target->k < k)
      break;
    target_pre = target;
    target = target->next;
  }
  if (target_pre == nullptr) {
    if (target != nullptr) {
      target->prev = n;
      n->next = target;
    }
    max = n;
  } else if (target == nullptr) {
    target_pre->next = n;
    n->prev = target_pre;
  } else {
    n->next = target;
    target->prev->next = n;
    n->prev = target->prev;
    target->prev = n;
  }
  if (target == max)
    max = n;
  S.push_back(n);
}

int PriorityQueue::extractMax() {
  if (max == nullptr)
    return -1;
  int ans = max->k;
  // for (int i = 0; i < (int)S.size(); ++i) {
  //   if (S[i] == max) {
  //     S[i]->active = false;
  //     break;
  //   }
  // }
  if (max->next != nullptr)
    max->next->prev = nullptr;
  max = max->next;
  return ans;
}

int main() {
  PriorityQueue pq;
  while (1) {
    std::string str;
    std::cin >> str;
    if (str == "end")
      break;
    if (str == "insert") {
      int i;
      std::cin >> i;
      pq.insert(i);
    } else if (str == "extract") {
      int i = pq.extractMax();
      std::cout << i << std::endl;
    }
  }
}