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

//
// Created by sanyinchen on 17/4/24.
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_9_B
//

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

using namespace std;

void _print(vector<int> res) {
  for (int i = 1; i < res.size(); i++) {
    cout << " " << res[i];
  }
  cout << endl;
}

void maxHeap(vector<int> &heap, int i) {

  int left = 2 * i;
  int right = 2 * i + 1;
  int lagest = i;
  if (left < heap.size() && left >= 1) {
    if (heap[left] > heap[lagest]) {
      lagest = left;
    }
  }

  if (right < heap.size() && right >= 1) {
    if (heap[right] > heap[lagest]) {
      lagest = right;
    }
  }

  if (lagest != i) {
    swap(heap[lagest], heap[i]);
    maxHeap(heap, lagest);
  }
}

void buildHeap(vector<int> &heap) {
  for (int i = heap.size() / 2; i >= 1; i--) {
    maxHeap(heap, i);
  }
}

vector<int> heapSort(vector<int> heap) {
  buildHeap(heap);
  _print(heap);
  //    while (heap.size() > 1) {
  //
  //        cout << " " << heap[1];
  //        swap(heap[1], heap[heap.size() - 1]);
  //        heap.pop_back();
  //        maxHeap(heap, 1);
  //    }
}

int main() {
  // freopen("../files/in.txt", "r", stdin);
  int n, temp;
  cin >> n;
  vector<int> input;
  input.push_back(0);
  for (int i = 0; i < n; i++) {
    cin >> temp;
    input.push_back(temp);
  }
  heapSort(input);

  return 0;
}

Error: -11

Fixed submission:

//
// Created by sanyinchen on 17/4/24.
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_9_B
//

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

using namespace std;

void _print(vector<int> res) {
  for (int i = 1; i < res.size(); i++) {
    cout << res[i] << " ";
  }
  cout << endl;
}

void maxHeap(vector<int> &heap, int i) {

  int left = 2 * i;
  int right = 2 * i + 1;
  int lagest = i;
  if (left < heap.size() && left >= 1) {
    if (heap[left] > heap[lagest]) {
      lagest = left;
    }
  }

  if (right < heap.size() && right >= 1) {
    if (heap[right] > heap[lagest]) {
      lagest = right;
    }
  }

  if (lagest != i) {
    swap(heap[lagest], heap[i]);
    maxHeap(heap, lagest);
  }
}

void buildHeap(vector<int> &heap) {
  for (int i = heap.size() / 2; i >= 1; i--) {
    maxHeap(heap, i);
  }
}

vector<int> heapSort(vector<int> heap) {
  buildHeap(heap);
  bool first = true;
  while (heap.size() > 1) {
    if (first) {
      first = false;
    } else {
      cout << " ";
    }
    cout << heap[1];
    swap(heap[1], heap[heap.size() - 1]);
    heap.pop_back();
    maxHeap(heap, 1);
  }
}

int main() {
  // freopen("../files/in.txt", "r", stdin);
  int n, temp;
  cin >> n;
  vector<int> input(n + 1, 0);

  for (int i = 0; i < n; i++) {
    cin >> temp;
    input.push_back(temp);
  }
  heapSort(input);
  // cout << endl;
  return 0;
}
Problem p02288 (C++) — buggy submission, verdict: 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[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);
  }
}

Error: 0

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[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);
  }
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

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

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

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


Error: 0

Fixed submission:

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

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

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

#include <iostream>
using namespace std;

#define INF 2000000005

int hsize;
int A[500005];

void modheap(int i) {
  int l = i * 2 + 1;
  int r = i * 2 + 2;
  int larger = i;
  if (l < hsize && A[larger] < A[l])
    larger = l;
  if (r < hsize && A[larger] < A[r])
    larger = r;
  if (larger != i) {
    swap(A[larger], A[i]);
    modheap(larger);
  }
}

void buildheap() {
  for (int i = hsize / 2 - 1; i >= 0; i--)
    modheap(i);
}

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

  buildheap();

  for (int i = 0; i < hsize; i++)
    cout << " " << A[i];
  cout << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define INF 2000000005

int hsize;
int A[500000];

void modheap(int i) {
  int l = i * 2 + 1;
  int r = i * 2 + 2;
  int larger = i;
  if (A[larger] < A[l])
    larger = l;
  if (A[larger] < A[r])
    larger = r;
  if (larger != i) {
    swap(A[larger], A[i]);
    modheap(larger);
  }
}

void buildheap() {
  for (int i = hsize / 2 - 1; i >= 0; i--)
    modheap(i);
}

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

  buildheap();

  for (int i = 0; i < hsize; i++)
    cout << " " << A[i];
  cout << endl;

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, char> pic;
typedef priority_queue<pii, vector<pii>, greater<pii>> RPQ;
#define mk make_pair
#define INF (1 << 30)
#define INFL (1ll << 60ll)
#define mod 1000000007

int N, heap[600000];

void maxheapify(int i) {
  if (i > N / 2)
    return;
  int l = i * 2;
  int r = i * 2 + 1;
  int largest = i;
  if (heap[i] < heap[l] && l <= N) {
    largest = l;
  }
  if (r <= N) {
    if (heap[largest] < heap[r])
      largest = r;
  }
  if (largest != i) {
    swap(heap[i], heap[largest]);
    maxheapify(largest);
  }
}

