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

#include <bits/stdc++.h>
#define INF ((2e9) + 1)
using namespace std;
int A[2000020], H;

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

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

void insert(int k) {
  H++;
  A[H] = -INF;
  heapIncreaseKey(H, k);
}

int extract() {
  int res = A[1];
  A[1] = A[H];
  H--;
  maxHeapify(1);
  return res;
}

int main() {
  while (1) {
    int a;
    char str[10];
    scanf("%s", str);
    if (str[0] == 'i')
      scanf("%d", &a), insert(a);
    else if (str[1] == 'x')
      cout << extract() << endl;
    else
      break;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF ((2e9) + 1)
using namespace std;
int A[2000020], H;

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

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

void insert(int k) {
  H++;
  A[H] = -INF;
  heapIncreaseKey(H, k);
}

int extract() {
  int res = A[1];
  A[1] = A[H];
  H--;
  maxHeapify(1);
  return res;
}

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

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

#define N 200000000
#define INF 200000001

int size;
int h[N];

void swap(int *a, int *b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

void maxHeapify(int i) {
  int l = 2 * i;
  int r = 2 * i + 1;
  int valM;
  if (l <= size && h[l] > h[i])
    valM = l;
  else
    valM = i;
  if (r <= size && h[r] > h[valM])
    valM = r;
  if (valM != i) {
    swap(&h[i], &h[valM]);
    maxHeapify(valM);
  }
}

void hIncreaseKey(int i, int key) {
  if (key < h[i])
    return;
  h[i] = key;
  while (i > 1 && h[i / 2] < h[i]) {
    swap(&h[i], &h[i / 2]);
    i = i / 2;
  }
}

void maxHeapInsert(int key) {
  size++;
  h[size] = -INF;
  hIncreaseKey(size, key);
}

int heapExtractMax() {
  int valM;
  if (size < 1)
    return 0;
  valM = h[1];
  h[1] = h[size--];
  maxHeapify(1);
  return valM;
}

int main() {
  string input;
  size = 0;
  while (cin >> input) {
    if (input == "end")
      break;
    if (input == "extract") {
      cout << heapExtractMax() << endl;
    } else {
      int val;
      cin >> val;
      maxHeapInsert(val);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define N 200000000
#define INF 200000001

int size;
int h[N];

void swap(int *a, int *b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

void maxHeapify(int i) {
  int l = 2 * i;
  int r = 2 * i + 1;
  int valM;
  if (l <= size && h[l] > h[i])
    valM = l;
  else
    valM = i;
  if (r <= size && h[r] > h[valM])
    valM = r;
  if (valM != i) {
    swap(&h[i], &h[valM]);
    maxHeapify(valM);
  }
}

void hIncreaseKey(int i, int key) {
  if (key < h[i])
    return;
  h[i] = key;
  while (i > 1 && h[i / 2] < h[i]) {
    swap(&h[i], &h[i / 2]);
    i = i / 2;
  }
}

void maxHeapInsert(int key) {
  size++;
  h[size] = -INF;
  hIncreaseKey(size, key);
}

int heapExtractMax() {
  int valM;
  if (size < 1)
    return 0;
  valM = h[1];
  h[1] = h[size--];
  maxHeapify(1);
  return valM;
}

int main() {
  string input;
  size = 0;
  while (cin >> input) {
    if (input == "end")
      break;
    if (input == "extract") {
      cout << heapExtractMax() << endl;
    } else {
      string val;
      cin >> val;
      maxHeapInsert(atoi(val.c_str()));
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using int64 = long long;

using namespace std;
using int_vec = vector<int64>;

void heapify(int_vec &v, int i) {
  int max;

  auto h = v.size();
  auto l = i * 2 + 1;
  auto r = i * 2 + 2;
  if (l < h && v[l] > v[i]) {
    max = l;
  } else {
    max = i;
  }
  if (r < h && v[r] > v[max]) {
    max = r;
  }

  if (max != i) {
    swap(v[i], v[max]);
    heapify(v, max);
  }
}

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

void insert(int_vec &v, int64 x) {
  v.push_back(x);

  int i = v.size() - 1;
  while (i > 0 && v[parent(i)] < v[i]) {
    swap(v[parent(i)], v[i]);
    i = parent(i);
  }
}

int main() {
  int_vec v;

  while (1) {
    string s;
    cin >> s;
    if (s == "end")
      break;

    if (s == "insert") {
      int x;
      cin >> x;
      insert(v, x);
    } else if (s == "extract") {
      cout << v[0] << endl;
      swap(v[0], v.back());
      v.pop_back();
      heapify(v, 0);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
using int_vec = vector<int>;

void heapify(int_vec &v, int i) {
  int max;

  auto h = v.size();
  auto l = i * 2 + 1;
  auto r = i * 2 + 2;
  if (l < h && v[l] > v[i]) {
    max = l;
  } else {
    max = i;
  }
  if (r < h && v[r] > v[max]) {
    max = r;
  }

  if (max != i) {
    swap(v[i], v[max]);
    heapify(v, max);
  }
}

void heapify(int_vec &v) {
  for (int i = v.size() / 2; i >= 0; --i) {
    heapify(v, i);
  }
}

int main() {
  int_vec v;

  while (1) {
    string s;
    cin >> s;
    if (s == "end")
      break;

    if (s == "insert") {
      int x;
      cin >> x;
      v.push_back(x);
    }
    if (s == "extract") {
      heapify(v);
      cout << v[0] << endl;

      v.erase(v.begin());
    }
  }

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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  priority_queue<int> qu;
  string p = "";
  int x;

  while (p != "end") {
    cin >> p;
    if (p == "insert") {
      cin >> x;
      qu.push(x);
    }
    if (p == "extract") {
      cout << qu.top() << endl;
      qu.pop();
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int N[2000000], i;

void ExtractMax() {
  sort(N, N + i);
  cout << N[i - 1] << endl;
  i--;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  i = 0;
  string p = "";
  int x;

  while (p != "end") {
    cin >> p;
    if (p == "insert") {
      cin >> x;
      N[i] = x;
      i++;
    }
    if (p == "extract")
      ExtractMax();
  }
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 2000000
#define INFTY (1 << 30)

int H, A[MAX + 1];

void maxHeap(int i) {
  int l, r, large;
  l = 2 * i;
  r = 2 * i + 1;

  if (l <= H && A[l] > A[i]) {
    large = l;
  } else {
    large = i;
  }
  if (r <= H && A[r] > A[large]) {
    large = r;
  }
  if (large != i) {
    swap(A[large], A[i]);
    maxHeap(large);
  }
}

int extract() {
  int maxv;
  if (H < 1)
    return -INFTY;
  maxv = A[1];
  A[1] = A[H--];
  maxHeap(1);
  return maxv;
}
void increasekey(int i, int key) {
  if (key < A[i])
    return;
  A[i] = key;
  while (i > 1 && A[i / 2] < A[i]) {
    swap(A[i], A[i / 2]);
    i = i / 2;
  }
}

int insert(int key) {
  H++;
  A[H] = -INFTY;
  increasekey(H, key);
}

int main() {
  int key;
  string com;
  while (1) {
    cin >> com;
    if (com == "end")
      break;
    if (com == "insert") {
      cin >> key;
      insert(key);
    } else {
      cout << extract() << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 2000000
#define INFTY (1 << 30)

int H, A[MAX + 1];

void maxHeap(int i) {
  int l, r, large;
  l = 2 * i;
  r = 2 * i + 1;

  if (l <= H && A[l] > A[i]) {
    large = l;
  } else {
    large = i;
  }
  if (r <= H && A[r] > A[large]) {
    large = r;
  }
  if (large != i) {
    swap(A[large], A[i]);
    maxHeap(large);
  }
}

int extract() {
  int maxv;
  if (H < 1)
    return -INFTY;
  maxv = A[1];
  A[1] = A[H--];
  maxHeap(1);
  return maxv;
}
void increasekey(int i, int key) {
  if (key < A[i])
    return;
  A[i] = key;
  while (i > 1 && A[i / 2] < A[i]) {
    swap(A[i], A[i / 2]);
    i = i / 2;
  }
}

int insert(int key) {
  H++;
  A[H] = -INFTY;
  increasekey(H, key);
}

int main() {
  int key;
  char com[10];
  while (1) {
    cin >> com;
    if (com == "end")
      break;
    if (com == "insert") {
      cin >> key;
      insert(key);
    } else {
      cout << extract() << endl;
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string>

void max_heapify(int *A, int i, int H) {
  int l = 2 * i, r = 2 * i + 1;
  if (l > H && r > H)
    return;
  int maxid = i, x;
  if (l <= H)
    maxid = (A[l] > A[maxid] ? l : maxid);
  if (r <= H)
    maxid = (A[r] > A[maxid] ? r : maxid);
  if (maxid == i)
    return;
  x = A[maxid];
  A[maxid] = A[i];
  A[i] = x;
  max_heapify(A, maxid, H);
}

int main() {
  int A[2000001];
  int i, H, key;
  char cmd[8];
  scanf("%d", &H);
  for (i = 1; i <= H; i++)
    scanf("%d", &A[i]);
  H = 0;

  while (1) {
    scanf("%s", cmd);
    if (cmd[2] == 'd')
      break;             // end.
    if (cmd[2] == 't') { // extract.
      if (H == 0)
        continue;
      printf("%d\\n", A[1]);
      A[1] = A[H];
      H--;
      if (H > 0)
        max_heapify(A, 1, H);
    } else {
      scanf("%d", &key); // H++;   // insert key.
      i = ++H;
      while (i > 1) {
        if (A[i / 2] > key)
          break;
        A[i] = A[i / 2];
        i /= 2;
      };
      A[i] = key;
    }
  };
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string>

void max_heapify(int *A, int i, int H) {
  int l = 2 * i, r = 2 * i + 1;
  if (l > H && r > H)
    return;
  int maxid = i, x;
  if (l <= H)
    maxid = (A[l] > A[maxid] ? l : maxid);
  if (r <= H)
    maxid = (A[r] > A[maxid] ? r : maxid);
  if (maxid == i)
    return;
  x = A[maxid];
  A[maxid] = A[i];
  A[i] = x;
  max_heapify(A, maxid, H);
}

int main() {
  int A[201];
  int i, H, key;
  char cmd[8];
  scanf("%d", &H);
  for (i = 1; i <= H; i++)
    scanf("%d", &A[i]);
  H = 0;

  while (1) {
    scanf("%s", cmd);
    if (cmd[2] == 'd')
      break;             // end.
    if (cmd[2] == 't') { // extract.
      if (H == 0)
        continue;
      printf("%d\\n", A[1]);
      A[1] = A[H];
      H--;
      if (H > 0)
        max_heapify(A, 1, H);
    } else {
      scanf("%d", &key); // H++;   // insert key.
      i = ++H;
      while (i > 1) {
        if (A[i / 2] > key)
          break;
        A[i] = A[i / 2];
        i /= 2;
      };
      A[i] = key;
    }
  };
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 2000000
#define INFTY (1 << 30)

int H, A[MAX + 1];

void maxHeap(int i) {
  int l, r, large;
  l = 2 * i;
  r = 2 * i + 1;

  if (l <= H && A[l] > A[i]) {
    large = l;
  } else {
    large = i;
  }
  if (r <= H && A[r] > A[large]) {
    large = r;
  }
  if (large != i) {
    swap(A[large], A[i]);
    maxHeap(large);
  }
}

int extract() {
  int maxv;
  if (H < 1)
    return -INFTY;
  maxv = A[1];
  A[1] = A[H--];
  maxHeap(1);
  return maxv;
}
void increasekey(int i, int key) {
  if (key < A[i])
    return;
  A[i] = key;
  while (i > 1 && A[i / 2] < A[i]) {
    swap(A[i], A[i / 2]);
    i = i / 2;
  }
}

int insert(int key) {
  H++;
  A[H] = -INFTY;
  increasekey(H, key);
}

int main() {
  int key;
  char com[10];
  while (1) {
    cin >> com;
    if (com[0] == 'e' && com[1] == 'n')
      break;
    if (com[0] == 'i') {
      cin >> key;
      insert(key);
    } else {
      cout << extract() << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 2000000
#define INFTY (1 << 30)

int H, A[MAX + 1];

void maxHeap(int i) {
  int l, r, large;
  l = 2 * i;
  r = 2 * i + 1;

  if (l <= H && A[l] > A[i]) {
    large = l;
  } else {
    large = i;
  }
  if (r <= H && A[r] > A[large]) {
    large = r;
  }
  if (large != i) {
    swap(A[large], A[i]);
    maxHeap(large);
  }
}

int extract() {
  int maxv;
  if (H < 1)
    return -INFTY;
  maxv = A[1];
  A[1] = A[H--];
  maxHeap(1);
  return maxv;
}
void increasekey(int i, int key) {
  if (key < A[i])
    return;
  A[i] = key;
  while (i > 1 && A[i / 2] < A[i]) {
    swap(A[i], A[i / 2]);
    i = i / 2;
  }
}

int insert(int key) {
  H++;
  A[H] = -INFTY;
  increasekey(H, key);
}

int main() {
  int key;
  char com[10];
  while (1) {
    cin >> com;
    if (com == "end")
      break;
    if (com == "insert") {
      cin >> key;
      insert(key);
    } else {
      cout << extract();
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>

int maxHeapify(int *a, int i, int h) {
  int l = i * 2;
  int r = i * 2 + 1;

  int largest, tmp;
  if (l <= h and a[l] > a[i])
    largest = l;
  else
    largest = i;
  if (r <= h and a[r] > a[largest])
    largest = r;

  if (largest != i) {
    tmp = a[i];
    a[i] = a[largest];
    a[largest] = tmp;
    return largest;
  } else
    return 0;
}

void heapUp(int *a, int h) {
  int tmp = h;
  int hoge = 1;
  while (tmp > 1 and hoge > 0) {
    tmp = tmp / 2;
    hoge = maxHeapify(a, tmp, h);
  }
}

void heapDown(int *a, int h) {
  int tmp = 1;
  while (tmp > 0) {
    tmp = maxHeapify(a, tmp, h);
  }
}

int main() {
  int x;
  char cmd[8];
  int array[2000001];
  int tail = 1;
  while (true) {
    scanf("%s", cmd);
    if (strcmp(cmd, "insert") == 0) {
      scanf("%d", &x);
      array[tail] = x;
      tail++;
      heapUp(array, tail - 1);
    } else if (strcmp(cmd, "extract") == 0) {
      printf("%d\\n", array[1]);
      array[1] = -1;
      heapDown(array, tail - 1);
    } else if (strcmp(cmd, "end") == 0) {
      break;
    } else
      return -1;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>

void maxHeapify(int *a, int i, int h) {
  int l = i * 2;
  int r = i * 2 + 1;

  int largest, tmp;
  if (l <= h and a[l] > a[i])
    largest = l;
  else
    largest = i;
  if (r <= h and a[r] > a[largest])
    largest = r;

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

void buildMaxHeap(int *a, int h) {
  int i;
  for (i = h / 2; i > 0; i--)
    maxHeapify(a, i, h);
}

int main() {
  int x;
  char cmd[8];
  int array[2000001];
  int head = 1;
  int tail = 1;
  while (true) {
    scanf("%s", cmd);
    if (strcmp(cmd, "insert") == 0) {
      scanf("%d", &x);
      array[tail] = x;
      tail++;
    } else if (strcmp(cmd, "extract") == 0) {
      if (tail == head)
        return -1;
      buildMaxHeap(array + head - 1, tail - head);
      printf("%d\\n", array[head]);
      head++;
    } else if (strcmp(cmd, "end") == 0) {
      break;
    } else
      return -1;
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int H[2000001];

void printResult(int i, int n) {
  printf("node %d: key = %d, ", i, H[i]);
  if (i != 1)
    printf("parent key = %d, ", H[i / 2]);
  if (2 * i <= n)
    printf("left key = %d, ", H[2 * i]);
  if (2 * i + 1 <= n)
    printf("right key = %d, ", H[2 * i + 1]);
  printf("\\n");
}

void maxHeapify(int i, int n) {
  int l = 2 * i;
  int r = 2 * i + 1;
  int largest = i;
  if (l <= n and H[l] >= H[i])
    largest = l;
  if (r <= n and H[r] >= H[largest])
    largest = r;

  if (largest != i) {
    swap(H[i], H[largest]);
    maxHeapify(largest, n);
  }
}

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

void heapincreasekey(int n) {
  while (n > 1 and H[n / 2] < H[n]) {
    swap(H[n], H[n / 2]);
    n = n / 2;
  }
}
int main() {
  int n, i, key;
  string com;
  n = 0;
  while (true) {
    cin >> com;
    if (com == "insert") {
      n++;
      cin >> H[n];
      heapincreasekey(n);
    } else if (com == "extract") {
      cout << H[1] << endl;
      H[1] = H[n];
      n--;
      maxHeapify(1, n);
    } else if (com == "end") {
      break;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int H[2000001];

void printResult(int i, int n) {
  printf("node %d: key = %d, ", i, H[i]);
  if (i != 1)
    printf("parent key = %d, ", H[i / 2]);
  if (2 * i <= n)
    printf("left key = %d, ", H[2 * i]);
  if (2 * i + 1 <= n)
    printf("right key = %d, ", H[2 * i + 1]);
  printf("\\n");
}

void maxHeapify(int i, int n) {
  int l = 2 * i;
  int r = 2 * i + 1;
  int largest = i;
  if (l <= n and H[l] >= H[i])
    largest = l;
  if (r <= n and H[r] >= H[largest])
    largest = r;

  if (largest != i) {
    swap(H[i], H[largest]);
    maxHeapify(largest, n);
  }
}

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

void heapincreasekey(int n) {
  while (n > 1 and H[n / 2] < H[n]) {
    swap(H[n], H[n / 2]);
    n = n / 2;
  }
}
int main() {
  int n, i, key;
  string com;
  n = 0;
  while (true) {
    cin >> com;
    if (com == "insert") {
      n++;
      cin >> H[n];
      heapincreasekey(n);
    } else if (com == "extract") {
      cout << H[1] << endl;
      H[1] = H[n];
      n--;
      buildMaxHeap(n);
    } else if (com == "end") {
      break;
    }
  }

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

#include <cmath>
#include <cstdint>
#include <cstdio>
#include <vector>

using namespace std;

template <typename T> class PriorityQueue {
private:
  vector<T> v;

public:
  PriorityQueue() {}

  void insert(T k) {
    v.push_back(k);

    int32_t i = v.size() - 1;
    int32_t p = (i - 1) / 2;
    while (p >= 0 && v[p] < v[i]) {
      swap(v[p], v[i]);
      i = p;
      p = (i - 1) / 2;
    }

    return;
  }

  T extractMax() {
    T maximum = v[0];
    swap(v[0], v[v.size() - 1]);
    v.pop_back();

    int32_t i = 0;
    while (true) {
      int32_t l = i * 2 + 1, r = i * 2 + 2, largest = i;
      if (l < v.size() && v[l] > v[largest])
        largest = l;
      if (r < v.size() && v[r] > v[largest])
        largest = r;

      if (largest != i) {
        swap(v[largest], v[i]);
        i = largest;
      } else {
        break;
      }
    }

    return maximum;
  }
};

int main() {
  PriorityQueue<int32_t> pq;

  while (true) {
    uint8_t command[8];
    scanf("%s", command);

    if (command[0] == 'i') {
      int32_t k;
      scanf("%d", &k);
      pq.insert(k);
    } else if (command[1] == 'x') {
      printf("%d\\n", pq.extractMax());
    } else if (command[1] == 'n') {
      break;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdint>
#include <cstdio>
#include <vector>

using namespace std;

template <typename T> void maxHeapify(vector<T> &A, int32_t i) {
  int32_t l = 2 * i + 1, r = 2 * i + 2, largest = i;
  if (l < A.size() && A[l] > A[largest])
    largest = l;
  if (r < A.size() && A[r] > A[largest])
    largest = r;

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

  return;
}

template <typename T> void buildMaxHeap(vector<T> &A) {
  for (int32_t i = A.size() / 2; i >= 0; i--) {
    maxHeapify(A, i);
  }

  return;
}

template <typename T> T extractMax(vector<T> &A) {
  buildMaxHeap(A);

  T maximum = A[0];

  swap(A[0], A[A.size() - 1]);
  A.pop_back();

  return maximum;
}

int main() {
  vector<int32_t> heap(2000000);

  while (true) {
    uint8_t command[8];
    scanf("%s", command);

    if (command[0] == 'i') {
      int32_t k;
      scanf("%d", &k);
      heap.push_back(k);
    } else if (command[1] == 'x') {
      printf("%d\\n", extractMax(heap));
    } else if (command[1] == 'n') {
      break;
    }
  }

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



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

int main() {
  priority_queue<int> Q;
  string str;
  int a;
  while (1) {
    cin >> str;
    if (str == "insert") {
      cin >> a;
      Q.push(a);
    } else if (str == "extract") {
      cout << Q.top() << endl;
      Q.pop();
    } else {
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:



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

int main() {
  priority_queue<int> Q;
  string str;
  int a;
  while (1) {
    cin >> str;
    if (str[0] == 'i') {
      cin >> a;
      Q.push(a);
    } else if (str[2] = 't') {
      cout << Q.top() << endl;
      Q.pop();
    } else {
      break;
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int A[2020200];
int H;
// 1-indexed
void maxHeapify(int i) {
  int l = i * 2;
  int r = i * 2 + 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);
  }
}
void insert(int x) {
  int i = ++H;
  A[i] = x;
  // 親より大きかったら入れ替える
  while (i > 1 && A[i] > A[i / 2]) {
    swap(A[i], A[i / 2]);
    i /= 2;
  }
}
int extract() {
  int ret = A[1];
  A[1] = A[H--];
  maxHeapify(1);
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  while (cin >> s && s != "end") {
    if (s == "insert") {
      int x;
      cin >> x;
      insert(x);
    } else {
      cout << extract() << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int A[202020];
int H;
// 1-indexed
void maxHeapify(int i) {
  int l = i * 2;
  int r = i * 2 + 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);
  }
}
void insert(int x) {
  int i = ++H;
  A[i] = x;
  // 親より大きかったら入れ替える
  while (i > 1 && A[i] > A[i / 2]) {
    swap(A[i], A[i / 2]);
    i /= 2;
  }
}
int extract() {
  int ret = A[1];
  A[1] = A[H--];
  maxHeapify(1);
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  while (cin >> s && s != "end") {
    if (s == "insert") {
      int x;
      cin >> x;
      insert(x);
    } else {
      cout << extract() << endl;
    }
  }
}