alexjercan/bugnet huggingface dataset:
Problem p02287 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <sstream>
#include <string>
using namespace std;
struct dot {
  long long int k, p, l, r;
};
int main(int argc, char **argv) {
  int n;
  while (cin >> n) {
    dot p[n + 100];
    for (int i = 1; i <= n; i++) {
      cin >> p[i].k;
      if (i / 2)
        p[i].p = i / 2;
      else
        p[i].p = 0;
      if (i * 2 <= n)
        p[i].l = i * 2;
      else
        p[i].l = 0;
      if (i * 2 + 1 <= n)
        p[i].r = i * 2 + 1;
      else
        p[i].r = 0;
    }
    for (int i = 1; i <= n; i++) {
      cout << "node " << i << ": key = " << p[i].k << ", ";
      if (p[i].p > 0)
        cout << "parent key = " << p[p[i].p].k << ", ";
      if (p[i].l > 0)
        cout << "left key = " << p[p[i].l].k << ", ";
      if (p[i].r > 0)
        cout << "right key = " << p[p[i].r].k << ", ";
      cout << endl;
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <sstream>
#include <string>
using namespace std;
struct dot {
  int k, p, l, r;
};
int main(int argc, char **argv) {
  int n;
  while (cin >> n) {
    dot p[n + 1];
    for (int i = 1; i <= n; i++) {
      cin >> p[i].k;
      if (i / 2)
        p[i].p = i / 2;
      else
        p[i].p = 0;
      if (i * 2 <= n)
        p[i].l = i * 2;
      else
        p[i].l = 0;
      if (i * 2 + 1 <= n)
        p[i].l = i * 2 + 1;
      else
        p[i].l = 0;
    }
    for (int i = 1; i <= n; i++) {
      cout << "node " << i << ": key = " << p[i].k << ", ";
      if (p[i].p > 0)
        cout << "parent key = " << p[p[i].p].k << ", ";
      if (p[i].l > 0)
        cout << "left key = " << p[p[i].l].k << ", ";
      if (p[i].r > 0)
        cout << "right key = " << p[p[i].r].k << ", ";
      cout << endl;
    }
  }
  return 0;
}
Problem p02287 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#define N 5000000
void print(int);
int n, tree[N];

int main() {
  int i, j;

  scanf("%d", &n);

  for (i = 1; i <= n; i++)
    scanf("%d", &tree[i]);
  for (i = 1; i <= n; i++)
    print(i);
  return 0;
}
void print(int u) {
  int right = u * 2 + 1;
  int left = u * 2;
  int parent = u / 2;
  printf("node %d: key = %d, ", u, tree[u]);
  if (1 < u)
    printf("parent key = %d, ", tree[parent]);
  if (left <= n)
    printf("left key = %d, ", tree[left]);
  if (right <= n)
    printf("right key = %d, ", tree[right]);
  printf("\\n");
}

Error: WA

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
void print(int);
int n, *tree;

int main() {
  int i, j;

  scanf("%d", &n);
  tree = (int *)malloc(sizeof(int) * n);
  for (i = 1; i <= n; i++)
    scanf("%d", &tree[i]);
  for (i = 1; i <= n; i++)
    print(i);
  return 0;
}
void print(int u) {
  int right = u * 2 + 1;
  int left = u * 2;
  int parent = u / 2;
  printf("node %d: key = %d, ", u, tree[u]);
  if (1 < u)
    printf("parent key = %d, ", tree[parent]);
  if (left <= n)
    printf("left key = %d, ", tree[left]);
  if (right <= n)
    printf("right key = %d, ", tree[right]);
  printf("\\n");
}
Problem p02287 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAX 250

typedef struct Node {
  int key;
  Node *parent, *left, *right;
} Node;

Node *NIL, T[MAX];

void print(int n) {
  int i;
  Node *N;
  for (i = 1; i <= n; i++) {
    N = &T[i];
    printf("node %d: key = %d, ", i, N->key);
    if (N->parent != NIL)
      printf("parent key = %d, ", N->parent->key);
    if (N->left != NIL)
      printf("left key = %d, ", N->left->key);
    if (N->right != NIL)
      printf("right key = %d, ", N->right->key);
    printf("\\n");
  }
}

void fill(int i, int k) {
  if (i == 1) {
    T[i].key = k;
  } else {
    T[i].key = k;
    T[i].parent = &T[i / 2];
    if (i % 2 == 0)
      T[i / 2].left = &T[i];
    else
      T[i / 2].right = &T[i];
  }
}

int main() {
  int n, i, k;
  for (i = 0; i < MAX; i++) {
    T[i].parent = T[i].left = T[i].right = NIL;
    T[i].key = 0;
  }

  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &k);
    fill(i, k);
  }

  print(n);

  return 0;
}


Error: -11

Fixed submission:

#include <memory>
#include <stdio.h>
#define MAX 25
typedef struct Node {
  int key;
  Node *parent, *left, *right;
} Node;

Node *NIL, *T[MAX];

void print(int n) {
  int i;
  Node *N;
  for (i = 0; i < n; i++) {
    N = T[i];
    printf("node %d: key = %d, ", i + 1, N->key);
    if (N->parent != NIL)
      printf("parent key = %d, ", N->parent->key);
    if (N->left != NIL)
      printf("left key = %d, ", N->left->key);
    if (N->right != NIL)
      printf("right key = %d, ", N->right->key);
    printf("\\n");
  }
}

void fill(int i, int k) {
  int n1, n2, j;
  j = 0;
  if (i == 0) {
    T[i]->key = k;
  } else {
    T[i]->key = k;
    T[i]->parent = T[i / 2];
  }
}

int main() {
  int n, i, k;
  for (i = 0; i < MAX; i++) {
    T[i]->parent = NIL, T[i]->left = NIL, T[i]->right = NIL;
  }

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &k);
    fill(i, k);
  }

  print(n);

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

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