signed main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> heap[i];
  }
  for (int i = N / 2; i >= 1; i--) {
    maxheapify(i);
  }
  for (int i = 1; i <= N; i++) {
    printf(" %lld", heap[i]);
  }
  printf("\\n");

  return 0;
}

/*

*/


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// #define int long long
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, char> pic;
typedef priority_queue<pii, vector<pii>, greater<pii>> RPQ;
#define mk make_pair
#define INF (1 << 30)
#define INFL (1ll << 60ll)
#define mod 1000000007

int N, heap[600000];

void maxheapify(int i) {
  int l = i * 2;
  int r = i * 2 + 1;
  int largest = i;
  if (heap[i] < heap[l] && l <= N) {
    largest = l;
  }
  if (r <= N) {
    if (heap[largest] < heap[r])
      largest = r;
  }
  if (largest != i) {
    swap(heap[i], heap[largest]);
    maxheapify(largest);
  }
}

signed main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> heap[i];
  }
  for (int i = N / 2; i >= 1; i--) {
    maxheapify(i);
  }
  for (int i = 1; i <= N; i++) {
    printf(" %d", heap[i]);
  }
  printf("\\n");

  return 0;
}

/*

*/
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>

#define MAX 500000

int n;
int A[MAX + 1];

void maxHeapfy(int A[], int root) {
  int left = root * 2;
  int right = root * 2 + 1;

  int max = root;
  if (left <= n && A[max] < A[left]) {
    max = left;
  }
  if (right <= n && A[max] < A[right]) {
    max = right;
  }

  if (max != root) {
    std::swap(A[root], A[max]);
    maxHeapfy(A, max);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &A[i]);
  }
  for (int i = n / 2; i > 0; --i)
    maxHeapfy(A, i);
  for (int i = 1; i <= n; ++i)
    printf(" %d", A[i]);
  printf("\\n");

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>

#define MAX 500000
int A[MAX];

void maxHeapfy(int A[], int root) {
  int left = root * 2;
  int right = root * 2 + 1;

  int max = root;
  if (A[max] < A[left]) {
    max = left;
  }
  if (A[max] < A[right]) {
    max = right;
  }

  if (max != root) {
    std::swap(A[root], A[max]);
    maxHeapfy(A, max);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &A[i]);
  }
  for (int i = n / 2; i > 0; --i)
    maxHeapfy(A, 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 <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");
}

Error: 0

Fixed submission:

#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");
}
Problem p02288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
long int A[500000];

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

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

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
long long int A[500000];

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

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

int main() {
  cin >> n;
  A[0] = 0;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  buildMaxHeap();
  for (int i = 1; i < n; i++)
    cout << " " << A[i];
  cout << " " << A[n] << "\\n";
  return 0;
}
Problem p02288 (C++) — buggy submission, verdict: 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[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);
}

Error: 0

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[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);
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, x, n) for (int i = x; i < n; i++)
#define pb push_back
#define int long long
#define MAX 2000006
#define NIL -1
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int h, A[MAX];
void maxheap(int i) {
  int l = 2 * i;
  int r = 2 * i + 1;
  int lgst;
  if (l <= h && A[l] > A[i])
    lgst = l;
  else
    lgst = i;
  if (r <= h && A[r] > A[lgst])
    lgst = r;
  if (lgst != i) {
    swap(A[i], A[lgst]);
    maxheap(lgst);
  }
}

signed main() {
  cin >> h;
  loop(i, 1, h + 1) cin >> A[i];
  for (int i = h / 2; i >= 1; i--)
    maxheap(i);
  loop(i, 1, h + 1) cout << " " << A[i];
  cout << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, x, n) for (int i = x; i < n; i++)
#define pb push_back
#define int long long
#define MAX 100005
#define NIL -1
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int h, A[MAX];
void maxheap(int i) {
  int l = 2 * i;
  int r = 2 * i + 1;
  int lgst;
  if (l <= h && A[l] > A[i])
    lgst = l;
  else
    lgst = i;
  if (r <= h && A[r] > A[lgst])
    lgst = r;
  if (lgst != i) {
    swap(A[i], A[lgst]);
    maxheap(lgst);
  }
}

signed main() {
  cin >> h;
  loop(i, 1, h + 1) cin >> A[i];
  for (int i = h / 2; i > 0; i--)
    maxheap(i);
  loop(i, 1, h + 1) cout << " " << A[i];
  cout << endl;

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

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


Error: 0

Fixed submission:

#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');
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int heap[500000];
int h;

void max_heapify(int i) {
  int l = 2 * (i + 1) - 1;
  int r = l + 1;
  int largest;
  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]);
    max_heapify(largest);
  }
  return;
}

void build_max_heap() {
  for (int i = (h - 1) / 2; i >= 0; i--) {
    max_heapify(i);
  }
}

int main() {
  cin >> h;
  REP(i, h) cin >> heap[i];
  build_max_heap();
  REP(i, h) cout << " " << heap[i];
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int heap[250];
int h;

void max_heapify(int i) {
  int l = 2 * (i + 1) - 1;
  int r = l + 1;
  int largest;
  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]);
    max_heapify(largest);
  }
  return;
}

void build_max_heap() {
  for (int i = (h - 1) / 2; i >= 0; i--)
    max_heapify(i);
}

int main() {
  cin >> h;
  REP(i, h) cin >> heap[i];
  build_max_heap();
  REP(i, h) cout << " " << heap[i];
  cout << endl;
  return 0;
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

#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;
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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