prithivMLmods/Coder-Stat huggingface dataset:
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int D[10000];
int H[10000];

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

int main() {
  int n, i, x;
  string com;

  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> H[i];
  }
  buildMaxHeap(n);
  for (i = 1; i <= n; i++) {
    printf(" %d", H[i]);
  }
  printf("\n");

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int H[500001];

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

int main() {
  int n, i, x;
  string com;

  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> H[i];
  }
  buildMaxHeap(n);
  for (i = 1; i <= n; i++) {
    printf(" %d", H[i]);
  }
  printf("\n");

  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#define MAX 2000000
using namespace std;
int H, a[MAX + 1];
void max(int i) {
  int l, r, large;
  l = 2 * i;
  r = 2 * i + 1;
  if (l <= H && a[l] > a[i])
    large = 1;
  else
    large = i;
  if (r <= H && a[r] > a[large])
    large = r;
  if (large != i) {
    swap(a[i], a[large]);
    max(large);
  }
}
int main() {
  cin >> H;
  for (int i = 1; i <= H; i++)
    cin >> a[i];
  for (int i = H / 2; i >= 1; i--)
    max(i);
  for (int i = 1; i <= H; i++)
    cout << " " << a[i];
  cout << endl;
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#define MAX 2000000
using namespace std;
int H, a[MAX + 1];
void max(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[i], a[large]);
    max(large);
  }
}
int main() {
  cin >> H;
  for (int i = 1; i <= H; i++)
    cin >> a[i];
  for (int i = H / 2; i >= 1; i--)
    max(i);
  for (int i = 1; i <= H; i++)
    cout << " " << a[i];
  cout << endl;
  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int A[260], 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 buildHeap() {
  for (int i = H / 2; i >= 1; i--)
    maxHeapify(i);
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int A[500010], 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 buildHeap() {
  for (int i = H / 2; i >= 1; i--)
    maxHeapify(i);
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  H = n;
  buildHeap();
  for (int i = 1; i <= n; i++)
    cout << " " << A[i];
  cout << endl;
  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;

int p[250];
int id, q;
int k;

int parent(int i) {
  if (i % 2 == 0) {
    return i / 2;
  } else {
    return (i - 1) / 2;
  }
}

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

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

int inside(int i) {
  if (i > id || i <= 0) {
    return 0;
  } else {
    return 1;
  }
}

void maxHeapify(int i) {
  if (inside(left(i)) == 0 || (p[right(i)] <= p[i] && p[left(i)] <= p[i])) {
  } else {
    if (p[left(i)] >= p[right(i)]) {
      q = p[left(i)];
      p[left(i)] = p[i];
      p[i] = q;
      maxHeapify(left(i));
    } else {
      q = p[right(i)];
      p[right(i)] = p[i];
      p[i] = q;
      maxHeapify(right(i));
    }
  }
}

int main() {
  cin >> id;
  for (int m = 0; m <= 8; m++) {
    if (id >= pow(2, m)) {
      k = (int)pow(2, m);
    }
  }
  for (int m = 1; m <= id; m++) {
    cin >> p[m];
  }
  for (int m = id; m >= 1; m--) {
    maxHeapify(m);
  }
  for (int m = 1; m <= id; m++) {
    printf(" %d", p[m]);
  }
  printf("\n");
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;

int p[500000];
int id, q;
int k;

int parent(int i) {
  if (i % 2 == 0) {
    return i / 2;
  } else {
    return (i - 1) / 2;
  }
}

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

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

int inside(int i) {
  if (i > id || i <= 0) {
    return 0;
  } else {
    return 1;
  }
}

void maxHeapify(int i) {
  if (inside(left(i)) == 0 || (p[right(i)] <= p[i] && p[left(i)] <= p[i])) {
  } else {
    if (p[left(i)] >= p[right(i)]) {
      q = p[left(i)];
      p[left(i)] = p[i];
      p[i] = q;
      maxHeapify(left(i));
    } else {
      q = p[right(i)];
      p[right(i)] = p[i];
      p[i] = q;
      maxHeapify(right(i));
    }
  }
}

int main() {
  cin >> id;
  for (int m = 0; m <= 8; m++) {
    if (id >= pow(2, m)) {
      k = (int)pow(2, m);
    }
  }
  for (int m = 1; m <= id; m++) {
    cin >> p[m];
  }
  for (int m = id; m >= 1; m--) {
    maxHeapify(m);
  }
  for (int m = 1; m <= id; m++) {
    printf(" %d", p[m]);
  }
  printf("\n");
  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct Node {
  int key;
  int id, parent, left, right;
};

Node A[100000];
int H;

void heap(int H) {
  for (int i = 1; i <= H; i++) {
    if (i == 1) {
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    } else {
      A[i].parent = A[i / 2].key;
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    }
  }
}
void maxheap(int i) {
  int l = 0, r = 0;
  int lag;
  int tmp;
  l = 2 * i;
  r = 2 * i + 1;
  if (l <= H && A[l].key > A[i].key) {
    lag = l;
  } else {
    lag = i;
  }
  if (r <= H && A[r].key > A[lag].key) {
    lag = r;
  }
  if (lag != i) {
    tmp = A[i].key;
    A[i].key = A[lag].key;
    A[lag].key = tmp;
    maxheap(lag);
  }
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct Node {
  int key;
  int id, parent, left, right;
};

Node A[20000000];
int H;

void heap(int H) {
  for (int i = 1; i <= H; i++) {
    if (i == 1) {
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    } else {
      A[i].parent = A[i / 2].key;
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    }
  }
}
void maxheap(int i) {
  int l = 0, r = 0;
  int lag;
  int tmp;
  l = 2 * i;
  r = 2 * i + 1;
  if (l <= H && A[l].key > A[i].key) {
    lag = l;
  } else {
    lag = i;
  }
  if (r <= H && A[r].key > A[lag].key) {
    lag = r;
  }
  if (lag != i) {
    tmp = A[i].key;
    A[i].key = A[lag].key;
    A[lag].key = tmp;
    maxheap(lag);
  }
}

int main() {
  cin >> H;
  for (int i = 1; i <= H; i++) {
    cin >> A[i].key;
    A[i].id = i;
  }
  heap(H);
  for (int i = H / 2; i >= 1; i--) {
    maxheap(i);
  }
  for (int i = 1; i <= H; i++) {
    cout << " " << A[i].key;
  }
  cout << endl;
  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define N 251
#define NIL 2000000001
using namespace std;

int n;

void swap(int *, int *);
void maxHeapify(int[], int);
void buildMaxHeap(int[]);

int main() {
  int heap[N];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> heap[i];
  }
  buildMaxHeap(heap);
  for (int i = 1; i <= n; i++) {
    cout << " " << heap[i];
  }
  cout << endl;
  return 0;
}

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

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

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

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

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

Fixed submission:
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define N 500000
#define NIL 2000000001
using namespace std;

int n;

void swap(int *, int *);
void maxHeapify(int[], int);
void buildMaxHeap(int[]);

int main() {
  int heap[N];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> heap[i];
  }
  buildMaxHeap(heap);
  for (int i = 1; i <= n; i++) {
    cout << " " << heap[i];
  }
  cout << endl;
  return 0;
}

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

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

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

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

void swap(int *a, int *b) {
  int w = *a;
  *a = *b;
  *b = w;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define reps(i, s, n) for (int i = (s); i <= (n); ++i)
using namespace std;

int H, heap[200001];

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

  // 左の子、自分、右の子の中で最大のノードを選ぶ
  if (l <= H && heap[l] > heap[i])
    largest = l;
  else
    largest = i;

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

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

int main() {
  scanf("%d", &H);
  reps(i, 1, H) { scanf("%d", &heap[i]); }

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

  reps(i, 1, H) printf(" %d", heap[i]);
  putchar_unlocked('\n');
}


Fixed submission:
#include <bits/stdc++.h>
#define reps(i, s, n) for (int i = (s); i <= (n); ++i)
using namespace std;

int H, heap[500010];

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

  // 左の子、自分、右の子の中で最大のノードを選ぶ
  if (l <= H && heap[l] > heap[i])
    largest = l;
  else
    largest = i;

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

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

int main() {
  scanf("%d", &H);
  reps(i, 1, H) { scanf("%d", &heap[i]); }

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

  reps(i, 1, H) printf(" %d", heap[i]);
  putchar_unlocked('\n');
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
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[]);

int main() {
  int i, node[252];
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> node[i];

  buildMaxHeap(node);

  for (i = 1; i <= n; i++)
    cout << " " << node[i];
  cout << endl;

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

Fixed submission:
#include <algorithm>
#include <iostream>
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[]);

int main() {
  int i, node[500000];
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> node[i];

  buildMaxHeap(node);

  for (i = 1; i <= n; i++)
    cout << " " << node[i];
  cout << endl;

  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);
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#define N 250
int it = 0, n, tree[N + 1];

void swapInt(int *x, int *y) {
  int tmp = *x;
  *x = *y;
  *y = tmp;
}
void maxHeap(int u) {
  int left, right, largest;
  left = u * 2;
  right = u * 2 + 1;

  if (left <= it && tree[u] < tree[left])
    largest = left;
  else
    largest = u;
  if (right <= it && tree[largest] < tree[right])
    largest = right;

  if (largest != u) {
    swapInt(&tree[u], &tree[largest]);
    maxHeap(largest);
  }
}
int main() {
  int i;

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

  for (i = it / 2; i >= 1; i--)
    maxHeap(i);

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

Fixed submission:
#include <stdio.h>
#define N 500000
int it = 0, n, tree[N + 1];

void swapInt(int *x, int *y) {
  int tmp = *x;
  *x = *y;
  *y = tmp;
}
void maxHeap(int u) {
  int left, right, largest;
  left = u * 2;
  right = u * 2 + 1;

  if (left <= it && tree[u] < tree[left])
    largest = left;
  else
    largest = u;
  if (right <= it && tree[largest] < tree[right])
    largest = right;

  if (largest != u) {
    swapInt(&tree[u], &tree[largest]);
    maxHeap(largest);
  }
}
int main() {
  int i;

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

  for (i = it / 2; i >= 1; i--)
    maxHeap(i);

  for (i = 1; i <= n; i++)
    printf(" %d", tree[i]);
  printf("\n");
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long ll;
#define INF 2139062143

void heap(int);

int arr[300];
int n;

int main(int argc, char *argv[]) {
  memset(arr, 127, sizeof(arr));

  // input
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  // calc
  for (int i = n / 2; i > 0; i--)
    heap(i);

  // output
  for (int i = 1; i <= n; i++)
    cout << " " << arr[i];
  cout << endl;

  return 0;
}

void heap(int nd) {
  int lft = nd * 2, rht = nd * 2 + 1, largest = nd;

  if (lft <= n && arr[largest] < arr[lft])
    largest = lft;
  if (rht <= n && arr[largest] < arr[rht])
    largest = rht;

  if (largest != nd) {
    swap(arr[nd], arr[largest]);
    heap(largest);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long ll;
#define INF 2139062143

void heap(int);

int arr[500001];
int n;

int main(int argc, char *argv[]) {
  memset(arr, 127, sizeof(arr));

  // input
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  // calc
  for (int i = n / 2; i > 0; i--)
    heap(i);

  // output
  for (int i = 1; i <= n; i++)
    cout << " " << arr[i];
  cout << endl;

  return 0;
}

void heap(int nd) {
  int lft = nd * 2, rht = nd * 2 + 1, largest = nd;

  if (lft <= n && arr[largest] < arr[lft])
    largest = lft;
  if (rht <= n && arr[largest] < arr[rht])
    largest = rht;

  if (largest != nd) {
    swap(arr[nd], arr[largest]);
    heap(largest);
  }
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int H, A[500001] = {};

void mh(int i) {
  int l = i * 2, r = i * 2 + 1, lar = i;
  if (A[l] > A[i] && l <= H)
    lar = l;
  if (A[r] > A[lar] && r <= H)
    lar = r;

  if (lar != i) {
    swap(A[lar], A[i]);
    mh(lar);
  }
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int H, A[5000001] = {};

void mh(int i) {
  int l = i * 2, r = i * 2 + 1, lar = i;
  if (A[l] > A[i] && l <= H)
    lar = l;
  if (A[r] > A[lar] && r <= H)
    lar = r;

  if (lar != i) {
    swap(A[lar], A[i]);
    mh(lar);
  }
}

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

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define MAX 200000

int H, A[MAX + 1];

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

int main() {
  int i;

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

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

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

  return 0;
}


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

int H, A[MAX + 1];

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

int main() {
  int i;

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

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

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

  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
long long int a[100];
int x;
void build(int n) {
  long long int temp;
  int top = n, l = n * 2, r = n * 2 + 1;
  if (a[r] > a[n] && r <= x)
    top = r;
  if (a[l] > a[top] && l <= x)
    top = l;
  if (top != n) {
    temp = a[top];
    a[top] = a[n];
    a[n] = temp;
    build(top);
  }
}
int main() {
  scanf("%d", &x);
  for (int i = 1; i <= x; i++) {
    scanf("%lld", &a[i]);
    a[i * 2] = a[i * 2 + 1] = 0;
  }
  for (int i = x / 2; i > 0; i--) {
    build(i);
  }
  for (int i = 1; i <= x; i++) {
    printf(" %lld", a[i]);
  }
  printf("\n");
}

Fixed submission:
#include <stdio.h>
long long int a[1000000];
int x;
void build(int n) {
  long long int temp;
  int top = n, l = n * 2, r = n * 2 + 1;
  if (a[r] > a[n] && r <= x)
    top = r;
  if (a[l] > a[top] && l <= x)
    top = l;
  if (top != n) {
    temp = a[top];
    a[top] = a[n];
    a[n] = temp;
    build(top);
  }
}
int main() {
  scanf("%d", &x);
  for (int i = 1; i <= x; i++) {
    scanf("%lld", &a[i]);
    a[i * 2] = a[i * 2 + 1] = 0;
  }
  for (int i = x / 2; i > 0; i--) {
    build(i);
  }
  for (int i = 1; i <= x; i++) {
    printf(" %lld", a[i]);
  }
  printf("\n");
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n;
int H[251];
void buildMaxHeap(void);
void maxHeapify(int);
int main() {

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

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

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

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

  if (l <= n && H[l] > H[i])
    largest = l;
  else
    largest = i;

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

  if (largest != i) {
    tmp = H[i];
    H[i] = H[largest];
    H[largest] = tmp;

    maxHeapify(largest);
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int n;
int H[5000000];
void buildMaxHeap(void);
void maxHeapify(int);
int main() {

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

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

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

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

  if (l <= n && H[l] > H[i])
    largest = l;
  else
    largest = i;

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

  if (largest != i) {
    tmp = H[i];
    H[i] = H[largest];
    H[largest] = tmp;

    maxHeapify(largest);
  }
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int A[250 + 1], h;

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

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

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

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

int main(void) {
  int i;

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

  for (i = h / 2; i >= 1; i--)
    maxHeap(i);

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int A[500000 + 1], h;

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

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

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

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

int main(void) {
  int i;

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

  for (i = h / 2; i >= 1; i--)
    maxHeap(i);

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

  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):


#include <stdio.h>

void max_heapify(int *a, int i, int n)

{

  int j, temp;

  temp = a[i];

  j = 2 * i;

  while (j <= n)

  {

    if (j < n && a[j + 1] > a[j])

      j = j + 1;

    if (temp > a[j])

      break;

    else if (temp <= a[j])

    {

      a[j / 2] = a[j];

      j = 2 * j;
    }
  }

  a[j / 2] = temp;

  return;
}

void build_maxheap(int *a, int n)

{

  int i;

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

  {

    max_heapify(a, i, n);
  }
}

int main()

{

  int n, i, x;

  scanf("%d", &n);
  int a[20];

  for (i = 1; i <= n; i++)

  {

    scanf("%d", &a[i]);
  }

  build_maxheap(a, n);

  for (i = 1; i <= n; i++)

  {

    printf(" %d", a[i]);
  }
  printf("\n");
  return 0;
}


Fixed submission:


#include <stdio.h>

void max_heapify(int *a, int i, int n)

{

  int j, temp;

  temp = a[i];

  j = 2 * i;

  while (j <= n)

  {

    if (j < n && a[j + 1] > a[j])

      j = j + 1;

    if (temp > a[j])

      break;

    else if (temp <= a[j])

    {

      a[j / 2] = a[j];

      j = 2 * j;
    }
  }

  a[j / 2] = temp;

  return;
}

void build_maxheap(int *a, int n)

{

  int i;

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

  {

    max_heapify(a, i, n);
  }
}

int main()

{

  int n, i, x;

  scanf("%d", &n);
  int a[500001];

  for (i = 1; i <= n; i++)

  {

    scanf("%d", &a[i]);
  }

  build_maxheap(a, n);

  for (i = 1; i <= n; i++)

  {

    printf(" %d", a[i]);
  }
  printf("\n");
  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;
typedef long long ll;

const int MAXH = 260;

void maxHeap(int i, int H, int *A) {
  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]);
    maxHeap(largest, H, A);
  }
}

int main() {
  int H, A[MAXH];

  cin >> H;
  for (int i = 1; i <= H; i++) {
    scanf("%d", &A[i]);
  }
  for (int i = H / 2; i >= 1; i--) {
    maxHeap(i, H, A);
  }
  for (int i = 1; i <= H; i++) {
    cout << " " << A[i];
  }
  cout << endl;

  return (0);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;
typedef long long ll;

const int MAXH = 500100;

void maxHeap(int i, int H, int *A) {
  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]);
    maxHeap(largest, H, A);
  }
}

int main() {
  int H, A[MAXH];

  cin >> H;
  for (int i = 1; i <= H; i++) {
    scanf("%d", &A[i]);
  }
  for (int i = H / 2; i >= 1; i--) {
    maxHeap(i, H, A);
  }
  for (int i = 1; i <= H; i++) {
    cout << " " << A[i];
  }
  cout << endl;

  return (0);
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli h;
vll a;

void max_heapify(lli i) {
  lli l = i * 2;
  lli r = i * 2 + 1;
  lli est = max({i, r, l}, [](lli s, lli t) { return a[s] < a[t]; });
  swap(a[i], a[est]);
  if (i != est)
    max_heapify(est);
}

int main() {
  cin >> h;
  a = vll(h + 1);
  for (lli i = 1; i <= h; i++)
    cin >> a[i];
  for (lli i = h; i > 0; i--)
    max_heapify(i);
  for (lli i = 1; i <= h; i++)
    cout << " " << a[i];
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<vector<bool>> matb;
typedef vector<string> vst;
typedef pair<lli, lli> pll;
typedef pair<double, double> pdd;

lli h;
vll a;

void max_heapify(lli i) {
  lli l = i * 2;
  lli r = i * 2 + 1;
  lli est = max({i, r, l}, [](lli s, lli t) {
    if (s > h)
      return true;
    if (t > h)
      return false;
    return a[s] < a[t];
  });
  swap(a[i], a[est]);
  if (i != est)
    max_heapify(est);
}

int main() {
  cin >> h;
  a = vll(h + 1);
  for (lli i = 1; i <= h; i++)
    cin >> a[i];
  for (lli i = h; i > 0; i--)
    max_heapify(i);
  for (lli i = 1; i <= h; i++)
    cout << " " << a[i];
  cout << endl;
  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#define NUM 100000000
#define NIL -2000000001
using namespace std;

void print(int *heap, int H) {
  for (int i = 0; i < H; i++) {
    printf(" %d", heap[i + 1]);
  }
  printf("\n");
}

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

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

int largest_(int *heap, int a, int b, int c) {
  if (heap[a] < heap[b]) {
    if (heap[b] < heap[c]) {
      return c;
    } else {
      return b;
    }
  } else {
    if (heap[a] < heap[c]) {
      return c;
    } else {
      return a;
    }
  }
}

void maxHeapify(int *heap, int i) {
  int l = left(i);
  int r = right(i);
  int largest = largest_(heap, l, r, i);

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

void buildMaxHeap(int *heap, int H) {
  for (int i = H / 2; i >= 1; i--) {
    maxHeapify(heap, i);
  }
}

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

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

  for (int i = 1; i <= H; i++) {
    scanf("%d", &heap[i]);
  }

  buildMaxHeap(heap, H);

  print(heap, H);

  return 0;
}


Fixed submission:
#include <iostream>
#include <stdio.h>
#define NUM 10000000
#define NIL -2000000001
using namespace std;

void print(int *heap, int H) {
  for (int i = 0; i < H; i++) {
    printf(" %d", heap[i + 1]);
  }
  printf("\n");
}

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

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

int largest_(int *heap, int a, int b, int c) {
  if (heap[a] < heap[b]) {
    if (heap[b] < heap[c]) {
      return c;
    } else {
      return b;
    }
  } else {
    if (heap[a] < heap[c]) {
      return c;
    } else {
      return a;
    }
  }
}

void maxHeapify(int *heap, int i) {
  int l = left(i);
  int r = right(i);
  int largest = largest_(heap, l, r, i);

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

void buildMaxHeap(int *heap, int H) {
  for (int i = H / 2; i >= 1; i--) {
    maxHeapify(heap, i);
  }
}

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

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

  for (int i = 1; i <= H; i++) {
    scanf("%d", &heap[i]);
  }

  buildMaxHeap(heap, H);

  print(heap, H);

  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int H;

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

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

void buildmaxHeap(int *A) {
  for (int i = H / 2; i > 0; i--) {
    maxHeapify(A, i);
  }
}

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

Fixed submission:
#include <iostream>
using namespace std;

int H;

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

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

void buildmaxHeap(int *A) {
  for (int i = H / 2; i > 0; i--) {
    maxHeapify(A, i);
  }
}

int main() {
  int A[1999999];
  cin >> H;
  for (int i = 1; i <= H; i++)
    cin >> A[i];
  buildmaxHeap(A);
  for (int i = 1; i <= H; i++)
    cout << " " << A[i];
  cout << endl;
  return 0;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>

constexpr size_t MAX_HEAP_SIZE = 250;
int H[MAX_HEAP_SIZE + 1];

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

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

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

int MaxHeapify(int *H, int i, int heap_size) {
  int l = left(i);
  int r = right(i);
  int largest;
  if ((l <= heap_size) && (H[l]) > H[i]) {
    largest = l;
  } else {
    largest = i;
  }
  if ((r <= heap_size) && (H[r] > H[largest])) {
    largest = r;
  }
  if (largest != i) {
    std::swap(H[i], H[largest]);
    MaxHeapify(H, largest, heap_size);
  }
  return 0;
}

int BuildMaxHeap(int *H, int heap_size) {
  for (int i = heap_size / 2; i > 0; i--) {
    MaxHeapify(H, i, heap_size);
  }
  return 0;
}

int main() {
  int n;
  std::cin >> n;
  for (int i = 1; i <= n; i++)
    std::cin >> H[i];
  BuildMaxHeap(H, n);
  for (int i = 1; i <= n; i++) {
    std::cout << " ";
    std::cout << H[i];
  }
  std::cout << std::endl;
}


Fixed submission:
#include <cmath>
#include <iostream>

constexpr size_t MAX_HEAP_SIZE = 500000;
;
int H[MAX_HEAP_SIZE + 1];

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

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

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

int MaxHeapify(int *H, int i, int heap_size) {
  int l = left(i);
  int r = right(i);
  int largest;
  if ((l <= heap_size) && (H[l]) > H[i]) {
    largest = l;
  } else {
    largest = i;
  }
  if ((r <= heap_size) && (H[r] > H[largest])) {
    largest = r;
  }
  if (largest != i) {
    std::swap(H[i], H[largest]);
    MaxHeapify(H, largest, heap_size);
  }
  return 0;
}

int BuildMaxHeap(int *H, int heap_size) {
  for (int i = heap_size / 2; i > 0; i--) {
    MaxHeapify(H, i, heap_size);
  }
  return 0;
}

int main() {
  int n;
  std::cin >> n;
  for (int i = 1; i <= n; i++)
    std::cin >> H[i];
  BuildMaxHeap(H, n);
  for (int i = 1; i <= n; i++) {
    std::cout << " ";
    std::cout << H[i];
  }
  std::cout << std::endl;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>

constexpr int MAXN = 250;
int Heap[MAXN + 1];

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
int n;

void ShowHeap(int *Heap) {
  FOR(i, 1, n + 1) {
    std::printf("node %d: key = %d, ", i, Heap[i]);
    if (parent(i))
      std::printf("parent key = %d, ", Heap[parent(i)]);
    if (left(i) <= n)
      std::printf("left key = %d, ", Heap[left(i)]);
    if (right(i) <= n)
      std::printf("right key = %d, ", Heap[right(i)]);
    std::cout << std::endl;
  }
}

void MaxHeapify(int *H, int i) {
  int l = left(i), r = right(i);
  int largest = i;
  if (l <= n && H[largest] < H[l]) {
    largest = l;
  }
  if (r <= n && H[largest] < H[r]) {
    largest = r;
  }
  if (largest != i) {
    std::swap(H[i], H[largest]);
    MaxHeapify(H, largest);
  }
}

void BuildHeap(int *H) {
  for (int i = n / 2; i > 0; --i) {
    MaxHeapify(H, i);
  }
}

int main() {
  std::cin >> n;
  FOR(i, 0, n) std::cin >> Heap[i + 1];
  BuildHeap(Heap);
  FOR(i, 0, n) std::cout << " " << Heap[i + 1];
  std::cout << std::endl;
}


Fixed submission:
#include <cstdio>
#include <iostream>

constexpr int MAXN = 500000;
int Heap[MAXN + 1];

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
int n;

void ShowHeap(int *Heap) {
  FOR(i, 1, n + 1) {
    std::printf("node %d: key = %d, ", i, Heap[i]);
    if (parent(i))
      std::printf("parent key = %d, ", Heap[parent(i)]);
    if (left(i) <= n)
      std::printf("left key = %d, ", Heap[left(i)]);
    if (right(i) <= n)
      std::printf("right key = %d, ", Heap[right(i)]);
    std::cout << std::endl;
  }
}

void MaxHeapify(int *H, int i) {
  int l = left(i), r = right(i);
  int largest = i;
  if (l <= n && H[largest] < H[l]) {
    largest = l;
  }
  if (r <= n && H[largest] < H[r]) {
    largest = r;
  }
  if (largest != i) {
    std::swap(H[i], H[largest]);
    MaxHeapify(H, largest);
  }
}

void BuildHeap(int *H) {
  for (int i = n / 2; i > 0; --i) {
    MaxHeapify(H, i);
  }
}

int main() {
  std::cin >> n;
  FOR(i, 0, n) std::cin >> Heap[i + 1];
  BuildHeap(Heap);
  FOR(i, 0, n) std::cout << " " << Heap[i + 1];
  std::cout << std::endl;
}
Problem p02288 (C++)

Buggy submission (status: 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];

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

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

  return 0;
}


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];
  // 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;
}
Problem p02288 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int main(void) {
  int n;
  int a[250];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  make_heap(a, a + n);
  for (int i = 0; i < n; i++) {
    cout << " " << a[i];
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main(void) {
  int n;
  int a[500000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  make_heap(a, a + n);
  for (int i = 0; i < n; i++) {
    cout << " " << a[i];
  }
  cout << endl;
  return 0;
}
Problem p02289 (C++)

Buggy submission (status: 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--;
      buildMaxHeap(n);
    } else if (com == "end") {
      break;
    }
  }

  return 0;
}


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--;
      maxHeapify(1, n);
    } else if (com == "end") {
      break;
    }
  }

  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

ll a[500010] = {NIL};
int h;

ll rk(int n) {
  if (n * 2 + 1 <= h)
    return a[n * 2 + 1];
  return NIL;
}
ll lk(int n) {
  if (n * 2 <= h)
    return a[n * 2];
  return NIL;
}
ll pk(int n) {
  if (n / 2 <= h)
    return a[n / 2];
  return NIL;
}

void print(int n) {
  cout << "node " << n << ": key = " << a[n] << ", ";
  pk(n);
  lk(n);
  rk(n);
  cout << endl;
}

void heaplify(int n) {
  int l = n;
  if (a[l] < rk(n))
    l = n * 2 + 1;
  if (a[l] < lk(n))
    l = n * 2;
  if (l != n) {
    swap(a[l], a[n]);
    heaplify(l);
  }
}

void ins(int v) {
  a[h] = v;
  int d = h;
  h++;
  while (pk(d) < a[d]) {
    swap(a[d / 2], a[d]);
    d = d / 2;
  }
}

ll ext() {
  ll res = a[1];
  h--;
  a[1] = a[h];
  a[h] = NIL;
  heaplify(1);
  return res;
}

int main() {
  h = 1;
  a[0] = -NIL;
  string s;
  int t;
  while (!cin.eof()) {
    cin >> s;
    if (s == "extract")
      cout << ext() << endl;
    else {
      cin >> t;
      ins(t);
    }
  }
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

ll a[20000000] = {NIL};
int h;

ll rk(int n) {
  if (n * 2 + 1 <= h)
    return a[n * 2 + 1];
  return NIL;
}
ll lk(int n) {
  if (n * 2 <= h)
    return a[n * 2];
  return NIL;
}
ll pk(int n) {
  if (n / 2 <= h)
    return a[n / 2];
  return NIL;
}

void print(int n) {
  cout << "node " << n << ": key = " << a[n] << ", ";
  pk(n);
  lk(n);
  rk(n);
  cout << endl;
}

void heaplify(int n) {
  int l = n;
  if (a[l] < rk(n))
    l = n * 2 + 1;
  if (a[l] < lk(n))
    l = n * 2;
  if (l != n) {
    swap(a[l], a[n]);
    heaplify(l);
  }
}

void ins(int v) {
  a[h] = v;
  int d = h;
  h++;
  while (pk(d) < a[d]) {
    swap(a[d / 2], a[d]);
    d = d / 2;
  }
}

ll ext() {
  ll res = a[1];
  h--;
  a[1] = a[h];
  a[h] = NIL;
  heaplify(1);
  return res;
}

int main() {
  h = 1;
  a[0] = -NIL;
  string s;
  int t;
  while (!cin.eof()) {
    cin >> s;
    if (s == "extract")
      cout << ext() << endl;
    else {
      cin >> t;
      ins(t);
    }
  }
  return 0;
}
Problem p02289 (C++)

Buggy submission (status: 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) {
    if (A[l] > A[maxid])
      maxid = l;
  }
  if (r <= H) {
    if (A[r] > A[maxid])
      maxid = r;
  }
  if (maxid == i)
    return;
  x = A[maxid];
  A[maxid] = A[i];
  A[i] = x;
  max_heapify(A, maxid, H);
}

int main() {
  int A[2001];
  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;
}

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) {
    if (A[l] > A[maxid])
      maxid = l;
  }
  if (r <= H) {
    if (A[r] > A[maxid])
      maxid = r;
  }
  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;
}