#define MAX 2000000

int H, A[MAX + 1];

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

void maxHeapify(int i) {
  int l, r, t, largest;
  l = left(i);
  r = right(i);
  if (l <= H && A[l] > A[i]) {
    largest = l;
  } else {
    largest = i;
  }

  if (r <= H && A[r] > A[largest])
    largest = r;

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

int main() {
  cin >> H;
  for (int i = 1; i <= H; i++)
    cin >> A[i];

  for (int i = H / 2; i >= 1; i--)
    maxHeapify(i);

  for (int i = 1; i <= H; i++)
    cout << " " << A[i];

  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define MAX 500000

int H, A[MAX + 1];

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

void maxHeapify(int i) {
  int l, r, t, largest;
  l = left(i);
  r = right(i);
  if (l <= H && A[l] > A[i]) {
    largest = l;
  } else {
    largest = i;
  }

  if (r <= H && A[r] > A[largest])
    largest = r;

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

int main() {
  cin >> H;
  for (int i = 1; i <= H; i++)
    cin >> A[i];

  for (int i = H / 2; i >= 1; i++)
    maxHeapify(i);

  for (int i = 1; i <= H; i++)
    printf("%d ", A[i]);

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

#include <stdio.h>
#define MAX 500001

typedef struct Node {
  int key;
  Node *parent, *left, *right;
} Node;

Node *NIL, T[MAX];

void print(int n) {
  int i;
  Node *N;
  for (i = 1; i <= n; i++) {
    N = &T[i];
    printf("node %d: key = %d, ", i, N->key);
    if (N->parent != NIL)
      printf("parent key = %d, ", N->parent->key);
    if (N->left != NIL)
      printf("left key = %d, ", N->left->key);
    if (N->right != NIL)
      printf("right key = %d, ", N->right->key);
    printf("\\n");
  }
}

void fill(int i, int k) {
  if (i == 1) {
    T[i].key = k;
  } else {
    T[i].key = k;
    T[i].parent = &T[i / 2];
    if (i % 2 == 0)
      T[i / 2].left = &T[i];
    else
      T[i / 2].right = &T[i];
  }
}

void maxHeapify(int i) {
  int j = 0, k;
  Node *largest = &T[i];
  Node *l = T[i].left;
  Node *r = T[i].right;
  if (l != NIL && l->key > T[i].key) {
    largest = l;
    j = 2 * i;
  }
  if (r != NIL && r->key > largest->key) {
    largest = r;
    j = 2 * i + 1;
  }

  if (largest != &T[i]) {
    k = largest->key;
    T[j].key = T[i].key;
    T[i].key = k;
    maxHeapify(j);
  }
}

int main() {
  int n, i, k;
  for (i = 0; i < MAX; i++) {
    T[i].parent = T[i].left = T[i].right = NIL;
    T[i].key = 0;
  }

  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &k);
    fill(i, k);
  }

  for (i = n; i >= 1; i--) {
    maxHeapify(i);
  }

  for (i = 1; i <= n; i++) {
    printf(" %d", T[i].key);
  }
  printf("\\n");

  return 0;
}


Error: -11

Fixed submission:

#include <stdio.h>
#define MAX 500001

typedef struct Node {
  int key;
  Node *parent, *left, *right;
} Node;

Node *NIL, T[MAX];

void print(int n) {
  int i;
  Node *N;
  for (i = 1; i <= n; i++) {
    N = &T[i];
    printf("node %d: key = %d, ", i, N->key);
    if (N->parent != NIL)
      printf("parent key = %d, ", N->parent->key);
    if (N->left != NIL)
      printf("left key = %d, ", N->left->key);
    if (N->right != NIL)
      printf("right key = %d, ", N->right->key);
    printf("\\n");
  }
}

void fill(int i, int k) {
  if (i == 1) {
    T[i].key = k;
  } else {
    T[i].key = k;
    T[i].parent = &T[i / 2];
    if (i % 2 == 0)
      T[i / 2].left = &T[i];
    else
      T[i / 2].right = &T[i];
  }
}

void maxHeapify(int i) {
  int j;
  Node *largest;
  Node *l = T[i].left;
  Node *r = T[i].right;
  if (l != NIL && l->key > T[i].key) {
    largest = l;
    j = 2 * i;
  } else
    largest = &T[i];
  if (r != NIL && r->key > largest->key) {
    largest = r;
    j = 2 * i + 1;
  }

  if (largest != &T[i]) {
    T[j] = T[i];
    T[i] = *largest;
    maxHeapify(j);
  }
}

int main() {
  int n, i, k;
  for (i = 0; i < MAX; i++) {
    T[i].parent = T[i].left = T[i].right = NIL;
    T[i].key = 0;
  }

  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &k);
    fill(i, k);
  }

  for (i = n; i >= 1; i--) {
    maxHeapify(i);
  }

  print(n);

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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int maxx = 2000000;

int A[maxx + 1];
int n;
void maxHeapify(int i) {

  int left = 2 * i, right = 2 * i + 1;
  int largest;

  // ?????????????????????????????????????????????????????§??????????????¶?????§???????????????
  if (left <= n && A[left] > A[i])
    largest = left;
  else
    largest = i;

  if (right <= n && A[right] > A[largest])
    largest = right;

  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(largest);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &A[i]);

  for (int i = n / 2; i >= 1; i--)
    maxHeapify(i);
  for (int i = 1; i <= n; i++)
    printf(" %d", A[i]);
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int maxx = 200000;

int A[maxx + 1];
int n;
void maxHeapify(int i) {

  int left = 2 * i, right = 2 * i + 1;
  int largest;

  // ?????????????????????????????????????????????????????§??????????????¶?????§???????????????
  if (left <= n && A[left] > A[i])
    largest = left;
  else
    largest = i;

  if (right <= n && A[right] > A[largest])
    largest = right;

  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(largest);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &A[i]);

  for (int i = n / 2; i >= 1; i--)
    maxHeapify(i);
  for (int i = 1; i <= n; i++)
    printf(" %d", A[i]);
  printf("\\n");
  return 0;
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>

#define abs(a) (a < 0 ? -(a) : a)
#define square(x) ((x) * (x))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define swap(a, b) ((a != b) ? (a += b, b = a - b, a -= b) : 0)
#define This
#define True true
#define False false
#define null Null

using namespace std;
int N;
void maxHeapify(int num[], int i) {
  int left = 2 * i;
  int right = 2 * i + 1;
  int largest;
  if (left <= N && num[left] > num[i]) {
    largest = left;
  } else {
    largest = i;
  }
  if (right <= N && num[right] > num[largest]) {
    largest = right;
  }

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

void buildMaxHeap(int num[]) {
  for (int i = N / 2; i >= 1; i--) {
    maxHeapify(num, i);
  }

  for (int i = 1; i <= N; i++) {
    cout << " " << num[i];
  }
  cout << endl;
}
int main() {
  int num[1000000];
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> num[i];
  }
  buildMaxHeap(num);
}


Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>

#define abs(a) (a < 0 ? -(a) : a)
#define square(x) ((x) * (x))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define swap(a, b) ((a != b) ? (a += b, b = a - b, a -= b) : 0)
#define This
#define True true
#define False false
#define null Null

using namespace std;
int N;
void maxHeapify(int num[], int i) {
  int left = 2 * i;
  int right = 2 * i + 1;
  int largest;
  if (left <= N && num[left] > num[i]) {
    largest = left;
  } else {
    largest = i;
  }
  if (right <= N && num[right] > num[largest]) {
    largest = right;
  }

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

void buildMaxHeap(int num[]) {
  for (int i = N / 2; i >= 1; i--) {
    maxHeapify(num, i);
  }

  for (int i = 1; i <= N; i++) {
    cout << " " << num[i];
  }
  cout << endl;
}
int main() {
  int num[1000];
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> num[i];
  }
  buildMaxHeap(num);
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

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

int gHeap[MAX + 1], gSize;

void maxHeapify(int i) {
  int left, right, largest;
  left = 2 * i;
  right = 2 * i + 1;

  // 左の子、自分、右の子で値が最大のノードを選択
  if (left <= gSize && gHeap[left] > gHeap[i]) {
    largest = left;
  } else {
    largest = i;
  }
  // 右の子と比較
  if (right <= gSize && gHeap[right] > gHeap[largest])
    largest = right;

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

int main() {
  cin >> gSize;

  for (int i = 1; i <= gSize; ++i)
    cin >> gHeap[i];
  // buildMaxHeap
  for (int i = gSize / 2; i >= 1; --i)
    maxHeapify(i);

  for (int i = 1; i <= gSize; ++i) {
    cout << " " << gHeap[i];
  }
  cout << endl;

  return 0;
}


Error: WA

Fixed submission:

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

int gHeap[MAX + 1], gSize;

void maxHeapify(int i) {
  int left, right, largest;
  left = 2 * i;
  right = 2 * i + 1;

  // 左の子、自分、右の子で値が最大のノードを選択
  if (left <= gSize && gHeap[left] > gHeap[i]) {
    largest = left;
  } else {
    largest = i;
  }
  // 右の子と比較
  if (right <= gSize && gHeap[right] > gHeap[largest])
    largest = right;

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

int main() {
  cin >> gSize;

  for (int i = 1; i <= gSize; ++i)
    cin >> gHeap[i];

  for (int i = gSize / 2; i <= 1; --i)
    maxHeapify(i);

  for (int i = 1; i <= gSize; ++i) {
    cout << " " << gHeap[i];
  }
  cout << endl;

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

#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
#define MAX 20000000

int H, A[MAX + 1];

void maxheapify(int i) {
  int l, r, largest;
  l = 2 * i;
  r = 2 * i + 1;

  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);
  }
}

int main() {
  cin >> H;
  for (int i = 1; i <= H; i++) {
    cin >> A[i];
  }
  for (int i = H / 2; i >= 1; i--)
    maxheapify(i);
  for (int i = 1; i <= H; i++) {
    cout << " " << A[i];
  }
  cout << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
#define MAX 100000

int H, A[MAX + 1];

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

void maxheapify(int i) {
  int l, r, largest;
  l = 2 * i;
  r = 2 * i + 1;

  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);
  }
}

int main() {
  cin >> H;
  for (int i = 1; i <= H; i++) {
    cin >> A[i];
  }
  for (int i = H / 2; i >= 1; i--)
    maxheapify(i);
  for (int i = 1; i <= H; i++) {
    cout << " " << A[i];
  }
  cout << endl;
  return 0;
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

void f(const int, const int, int *);

int main() {
  int N;
  int data[500001];
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> data[i];
  }
  for (int i = N / 2; i > 0; i--) {
    f(N, i, data);
  }
  for (int i = 1; i <= N; i++) {
    cout << " " << data[i];
  }
  cout << endl;
}
void f(const int N, const int d, int *data) {
  int l = d * 2;
  int r = d * 2 + 1;
  int largest = d;
  if (l <= N && data[d] < data[l]) {
    largest = l;
  }
  if (r <= N && data[largest] < data[r]) {
    largest = r;
  }
  if (largest != d) {
    swap(data[d], data[largest]);
    f(N, largest, data);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

void f(const int, const int, int *);

int main() {
  int N;
  int data[251];
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> data[i];
  }
  for (int i = N / 2; i > 0; i--) {
    f(N, i, data);
  }
  for (int i = 1; i <= N; i++) {
    cout << " " << data[i];
  }
  cout << endl;
}
void f(const int N, const int d, int *data) {
  int l = d * 2;
  int r = d * 2 + 1;
  int largest = d;
  if (l <= N && data[d] < data[l]) {
    largest = l;
  }
  if (r <= N && data[largest] < data[r]) {
    largest = r;
  }
  if (largest != d) {
    swap(data[d], data[largest]);
    f(N, largest, data);
  }
}
Problem p02288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define N 1000000
int left_key(int);
int right_key(int);
void maxHeapify(int);

int n, A[N + 1];
int main() {
  int i;
  scanf("%d", &n);

  for (i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
  }

  for (i = n / 2; i >= 1; i--) //???????????°?????????????????§???n/2
  {
    maxHeapify(i);
  }

  for (i = 1; i <= n; i++) {
    printf(" %d", A[i]);
  }

  printf("\\n");
  return 0;
}
void maxHeapify(int x) {
  int l = 0, r = 0, largest = 0;
  int es_largest = 0; //??\\???????????????????????¨???
  l = left_key(x);
  r = right_key(x);

  if (l <= n && A[l] > A[x])
    largest = l;
  else
    largest = x;

  if (r <= n && A[r] > A[largest])
    largest = r;

  if (largest != x) {
    es_largest = A[largest];
    A[largest] = A[x];
    A[x] = es_largest;
    maxHeapify(largest);
  }
}

int left_key(int x) { return 2 * x; }
int right_key(int x) { return 2 * x + 1; }

Error: TLE

Fixed submission:

#include <stdio.h>
#define N 1000000
int left_key(int);
int right_key(int);
void maxHeapify(int);

int n, A[N + 1];
int main() {
  int i;
  scanf("%d", &n);

  for (i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
  }

  for (i = n / 2; i >= 1; i--) //???????????°?????????????????§???n/2
  {
    printf("test");
    maxHeapify(i);
  }

  for (i = 1; i <= n; i++) {
    printf(" %d", A[i]);
  }

  printf("\\n");
  return 0;
}
void maxHeapify(int x) {
  int l = 0, r = 0, largest = 0;
  int es_largest = 0; //??\\???????????????????????¨???
  l = left_key(x);
  r = right_key(x);

  if (l <= n && A[l] > A[x])
    largest = l;
  else
    largest = x;

  if (r <= n && A[r] > A[largest])
    largest = r;

  if (largest != x) {
    es_largest = A[x];
    A[x] = A[largest];
    A[largest] = es_largest;
  }
  maxHeapify(largest);
}

int left_key(int x) { return 2 * x; }
int right_key(int x) { return 2 * x + 1; }
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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

int H, A[MAX + 1];

void maxHeapify(int i) {
  int largest;
  int l = left(i);
  int r = right(i);
  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 buuldMaxHeap(int A[]) {
  for (int i = H / 2; i >= 1; i--) {
    maxHeapify(i);
  }
}

int main() {
  int i;

  cin >> H;

  for (i = 1; i <= H; i++) {
    cin >> A[i];
  }

  buuldMaxHeap(A);
  for (int i = 1; i <= H; i++) {
    cout << " " << A[i];
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 100000

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

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

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

int H, A[MAX + 1];

void maxHeapify(int A[], int i) {
  int largest;
  int l = left(i);
  int r = right(i);
  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(A, largest);
  }
}

void buuldMaxHeap(int A[]) {
  for (int i = H / 2; i >= 1; i--) {
    maxHeapify(A, i);
  }
}

int main() {
  int i;

  cin >> H;

  for (i = 1; i <= H; i++) {
    cin >> A[i];
  }

  buuldMaxHeap(A);
  for (int i = 1; i <= H; i++) {
    cout << " " << A[i];
  }
  cout << endl;
  return 0;
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>

const int MAX = 20000000;

int n, tree[MAX + 1];

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

  // Select the node which has the maximum key value
  int maximum_i = i;
  if (l <= n && tree[l] > tree[maximum_i])
    maximum_i = l;
  if (r <= n && tree[r] > tree[maximum_i])
    maximum_i = r;

  if (maximum_i != i) {
    // Call recursively
    std::swap(tree[i], tree[maximum_i]);
    maxHeapify(maximum_i);
  }
}

int main() {
  scanf("%d", &n);

  for (int i = 1; i <= n; ++i)
    scanf("%d", &tree[i]);

  for (int i = n / 2; i >= 1; --i)
    maxHeapify(i);

  for (int i = 1; i <= n; ++i)
    printf(" %d", tree[i]);
  printf("\\n");

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>

const int MAX = 20000000;

int tree[MAX + 1];

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

  // Select the node which has the maximum key value
  int maximum = std::max({tree[i], tree[l], tree[r]});
  int maximum_i = i;
  if (maximum == tree[l])
    maximum_i = l;
  if (maximum == tree[r])
    maximum_i = r;

  if (maximum_i != i) {
    // Call recursively
    std::swap(tree[i], tree[maximum_i]);
    maxHeapify(maximum_i);
  }
}

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 1; i <= n; ++i)
    scanf("%d", &tree[i]);

  for (int i = n / 2; i >= 1; --i)
    maxHeapify(i);

  for (int i = 1; i <= n; ++i)
    printf(" %d", tree[i]);
  printf("\\n");

  return 0;
}