alexjercan/bugnet huggingface dataset:
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class PriorityQueue {
public:
  vector<int> q;
  int solve();
  int insert(int x);
  int extractMax();
  int maxHeapify(int i);
};

int PriorityQueue::solve() {
  string com;
  int a;

  q.push_back(0);

  while ((cin >> com) && com != "end") {
    if (com == "insert") {
      cin >> a;
      insert(a);
    } else {
      cout << extractMax() << endl;
    }
  }

  return 0;
}

int PriorityQueue::insert(int x) {
  q.push_back(x);
  maxHeapify(q.size() - 1);
  return 0;
}

int PriorityQueue::extractMax() {
  int ret = q[1];
  q[1] = -1;

  int p = 1;
  int l = p * 2;
  int r = l + 1;

  while (l < (int)q.size()) {
    if (r < (int)q.size()) {
      if (q[l] > q[r]) {
        swap(q[p], q[l]);
        p = l;
      } else {
        swap(q[p], q[r]);
        p = r;
      }
      l = p * 2;
      r = l + 1;
    } else {
      swap(q[p], q[l]);
      p = l;
      l = p * 2;
      r = l + 1;
    }
  }
  return ret;
}

int PriorityQueue::maxHeapify(int i) {
  int p = i / 2;

  while (p > 0 && q[p] < q[i]) {
    swap(q[p], q[i]);
    i = p;
    p = i / 2;
  }

  return 0;
}

