alexjercan/bugnet huggingface dataset:
Problem p02288 (C++) — buggy submission, verdict: 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 = 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;
}


Error: TLE

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

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


Error: 0

Fixed submission:

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

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

int left(int i) { return i * 2; }
int right(int i) { return i * 2 + 1; }
void maxHeapify(std::vector<int> &A, int i) {
  int H = A.size();
  int l = left(i);
  int 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) {
    std::swap(A[i], A[largest]);
    maxHeapify(A, largest);
  }
}

void buildMaxHeap(std::vector<int> &A) {
  int H = A.size() + 1;
  for (std::size_t i = H / 2; i; --i) {
    maxHeapify(A, i);
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int H;
  std::cin >> H;
  std::vector<int> A(H + 1);
  for (std::size_t i = 1; i <= H; ++i) {
    int Value;
    std::cin >> Value;
    A[i] = Value;
  }
  buildMaxHeap(A);
  for (std::size_t i = 1; i <= H; ++i) {
    std::cout << " " << A[i];
  }
  std::cout << std::endl;
  return 0;
}

Error: 0

Fixed submission:

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

int left(int i) { return i * 2; }
int right(int i) { return i * 2 + 1; }
void maxHeapify(std::vector<int> &A, int i) {
  int H = A.size() + 1;
  int l = left(i);
  int 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) {
    std::swap(A[i], A[largest]);
    maxHeapify(A, largest);
  }
}

