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

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


Error: 0

Fixed submission:

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

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

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


Error: -11

Fixed submission:

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

#include <cstdio>
#include <iostream>
using namespace std;
int N;
int parent(int i) { return i / 2; }
int left(int i) { return i * 2; }
int right(int i) { return i * 2 + 1; }

void maxHeapify(int A[], int i) {
  int l = left(i);
  int r = right(i);
  int largest = 0;
  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[largest], A[i]);
    maxHeapify(A, largest);
  }
}
int main() {
  scanf("%d", &N);
  int A[500000];
  for (int i = 1; i <= N; i++)
    scanf("%d", &A[i]);
  for (int i = N / 2; i >= 1; i--)
    maxHeapify(A, i);
  for (int i = 1; i <= N; i++) {
    printf(" ");
    printf("%d", A[i]);
  }
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int N;
int parent(int i) { return i / 2; }
int left(int i) { return i * 2; }
int right(int i) { return i * 2 + 1; }

void maxHeapify(int A[], int i) {
  int l = left(i);
  int r = right(i);
  int largest = 0;
  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[largest], A[i]);
    maxHeapify(A, largest);
  }
}
int main() {
  scanf("%d", &N);
  int A[200000];
  for (int i = 1; i <= N; i++)
    scanf("%d", &A[i]);
  for (int i = N / 2; i >= 1; i--)
    maxHeapify(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 <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);
}


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

#include <iostream>
using namespace std;

const int MAX = 500000;
int H, i, A[MAX + 1];

int parent(int i) { return i / 2; }
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[l] > A[i]) {
    largest = l;
  } else {
    largest = i;
  }
  if (r <= H && A[r] > A[largest]) {
    largest = r;
  }

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

int main() {
  ios::sync_with_stdio(false);
  cin >> H;
  for (i = 1; i <= H; i++)
    cin >> A[i];
  for (i = H / 2; i >= 1; i--) {
    maxHeapify(A, i);
  }
  for (i = 1; i <= H; i++)
    cout << " " << A[i];
  cout << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

const int MAX = 100000;
int H, i, A[MAX + 1];

int parent(int i) { return i / 2; }
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[l] > A[i]) {
    largest = l;
  } else {
    largest = i;
  }
  if (r <= H && A[r] > A[largest]) {
    largest = r;
  }

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

int main() {
  ios::sync_with_stdio(false);
  cin >> H;
  for (i = 1; i <= H; i++)
    cin >> A[i];
  for (i = H / 2; i >= 1; i--) {
    maxHeapify(A, i);
  }
  for (i = 1; i <= H; i++)
    cout << " " << A[i];
  cout << endl;
}
Problem p02288 (C++) — buggy submission, verdict: 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[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;
}


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

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

Error: 0

Fixed submission:

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

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

Error: 0

Fixed submission:

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

// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_9_B: Maximum Heap

#include <algorithm>
#include <cstdio>

class MaxHeap {
  int n_;
  int *data_;

public:
  MaxHeap(unsigned int n) : n_(n), data_(new int[n]) {}

  ~MaxHeap() { delete[] data_; }

  int &operator[](unsigned int i) { return data_[i]; }

  void build() {
    for (auto i = (n_ / 2) - 1; i >= 0; --i) {
      max_heapify(i);
    }
  }

  void print() const {
    for (auto i = 0; i < n_; ++i) {
      std::printf(" %d", data_[i]);
    }
    std::printf("\\n");
  }

private:
  void max_heapify(int i) {
    while (i < n_ / 2) {
      auto c = 2 * i + 1; // left
      if (c + 1 < n_ && data_[c] < data_[c + 1]) {
        ++c; // right
      }
      if (data_[c] < data_[i]) {
        return;
      }
      std::swap(data_[c], data_[i]);
      i = c;
    }
  }
};

int main() {
  int h;
  std::scanf("%d", &h);
  MaxHeap mh(h);
  for (auto i = 0; i < h; ++i) {
    std::scanf("%d", &mh[i]);
  }
  mh.build();
  mh.print();
}

// eof

Error: 0

Fixed submission:

// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_9_B: Maximum Heap

#include <algorithm>
#include <cstdio>

class MaxHeap {
  int n_;
  int *data_;

public:
  MaxHeap(unsigned int n) : n_(n), data_(new int[n]) {}

  ~MaxHeap() { delete[] data_; }

  int &operator[](unsigned int i) { return data_[i]; }

  void build() {
    for (auto i = (n_ / 2) - 1; i >= 0; --i) {
      max_heapify(i);
    }
  }

  void print() const {
    for (auto i = 0; i < n_; ++i) {
      std::printf(" %d", data_[i]);
    }
    std::printf("\\n");
  }

private:
  void max_heapify(int i) {
    auto c = 2 * i + 1; // left
    if (c + 1 < n_ && data_[c] < data_[c + 1]) {
      ++c; // right
    }
    if (data_[c] < data_[i]) {
      return;
    }
    std::swap(data_[c], data_[i]);
    max_heapify(c);
  }
};

int main() {
  int h;
  std::scanf("%d", &h);
  MaxHeap mh(h);
  for (auto i = 0; i < h; ++i) {
    std::scanf("%d", &mh[i]);
  }
  mh.build();
  mh.print();
}

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

#include <iostream>
using namespace std;
int h, *a;
void maxHeapify(int i) {
  int l = 2 * i, r = 2 * i + 1, 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);
  }
}
int main(void) {
  scanf("%d", &h);
  a = new int[4 * h + 1];
  for (int i = 1; i <= h; i++)
    scanf("%d", &a[i]);
  for (int i = h / 2; i >= 1; i--)
    maxHeapify(i);
  for (int i = 1; i <= h; i++)
    printf(" %d", a[i]);
  printf("\\n");
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <utility>
#define maxn 100005
using namespace std;
int n, a[maxn];
void max(int i) {
  int l = 2 * i, r = 2 * i + 1, large;
  if (l <= n && a[i] < a[l])
    large = l;
  else
    large = i;
  if (r <= n && a[large] < a[r])
    large = r;
  if (large != i) {
    swap(a[i], a[large]);
    max(large);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  for (int i = n / 2; i >= 1; i--)
    max(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 <algorithm>
#include <stdio.h>
using namespace std;
int a[2000000];
int H;
void minitree(int i) {
  int l = i * 2;
  int r = i * 2 + 1;
  int bigest;
  if (a[i] < a[l] && l <= H)
    bigest = l;
  else
    bigest = i;
  if (a[bigest] < a[r] && r <= H)
    bigest = r;
  if (bigest != i) {
    swap(a[bigest], a[i]);
    minitree(bigest);
  }
}
int main() {

  scanf("%d", &H);
  for (int i = 1; i <= H; i++)
    scanf("%d", &a[i]);
  for (int i = H / 2; i >= 1; i--)
    minitree(i);
  for (int i = 1; i <= H; i++)
    printf(" %d", a[i]);
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

const int maxn = 500000 + 5;

int n, tree[maxn];

int max(int node, int child) {
  return (tree[child] > tree[node] && child <= n) ? child : node;
}

void MaxHeapify(int i) {
  int Max_Node = max(max(i, i * 2), i * 2 + 1);
  if (Max_Node != i)
    swap(tree[i], tree[Max_Node]), MaxHeapify(Max_Node);
}

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

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


Error: 0

Fixed submission:

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

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

Error: 0

Fixed submission:

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

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


Error: 0

Fixed submission:

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