int main() {
  PriorityQueue pq;

  pq.solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class PriorityQueue {
public:
  vector<int> q;
  int solve();
  int insert(int x);
  int extractMax();
  int maxHeapify(int x);
  int buildMaxHeap();
};

int PriorityQueue::solve() {
  string com;
  int a;

  q.push_back(0);

  while ((cin >> com) && com != "end") {
    if (com == "insert") {
      cin >> a;
      insert(a);
    } else {
      cout << extractMax() << endl;
    }
  }

  return 0;
}

int PriorityQueue::insert(int x) {
  q.push_back(x);
  buildMaxHeap();
  return 0;
}

int PriorityQueue::extractMax() {
  int ret = q[1];
  q[1] = -1;
  buildMaxHeap();
  return ret;
}

int PriorityQueue::maxHeapify(int x) {
  int l = x * 2;
  int r = l + 1;
  int s = q.size();
  int largest;

  if (l < s && q[l] > q[x])
    largest = l;
  else
    largest = x;

  if (r < s && q[r] > q[largest])
    largest = r;

  if (largest != x) {
    swap(q[x], q[largest]);
    maxHeapify(largest);
  }

  return 0;
}

int PriorityQueue::buildMaxHeap() {
  int s = q.size() - 1;

  for (auto i = s / 2; i >= 1; --i)
    maxHeapify(i);

  return 0;
}

int main() {
  PriorityQueue pq;

  pq.solve();

  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  priority_queue<int> PQ;

  char com[10];
  int key;
  while (1) {
    cin >> com;
    if (com[0] == 'i') {
      cin >> key;
      PQ.push(key);
    } else if (com[1] == 'x') {
      cout << PQ.top() << endl;
      PQ.pop();
    } else if (com[1] == 'n') {
      break;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  priority_queue<int> PQ;

  char com[10];
  int key;
  while (1) {
    cin >> com >> key;
    if (com[0] == 'i') {
      PQ.push(key);
    } else if (com[1] == 'x') {
      cout << PQ.top() << endl;
      PQ.pop();
    } else if (com[1] == 'n') {
      break;
    }
  }

  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

#define NUM 4000000
#define NIL -1
int A[NUM];
int heap_n = 0;

void init() {
  for (int i = 0; i < NUM; i++) {
    A[i] = NIL;
  }
}

void upHeapify(int num) {
  if (num == 1) {
    return;
  }

  if (A[num] > A[num / 2]) {
    int tmp = A[num];
    A[num] = A[num / 2];
    A[num / 2] = tmp;
    upHeapify(num / 2);
  }
}

void downHeapify(int num) {
  int m = max(A[num], max(A[num * 2], A[num * 2 + 1]));

  if (m == A[num * 2]) {
    swap(A[num * 2], A[num]);
    downHeapify(num * 2);
    return;
  }
  if (m == A[num * 2 + 1]) {
    swap(A[num * 2 + 1], A[num]);
    downHeapify(num * 2 + 1);
    return;
  }
}

void insert_(int num) {
  A[heap_n + 1] = num;
  heap_n++;
  upHeapify(heap_n);
}

void pop_() {
  A[1] = A[heap_n];
  A[heap_n] = NIL;
  heap_n--;
  downHeapify(1);
}

int main(void) {
  init();
  char buf[100];
  int num;

  while (1) {
    scanf("%s", buf);
    if (!strcmp(buf, "end")) {
      break;
    }

    if (!strcmp(buf, "insert")) {
      scanf("%d", &num);
      insert_(num);
    }

    if (!strcmp(buf, "extract")) {
      printf("%d\\n", A[1]);
      pop_();
    }

    if (!strcmp(buf, "print")) {
      for (int i = 1; i <= heap_n; i++) {
        printf("%d ", A[i]);
      }
    }
  }
}


Error: -11

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

#define NUM 100000
#define NIL -1
int A[NUM];
int heap_n = 0;

void init() {
  for (int i = 0; i < NUM; i++) {
    A[i] = NIL;
  }
}

void upHeapify(int num) {
  if (num == 1) {
    return;
  }

  if (A[num] > A[num / 2]) {
    int tmp = A[num];
    A[num] = A[num / 2];
    A[num / 2] = tmp;
    upHeapify(num / 2);
  }
}

void downHeapify(int num) {
  int m = max(A[num], max(A[num * 2], A[num * 2 + 1]));

  if (m == A[num * 2]) {
    swap(A[num * 2], A[num]);
    downHeapify(num * 2);
    return;
  }
  if (m == A[num * 2 + 1]) {
    swap(A[num * 2 + 1], A[num]);
    downHeapify(num * 2 + 1);
    return;
  }
}

void insert_(int num) {
  A[heap_n + 1] = num;
  heap_n++;
  upHeapify(heap_n);
}

void pop_() {
  A[1] = A[heap_n];
  A[heap_n] = NIL;
  heap_n--;
  downHeapify(1);
}

int main(void) {
  init();
  char buf[100];
  int num;

  while (1) {
    scanf("%s", buf);
    if (!strcmp(buf, "end")) {
      break;
    }

    if (!strcmp(buf, "insert")) {
      scanf("%d", &num);
      insert_(num);
    }

    if (!strcmp(buf, "extract")) {
      printf("%d\\n", A[1]);
      pop_();
    }

    if (!strcmp(buf, "print")) {
      for (int i = 1; i <= heap_n; i++) {
        printf("%d ", A[i]);
      }
    }
  }
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define MAX 2000000
using namespace std;

int n;

int parent(int i);
int left(int i);
int right(int i);
void maxHeap(int node[], int i);
void buildMaxHeap(int node[]);
void insert(int node[], int k);
int extractMax(int node[]);

int main() {
  int k, node[MAX];
  char com[10];

  n = 0;

  while (1) {
    cin >> com;
    if (com[2] == 'd')
      break;
    else if (com[0] == 'e')
      cout << extractMax(node) << endl;
    else {
      cin >> k;
      insert(node, k);
    }
  }
  return 0;
}

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

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

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

void maxHeap(int node[], int i) {
  int r, l, largest;

  if (left(i) <= n)
    l = left(i);
  else
    l = i;
  if (right(i) <= n)
    r = right(i);
  else
    r = i;

  if (node[l] < node[r])
    largest = r;
  else
    largest = l;

  if (node[largest] < node[i])
    largest = i;

  if (largest != i) {
    swap(node[i], node[largest]);
    maxHeap(node, largest);
  }
}

void buildMaxHeap(int node[]) {
  int i;
  for (i = n / 2; i > 0; i--)
    maxHeap(node, i);
}

void insert(int node[], int k) {
  int i;
  node[++n] = k;
  i = n;
  while (i > 1 && node[i] > node[i / 2]) {
    swap(node[i], node[i / 2]);
    i /= 2;
  }
}

int extractMax(int node[]) {
  int max;
  max = node[1];
  node[1] = node[n--];
  maxHeap(node, 1);
  return max;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define MAX 1000000
using namespace std;

int n;

int parent(int i);
int left(int i);
int right(int i);
void maxHeap(int node[], int i);
void buildMaxHeap(int node[]);
void insert(int node[], int k);
int extractMax(int node[]);

int main() {
  int k, node[MAX];
  char com[10];

  n = 0;

  while (1) {
    cin >> com;
    if (com[2] == 'd')
      break;
    else if (com[0] == 'e')
      cout << extractMax(node) << endl;
    else {
      cin >> k;
      insert(node, k);
    }
  }
  return 0;
}

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

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

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

void maxHeap(int node[], int i) {
  int r, l, largest;

  if (left(i) <= n)
    l = left(i);
  else
    l = i;
  if (right(i) <= n)
    r = right(i);
  else
    r = i;

  if (node[l] < node[r])
    largest = r;
  else
    largest = l;

  if (node[largest] < node[i])
    largest = i;

  if (largest != i) {
    swap(node[i], node[largest]);
    maxHeap(node, largest);
  }
}

void buildMaxHeap(int node[]) {
  int i;
  for (i = n / 2; i > 0; i--)
    maxHeap(node, i);
}

void insert(int node[], int k) {
  int i;
  node[++n] = k;
  i = n;
  while (i > 1 && node[i] > node[i / 2]) {
    swap(node[i], node[i / 2]);
    i /= 2;
  }
}

int extractMax(int node[]) {
  int max;
  max = node[1];
  node[1] = node[n--];
  maxHeap(node, 1);
  return max;
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <limits.h>
#include <string>
using namespace std;
long int A[2000000];

int n;

int parent(int i) { return i / 2; }
int left(int i) { return 2 * i; }
int right(int i) { return 2 * i + 1; }

void heapIncreaseKey(int i, int key) {
  if (key < A[i])
    return;
  A[i] = key;
  while (i > 1 && A[parent(i)] < A[i]) {
    swap(A[i], A[parent(i)]);
    i = parent(i);
  }
}

void maxHeapInsert(int key) {
  n++;
  A[n] = INT_MIN;
  heapIncreaseKey(n, key);
}

void maxHeapify(int);
int heapExtractMax() {
  int maxi;
  if (n < 1)
    return -1;
  maxi = A[1];
  A[1] = A[n];
  n--;
  maxHeapify(1);
  return maxi;
}
void maxHeapify(int i) {
  int l = left(i);
  int r = right(i);
  int large;
  if (l <= n && A[l] > A[i]) {
    large = l;
  } else {
    large = i;
  }
  if (r <= n && A[r] > A[large]) {
    large = r;
  }
  if (large != i) {
    swap(A[i], A[large]);
    maxHeapify(large);
  }
}

void buildMaxHeap() {
  for (int i = n / 2; i > 0; i--)
    maxHeapify(i);
}

int main() {
  int id;
  string cmd;
  A[0] = 0;
  do {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> id;
      maxHeapInsert(id);
    }
    if (cmd == "extract") {
      cout << heapExtractMax() << endl;
      ;
    }
  } while (cmd != "end");

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <limits.h>
#include <string>
using namespace std;
long int A[500000];

int n;

int parent(int i) { return i / 2; }
int left(int i) { return 2 * i; }
int right(int i) { return 2 * i + 1; }

void heapIncreaseKey(int i, int key) {
  if (key < A[i])
    return;
  A[i] = key;
  while (i > 1 && A[parent(i)] < A[i]) {
    swap(A[i], A[parent(i)]);
    i = parent(i);
  }
}

void maxHeapInsert(int key) {
  n++;
  A[n] = INT_MIN;
  heapIncreaseKey(n, key);
}

void maxHeapify(int);
int heapExtractMax() {
  int maxi;
  if (n < 1)
    return -1;
  maxi = A[1];
  A[1] = A[n];
  n--;
  maxHeapify(1);
  return maxi;
}
void maxHeapify(int i) {
  int l = left(i);
  int r = right(i);
  int large;
  if (l <= n && A[l] > A[i]) {
    large = l;
  } else {
    large = i;
  }
  if (r <= n && A[r] > A[large]) {
    large = r;
  }
  if (large != i) {
    swap(A[i], A[large]);
    maxHeapify(large);
  }
}

void buildMaxHeap() {
  for (int i = n / 2; i > 0; i--)
    maxHeapify(i);
}

int main() {
  int id;
  string cmd;
  A[0] = 0;
  do {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> id;
      maxHeapInsert(id);
    }
    if (cmd == "extract") {
      cout << heapExtractMax() << endl;
      ;
    }
  } while (cmd != "end");

  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int parent(int i) { return (i + 1) / 2 - 1; }
int left(int i) { return 2 * (i + 1) - 1; }
int right(int i) { return 2 * (i + 1) + 1 - 1; }
void downHeap(vector<int> &A, int i) {
  int n = A.size();
  int l = left(i);
  int r = right(i);
  int largest = i;
  // A[largest] = max(A[i],A[l],A[r])
  if (l < n) {
    if (A[l] > A[i])
      largest = l;
  }
  if (r < n) {
    if (A[r] > A[largest])
      largest = r;
  }
  if (largest != i) {
    swap(A[i], A[largest]);
    downHeap(A, largest);
  }
}
/*
void buildMaxHeap(vector<int> &A) {
  for(int i=(A.size()-1)/2;i>=0;i--) {
    downHeap(A,i);
  }
}
*/
void upHeap(vector<int> &A, int i) {
  int p = parent(i);
  if (p >= 0) {
    if (A[i] > A[p]) {
      swap(A[i], A[p]);
      upHeap(A, p);
    }
  }
}
void insert(vector<int> &A, int key) {
  A.push_back(key);
  upHeap(A, A.size() - 1);
}
int extractMax(vector<int> &A) {
  int max = A[0];
  A[0] = A.back();
  A.pop_back();
  downHeap(A, 0);
  return max;
}
int main() {
  vector<int> A;
  char buf[10];
  while (scanf("%s", buf)) {
    if (buf[0] == 'i') {
      int key;
      scanf("%d", &key);
      insert(A, key);
    } else if (buf[1] == 'x') {
      int max = extractMax(A);
      printf("%d\\n", max);
    } else if (buf[2] == 'd') {
      break;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
using namespace std;
int parent(int i) { return (i + 1) / 2 - 1; }
int left(int i) { return 2 * (i + 1) - 1; }
int right(int i) { return 2 * (i + 1) + 1 - 1; }
void downHeap(deque<int> &A, int i) {
  int n = A.size();
  int l = left(i);
  int r = right(i);
  int largest = i;
  // A[largest] = max(A[i],A[l],A[r])
  if (l < n) {
    if (A[l] > A[i])
      largest = l;
  }
  if (r < n) {
    if (A[r] > A[largest])
      largest = r;
  }
  if (largest != i) {
    swap(A[i], A[largest]);
    // for leaf...
    downHeap(A, largest);
  }
}
/*
void buildMaxHeap(deque<int> &A) {
  for(int i=(A.size()-1)/2;i>=0;i--) {
    downHeap(A,i);
  }
}
*/
void upHeap(deque<int> &A, int i) {
  int p = parent(i);
  if (p >= 0) {
    if (A[i] > A[p]) {
      swap(A[i], A[p]);
      upHeap(A, p);
    }
  }
}
void insert(deque<int> &A, int key) {
  A.push_back(key);
  upHeap(A, A.size() - 1);
}
int extract(deque<int> &A) {
  int max = A[0];
  A[0] = A.back();
  A.pop_back();
  downHeap(A, 0);
  return max;
}
int main() {
  deque<int> A;
  while (true) {
    string str;
    cin >> str;
    if (str == "insert") {
      int key;
      cin >> key;
      insert(A, key);
    } else if (str == "extract") {
      cout << extract(A) << endl;
    } else if (str == "end") {
      break;
    }
  }
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;

int main() {
  priority_queue<int> pq;
  string order;
  while (true) {
    cin >> order;
    if (order == "insert") {
      int key;
      cin >> key;
      pq.push(key);
    } else if (order == "extract") {
      cout << pq.top() << endl;
      pq.pop();
    } else if (order == "end") {
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<int> PQ;
  string order;
  while (true) {
    cin >> order;
    if (order == "insert") {
      int key;
      cin >> key;
      PQ.push_back(key);
    } else if (order == "extract") {
      sort(PQ.begin(), PQ.end(), greater<int>());
      cout << *(PQ.begin()) << endl;
      PQ.erase(PQ.begin());
    } else if (order == "end") {
      break;
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#define Lens 2000005

using namespace std;
int N, h = 0;
;
int A[Lens];
char com[20];

int parent(int i) { return i / 2; }
int lc(int i) { return 2 * i; }
int rc(int i) { return 2 * i + 1; }
void changeheapify(int i) {
  int p = parent(i);
  if (p > 0) {
    if (A[i] > A[p]) {
      swap(A[i], A[p]);
      changeheapify(p);
    }
  }
}
void Insert(int k) {
  h++;
  A[h] = k;
  changeheapify(h);
}

void maxheapify(int i) {
  int l = lc(i);
  int r = rc(i);
  int largest = i;
  if (A[l] > A[i] && l <= h)
    largest = l;
  if (A[r] > A[largest] && r <= h)
    largest = r;
  if (largest != i) {
    swap(A[i], A[largest]);
    maxheapify(largest);
  }
}

int Delete() {
  int tmp = A[1];
  A[1] = A[h];
  A[h] = 0;
  h--;
  maxheapify(1);
  return tmp;
}

int main() {
  int k;
  while (cin >> com) {
    if (com[0] == 'i') {
      cin >> k;
      Insert(k);
    } else if (com[0] == 'e' && com[1] == 'x') {
      int tmp = Delete();
      cout << tmp << endl;
    } else if (com[0] == 'e' && com[1] == 'n')
      break;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define Lens 200005

using namespace std;
int N, h = 0;
;
int A[Lens];
char com[20];

int parent(int i) { return i / 2; }
int lc(int i) { return 2 * i; }
int rc(int i) { return 2 * i + 1; }

void changeheapify(int i) {
  int p = parent(i);
  if (p > 0) {
    if (A[i] > A[p]) {
      swap(A[i], A[p]);
      changeheapify(p);
    }
  }
}

void maxheapify(int i) {
  int l = lc(i);
  int r = rc(i);
  int largest = i;
  if (A[l] > A[i] && l <= h)
    largest = l;
  if (A[r] > A[largest] && r <= h)
    largest = r;
  if (largest != i) {
    swap(A[i], A[largest]);
    maxheapify(largest);
  }
}

void Insert(int k) {
  h++;
  A[h] = k;
  changeheapify(h);
}

int Delete() {
  int tmp = A[1];
  A[1] = A[h];
  A[h] = 0;
  h--;
  for (int i = h / 2; i > 0; i--) {
    maxheapify(i);
  }
  return tmp;
}

int main() {
  int k;
  while (cin >> com) {
    if (com[0] == 'i') {
      cin >> k;
      Insert(k);
    } else if (com[0] == 'e' && com[1] == 'x') {
      int tmp = Delete();
      cout << tmp << endl;
    } else if (com[0] == 'e' && com[1] == 'n')
      break;
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define infinity (1 << 30)
const int maxx = 2000100;
int A[maxx];
int n;

void max_heap(int i) {
  int left = 2 * i, right = 2 * i + 1, x;
  if (left <= n && A[left] > A[i])
    x = left;
  else
    x = i;
  if (right <= n && A[right] > A[x])
    x = right;
  if (x != i) {
    swap(A[x], A[i]);
    max_heap(x);
  }
  return;
}

int extract() {
  int x;
  x = A[1];
  A[1] = A[n];
  n--;
  max_heap(1);
  return x;
}

int main() {
  string s;
  int a;
  n = 0;
  while (cin >> s) {
    if (s[0] == 'i') {
      cin >> A[++n];
      int i = n;
      while (i > 1 && A[i] > A[i / 2]) {
        swap(A[i], A[i / 2]);
        i = i / 2;
      }
    } else if (s[1] == 'x') {
      cout << extract() << endl;
    } else
      return 0;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxx = 2000100;
int A[maxx];
int n;

void max_heap(int i) {
  int left = 2 * i, right = 2 * i + 1, x;
  if (left <= n && A[left] > A[i])
    x = left;
  else
    x = i;
  if (right <= n && A[right] > A[x])
    x = right;
  if (x != i) {
    swap(A[x], A[i]);
    max_heap(x);
  }
  return;
}

int main() {
  string s;
  int a;
  n = 1;
  while (cin >> s) {
    if (s[0] == 'i') {
      cin >> A[n++];
    } else if (s[1] == 'x') {
      for (int i = n / 2; i >= 1; i--)
        max_heap(i);
      cout << A[1] << endl;
      for (int i = 2; i <= n; i++)
        A[i - 1] = A[i];
      n--;
    } else
      return 0;
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <map>
#include <math.h>
#define INFTY (1 << 30)

using namespace std;

int h = 0, a[2000000 + 1];

void maxHeapify(int i) {
  int l = 2 * i, 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;

  if (largest != i) {
    swap(a[i], a[largest]);
    maxHeapify(largest);
  }
  return;
}

void increaseKey(int i, int k) {
  if (k < a[i])
    return;
  a[i] = k;
  while (i > 1 && a[i / 2] < a[i]) {
    swap(a[i], a[i / 2]);
    i = i / 2;
  }
}

int extract() {
  if (h < 1)
    return -INFTY;
  int max = a[1];
  a[1] = a[h--];
  maxHeapify(1);
  return max;
}

int main() {
  while (1) {
    char str[10];
    int k;
    scanf("%s", str);
    if (str[0] == 'e' && str[1] == 'n')
      break;
    else if (str[0] == 'i') {
      cin >> k;
      h++;
      a[h] = -INFTY;
      increaseKey(h, k);
    } else {
      cout << extract() << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <map>
#include <math.h>
#define INFTY (1 << 30)

using namespace std;

int h = 0, a[2000000 + 1];

void maxHeapify(int i) {
  int l = 2 * i, 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;

  if (largest != i) {
    swap(a[i], a[largest]);
    maxHeapify(largest);
  }
  return;
}

void increaseKey(int i, int k) {
  if (k < a[i])
    return;
  a[i] = k;
  while (i > 1 && a[i / 2] < a[i]) {
    swap(a[i], a[i / 2]);
    i = i / 2;
  }
}

int extract() {
  if (h < 1)
    return -INFTY;
  int max = a[1];
  a[1] = a[h--];
  maxHeapify(1);
  return max;
}

int main() {
  while (1) {
    string str;
    int k;
    cin >> str;
    if (str[0] == 'e' && str[1] == 'n')
      break;
    else if (str[0] == 'i') {
      cin >> k;
      h++;
      a[h] = -INFTY;
      increaseKey(h, k);
    } else {
      cout << extract() << endl;
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

void solve() {
  priority_queue<int> pque;
  char order[10] = "";
  while (true) {
    cin >> order;
    // scanf_s(" %s", order);
    if (order[2] == 'd') {
      break;
    } else if (order[0] == 'i') {
      int num;
      scanf("%d", &num);
      pque.push(num);
    } else {
      printf("%d\\n", pque.top());
      pque.pop();
    }
  }
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

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

using namespace std;

void solve() {
  priority_queue<int, vector<int>> pque;
  while (true) {
    string order;
    cin >> order;
    if (order == "end") {
      break;
    } else if (order == "insert") {
      int num;
      cin >> num;
      pque.push(num);
    } else {
      cout << pque.top() << endl;
      pque.pop();
    }
  }
}

int main() {
  solve();
  return (0);
}