void buildMaxHeap(std::vector<int> &A) {
  int H = A.size() + 1;
  for (std::size_t i = H / 2; i; --i) {
    maxHeapify(A, i);
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int H;
  std::cin >> H;
  std::vector<int> A(H + 1);
  for (std::size_t i = 1; i <= H; ++i) {
    int Value;
    std::cin >> Value;
    A[i] = Value;
  }
  buildMaxHeap(A);
  for (std::size_t i = 1; i <= H; ++i) {
    std::cout << " " << A[i];
  }
  std::cout << std::endl;
  return 0;
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX 1000000

int n, H[MAX];

void maxHeap(int i) {
  int left = i * 2;
  int right = i * 2 + 1;

  if (left > n)
    return;

  int max = left;
  if (right <= n)
    max = H[left] >= H[right] ? left : right;
  max = H[i] >= H[max] ? i : max;

  if (max != i) {
    std::swap(H[i], H[max]);
    maxHeap(max);
  }
}

int main() {
  int key;

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

  for (int i = n / 2; i >= 1; i--) {
    maxHeap(i);
  }

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

  return 0;
}


Error: 0

Fixed submission:

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

#define MAX 1000000

int n, H[MAX];

void maxHeap(int i) {
  int left = i * 2;
  int right = i * 2 + 1;

  int max = left;
  if (right <= n)
    max = H[left] >= H[right] ? left : right;
  max = H[i] >= H[max] ? i : max;

  if (max != i) {
    std::swap(H[i], H[max]);
    maxHeap(max);
  }
}

int main() {
  int key;

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

  for (int i = n / 2; i >= 1; i--) {
    maxHeap(i);
  }

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

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

#include <cstdio>
#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_V 500000
int heap[MAX_V]{0};

int left(int i) { return 2 * i + 1; }
int right(int i) { return 2 * i + 2; }
int parent(int i) { return (i - 1) / 2; }
bool has_left(int size, int i) { return size > left(i); }
bool has_right(int size, int i) { return size > right(i); }

void max_heapify(int n, int i) {
  int l, r, largest;
  l = left(i);
  r = right(i);
  if (has_left(n, i) && heap[l] > heap[i])
    largest = l;
  else
    largest = i;
  if (has_right(n, i) && heap[r] > heap[largest])
    largest = r;
  if (largest != i) {
    swap(heap[i], heap[largest]);
    max_heapify(n, largest);
  }
}
void build_max_heap(int size) {
  for (int i = (size - 1) / 2; i >= 0; i--)
    max_heapify(size, i);
}

int main() {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d", &heap[i]);
  build_max_heap(n);
  REP(i, n) printf(" %d", heap[i]);
  puts("");
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_V 250
int heap[MAX_V]{0};

int left(int i) { return 2 * i + 1; }
int right(int i) { return 2 * i + 2; }
int parent(int i) { return (i - 1) / 2; }
bool has_left(int size, int i) { return size > left(i); }
bool has_right(int size, int i) { return size > right(i); }

void max_heapify(int n, int i) {
  int l, r, largest;
  l = left(i);
  r = right(i);
  if (has_left(n, i) && heap[l] > heap[i])
    largest = l;
  else
    largest = i;
  if (has_right(n, i) && heap[r] > heap[largest])
    largest = r;
  if (largest != i) {
    swap(heap[i], heap[largest]);
    max_heapify(n, largest);
  }
}
void build_max_heap(int size) {
  for (int i = (size - 1) / 2; i >= 0; i--)
    max_heapify(size, i);
}

int main() {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d", &heap[i]);
  build_max_heap(n);
  REP(i, n) printf(" %d", heap[i]);
  puts("");
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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

// 建最大堆
#include <algorithm>
#include <iostream>
using namespace std;
#define maxx 2000000
int H, A[maxx + 1];
void maxHeapify(int i) {
  int l, r, largest;
  l = i * 2;
  r = i * 2 + 1;
  if (H >= l && A[l] > A[i])
    largest = l;
  else
    largest = i;
  if (r <= H && A[r] > A[largest])
    largest = r;
  if (largest != i) {
    int tt = A[i];
    A[i] = A[largest];
    A[largest] = tt;
    // 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: TLE

Fixed submission:

// 建最大堆
#include <iostream>
using namespace std;
#define maxx 2000000
int H, A[maxx + 1];
void maxHeapify(int i) {
  int l, r, largest;
  l = i * 2;
  r = i * 2 + 1;
  if (H >= l && A[l] > A[i])
    largest = 1;
  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 <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;
}

Error: 0

Fixed submission:

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

#include <algorithm>
#include <cstdio>
#pragma warning(disable : 4996)

int h, A[600000];

void maxHeapify(int i) {
  int l, r, largest;
  l = i * 2;
  r = l + 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) {
    std::swap(A[i], A[largest]);
    maxHeapify(largest);
  }
}

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
#define _USE_MATH_DEFINES // M_PI????????????????????????
#include <math.h>
#pragma warning(disable : 4996)
using namespace std;

#define MAX 2000000

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

#include <iostream>
using namespace std;

int H, N[5000000];

void MaxHeap(int i) {
  int l = 2 * i;
  int r = l + 1;
  int largest = i;
  int temp;

  if (l <= H && N[l] > N[largest]) //---------
    largest = l;

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

  if (largest != i) {
    temp = N[i];
    N[i] = N[largest];
    N[largest] = temp;

    MaxHeap(largest);
  }
}

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

  cin >> H;
  for (int i = 1; i <= H; i++)
    cin >> N[i];

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

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

  cout << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int H, N[5000000];

void MaxHeap(int i) {
  int l = 2 * i;
  int r = l + 1;
  int largest = i;
  int temp;

  if (N[l] > N[largest])
    largest = l;

  if (N[r] > N[largest])
    largest = r;

  if (largest != i) {
    temp = N[i];
    N[i] = N[largest];
    N[largest] = temp;

    MaxHeap(largest);
  }
}

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

  cin >> H;
  for (int i = 1; i <= H; i++)
    cin >> N[i];

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

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

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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define MAX 1000000000000000000LL
long long inf = 1000000007;
using namespace std;
int H, A[2000001];

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 <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define MAX 1000000000000000000LL
long long inf = 1000000007;
using namespace std;
int H, A[200001];

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


Error: 0

Fixed submission:

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

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

int left(int i) { return i * 2; }
int right(int i) { return i * 2 + 1; }
void maxHeapify(std::vector<int> &A, int i) {
  int H = A.size();
  int l = left(i);
  int 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) {
    std::swap(A[i], A[largest]);
    maxHeapify(A, largest);
  }
}

void buildMaxHeap(std::vector<int> &A) {
  int H = A.size() + 1;
  for (std::size_t i = H / 2; i; --i) {
    maxHeapify(A, i);
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int H;
  std::cin >> H;
  std::vector<int> A(H + 1);
  for (std::size_t i = 1; i <= H; ++i) {
    int Value;
    std::cin >> Value;
    A[i] = Value;
  }
  buildMaxHeap(A);
  for (std::size_t i = 1; i <= H; ++i) {
    std::cout << " " << A[i];
  }
  std::cout << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 2000000
using namespace std;
int n;
int t[maxn + 1];

int Max(int a, int b, int c) {
  int d;
  if (b > n)
    return t[a] > t[c] ? a : c;
  if (t[a] > t[b])
    d = a;
  else
    d = b;
  return t[d] > t[c] ? d : c;
}

void Swap(int a, int b) {
  int m = t[a];
  t[a] = t[b];
  t[b] = m;
  return;
}

void maxheapify(int num) {
  int l, r, largest;
  l = num * 2;
  r = num * 2 + 1;
  largest = Max(l, r, num);
  if (largest != num) {
    Swap(largest, num);
    maxheapify(largest);
  }
  return;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &t[i]);
  for (int i = n / 2; i >= 1; --i)
    maxheapify(i);
  for (int i = 1; i <= n; ++i)
    printf(" %d", t[i]);
  cout << endl;
  return 0;
}
Problem p02288 (C++) — buggy submission, verdict: Runtime Error

#define scanf_s scanf
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define MAX 500000
// #define MAX_ 1000
static int H;
void maxHeapify(long long int A[], int i) {
  int l = i * 2, r = i * 2 + 1, largest;
  long long int x;
  if (l <= H && A[l] > A[i])
    largest = l;
  else
    largest = i;
  if (r <= H && A[r] > A[largest])
    largest = r;
  if (largest != i) {
    x = A[i];
    A[i] = A[largest];
    A[largest] = x;
    maxHeapify(A, largest);
  }
}
void buildMaxHeap(long long int A[]) {
  for (int i = H / 2; i >= 1; --i) {
    maxHeapify(A, i);
  }
}

int main(void) {
  long long int Heaps[MAX + 1];
  scanf_s("%d", &H);
  for (int i = 1; i <= H; ++i) {
    scanf_s("%lld", &Heaps[i]);
  }
  buildMaxHeap(Heaps);
  for (int i = 1; i <= H; ++i) {
    printf(" %lld", Heaps[i]);
  }
  printf("\\n");
}

Error: 0

Fixed submission:

#define scanf_s scanf
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define MAX 500 // 000
// #define MAX_ 1000
static int H;
void maxHeapify(long long int A[], int i) {
  int l = i * 2, r = i * 2 + 1, largest;
  long long int x;
  if (l <= H && A[l] > A[i])
    largest = l;
  else
    largest = i;
  if (r <= H && A[r] > A[largest])
    largest = r;
  if (largest != i) {
    x = A[i];
    A[i] = A[largest];
    A[largest] = x;
    maxHeapify(A, largest);
  }
}
void buildMaxHeap(long long int A[]) {
  for (int i = H / 2; i >= 1; --i) {
    maxHeapify(A, i);
  }
}

int main(void) {
  long long int Heaps[MAX + 1];
  scanf_s("%d", &H);
  for (int i = 1; i <= H; ++i) {
    scanf_s("%lld", &Heaps[i]);
  }
  buildMaxHeap(Heaps);
  for (int i = 1; i <= H; ++i) {
    printf(" %lld", Heaps[i]);
  }
  printf("\\n");
}