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

#include <cassert>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define TOPINDEX 1
#define DUMMYVALUE 0

void Insert(vector<int> &Heap, int k) {
  Heap.push_back(k);
  int InsertIndex = Heap.size() - 1;
  int ParentIndex = (Heap.size() - 1) / 2;
  while (Heap[InsertIndex] > Heap[ParentIndex] && InsertIndex > TOPINDEX) {
    swap(Heap[InsertIndex], Heap[ParentIndex]);
    InsertIndex = ParentIndex;
    ParentIndex = InsertIndex / 2;
  }
}

void maxHeapify(vector<int> &A, int i) {
  int H = A.size() - 1;
  int l = 2 * i;
  int r = 2 * i + 1;
  //????????????????????????????????§???????????§????????????????????¶
  int largest;
  if (l <= H && A[l] > A[i]) {
    largest = l;
  } else {
    largest = i;
  }
  if (r <= H && A[r] > A[largest]) {
    largest = r;
  }
  // i???????????????????????§????????´???
  if (largest != i) {
    swap(A[i], A[largest]);
    //?????°??????????????????
    maxHeapify(A, largest);
  }
}

void ExtractMax(vector<int> &Heap) {
  int TailIndex = Heap.size() - 1;
  swap(Heap[TailIndex], Heap[TOPINDEX]);
  cout << Heap[TailIndex] << endl;
  Heap.pop_back();
  maxHeapify(Heap, TOPINDEX);
}

int main() {
  /* ??????
  MaxHeap?????¢??§?????????
  1??????Exract????????????????????¨????°??????\\????????????????°??????????????¶????
  ??????????????????????????????MaxHeapify????????¨??????
  1??????Insert????????????????°????????????????????°???????????????????????????£??????????????????????????°??????
  (??????????????¨????????¢????????????????????????????????????????????§?????????)
  */
  vector<int> Heap(1, DUMMYVALUE);
  while (true) {
    string Command;
    cin >> Command;
    if (Command == "insert") {
      int k;
      cin >> k;
      Insert(Heap, k);
    } else if (Command == "extract") {
      ExtractMax(Heap);
    } else {
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cassert>
#include <iostream>
#include <string>
using namespace std;

class TPriorityQueue {
private:
  struct TNode {
  public:
    int Id;
    TNode *Parent;
    TNode *LChild;
    TNode *RChild;
    TNode(int Value) {
      Id = Value;
      Parent = NULL;
      LChild = NULL;
      RChild = NULL;
    }
  };
  TNode *Root;
  void Delete(const TNode *Node) {
    if (Node == NULL)
      return;
    Delete(Node->LChild);
    Delete(Node->RChild);
    delete Node;
  }
  void Insert(TNode *z) {
    assert(z != NULL);
    TNode *y = NULL;
    TNode *x = Root;
    while (x != NULL) {
      y = x;
      if (z->Id < x->Id) {
        x = x->LChild;
      } else {
        x = x->RChild;
      }
    }
    z->Parent = y;
    if (y == NULL) {
      Root = z;
    } else if (z->Id < y->Id) {
      y->LChild = z;
    } else {
      y->RChild = z;
    }
  }
  void EraseOnlyRoot() {
    if (Root->LChild != NULL) {
      TNode *NextRoot = Root->LChild;
      delete Root;
      Root = NextRoot;
    } else {
      Delete(Root);
      Root = NULL;
    }
  }
  void Erase(TNode *Node) {
    if (Node->LChild == NULL) {
      Node->Parent->RChild = NULL;
      delete Node;
    } else {
      TNode *Child = NULL;
      Child = Node->LChild;
      Node->Parent->RChild = Child;
      Child->Parent = Node->Parent;
      delete Node;
    }
  }

public:
  TPriorityQueue() { Root = NULL; }
  ~TPriorityQueue() { Delete(Root); }
  void Insert(int Value) { Insert(new TNode(Value)); }
  void ExtractMax() {
    TNode *MaxValueNode = Root;
    while (MaxValueNode->RChild != NULL) {
      MaxValueNode = MaxValueNode->RChild;
    }
    cout << MaxValueNode->Id << endl;
    if (MaxValueNode == Root) {
      EraseOnlyRoot();
    } else {
      Erase(MaxValueNode);
    }
  }
};

int main() {
  TPriorityQueue PriorityQueue;
  while (true) {
    string Command;
    cin >> Command;
    if (Command == "insert") {
      int k;
      cin >> k;
      PriorityQueue.Insert(k);
    } else if (Command == "extract") {
      PriorityQueue.ExtractMax();
    } else {
      break;
    }
  }
  return 0;
}