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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

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

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

int main() {
  int n, i, x;
  string com;
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", &com);
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

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

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

int main() {
  int n, i, x;
  string com;
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    }
  }

  return 0;
}
Problem p02283 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

#define N 4500000
#define NIL -1

int key[N];
int parent[N];
int leftc[N];
int rightc[N];

int root = NIL;
void insert(int z) {
  int x, y;

  y = NIL;
  x = root;

  while (x != NIL) {
    y = x;
    if (key[z] < key[x])
      x = leftc[x];
    else
      x = rightc[x];
  }

  parent[z] = y;

  if (y == NIL)
    root = z;

  else if (key[z] < key[y])
    leftc[y] = z;

  else
    rightc[y] = z;
}

void inorderprint(int now) {
  if (now != NIL) {
    inorderprint(leftc[now]);
    printf(" %d", key[now]);
    inorderprint(rightc[now]);
  }
}

void preorderprint(int now) {
  if (now != NIL) {
    printf(" %d", key[now]);
    preorderprint(leftc[now]);
    preorderprint(rightc[now]);
  }
}

int main() {

  int n, i, j, data;
  int z = 0;
  char str[10];

  for (i = 0; i < N; i++) {
    parent[i] = NIL;
    key[i] = NIL;
    rightc[i] = NIL;
    leftc[i] = NIL;
  }

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", str);
    if (strcmp(str, "insert") == 0) {
      scanf("%d", &data);
      key[z] = data;
      insert(z);
      z++;
    } else if (strcmp(str, "print") == 0) {
      inorderprint(root);
      cout << endl;
      preorderprint(root);
      cout << endl;
    }
  }

  return 0;
}

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

#define N 4000000
#define NIL -1

int key[N];
int parent[N];
int leftc[N];
int rightc[N];

int root = NIL;
void insert(int z) {
  int x, y;

  y = NIL;
  x = root;

  while (x != NIL) {
    y = x;
    if (key[z] < key[x])
      x = leftc[x];
    else
      x = rightc[x];
  }

  parent[z] = y;

  if (y == NIL)
    root = z;

  else if (key[z] < key[y])
    leftc[y] = z;

  else
    rightc[y] = z;
}

void inorderprint(int now) {
  if (now != NIL) {
    inorderprint(leftc[now]);
    printf(" %d", key[now]);
    inorderprint(rightc[now]);
  }
}

void preorderprint(int now) {
  if (now != NIL) {
    printf(" %d", key[now]);
    preorderprint(leftc[now]);
    preorderprint(rightc[now]);
  }
}

int main() {

  int n, i, j, data;
  int z = 0;
  char str[10];

  for (i = 0; i < N; i++) {
    parent[i] = NIL;
    key[i] = NIL;
    rightc[i] = NIL;
    leftc[i] = NIL;
  }

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", str);
    if (strcmp(str, "insert") == 0) {
      scanf("%d", &data);
      key[z] = data;
      insert(z);
      z++;
    } else if (strcmp(str, "print") == 0) {
      inorderprint(root);
      cout << endl;
      preorderprint(root);
      cout << endl;
    }
  }

  return 0;
}
Problem p02283 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

class Node {
public:
  Node *l, *r;
  int key;
};

void insert(Node **root, int key) {
  Node **cur = root, *node = new Node;
  node->l = NULL;
  node->r = NULL;
  node->key = key;
  while (NULL != *cur) {
    if (node->key < (*cur)->key)
      cur = &((*cur)->l);
    else
      cur = &((*cur)->r);
  }
  *cur = node;
  return;
}

void inorder(Node *cur) {
  if (cur == NULL)
    return;
  inorder(cur->l);
  cout << " " << cur->key;
  inorder(cur->r);
}

void preorder(Node *cur) {
  if (cur == NULL)
    return;
  cout << " " << cur->key;
  preorder(cur->l);
  preorder(cur->r);
  free(cur);
}

int main() {
  Node *root = NULL;
  int n, key;
  string command;
  cin >> n;
  while (n--) {
    cin >> command;
    if ("insert" == command) {
      cin >> key;
      insert(&root, key);
    }
    if ("print" == command) {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

class Node {
public:
  Node *l, *r;
  int key;
};

void insert(Node **root, int key) {
  Node **cur = root, *node = new Node;
  node->l = NULL;
  node->r = NULL;
  node->key = key;
  while (NULL != *cur) {
    if (node->key < (*cur)->key)
      cur = &((*cur)->l);
    else
      cur = &((*cur)->r);
  }
  *cur = node;
  return;
}

void inorder(Node *cur) {
  if (cur == NULL)
    return;
  inorder(cur->l);
  cout << " " << cur->key;
  inorder(cur->r);
}

void preorder(Node *cur) {
  if (cur == NULL)
    return;
  cout << " " << cur->key;
  preorder(cur->l);
  preorder(cur->r);
}

int main() {
  Node *root = NULL;
  int n, key;
  string command;
  cin >> n;
  while (n--) {
    cin >> command;
    if ("insert" == command) {
      cin >> key;
      insert(&root, key);
    }
    if ("print" == command) {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }
  return 0;
}
Problem p02283 (C++)

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

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

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }

  x->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

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

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    }
  }

  return 0;
}


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

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

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }

  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

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

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    }
  }

  return 0;
}
Problem p02283 (C++)

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

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

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }

  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

void inorder(Node *u) {
  if (u = NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}
void preorder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

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

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    }
  }

  return 0;
}

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

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

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }

  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}
void preorder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

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

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "print") {
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    }
  }

  return 0;
}
Problem p02283 (C++)

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

#define N 100000
int key[N];
int rt[N];
int lt[N];
int parent[N];
int np = 0;
int root = -1;

void insert(int k) {
  int y = -1;
  int x = root;
  while (x != -1) {
    y = x;
    if (k < key[x]) {
      x = lt[x];
    } else {
      x = rt[x];
    }
  }
  if (y == -1) {
    root = np;
  } else {
    if (k < key[y]) {
      lt[y] = np;
    } else {
      rt[y] = np;
    }
  }
  key[np] = k;
  parent[np] = y;
  np++;
}

void saiki1(int x) {
  if (lt[x] != -1)
    saiki1(lt[x]);
  cout << " " << key[x];
  if (rt[x] != -1)
    saiki1(rt[x]);
}

void saiki2(int x) {
  cout << " " << key[x];
  if (lt[x] != -1)
    saiki2(lt[x]);
  if (rt[x] != -1)
    saiki2(rt[x]);
}
void print() {
  saiki1(root);
  cout << endl;
  saiki2(root);
  cout << endl;
}

int main() {

  for (int i = 0; i < N; i++) {
    key[i] = -1;
    parent[i] = -1;
    lt[i] = -1;
    rt[i] = -1;
  }

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    //		cout << i << endl;
    string s;
    cin >> s;
    if (s == "print") {
      print();
    } else if (s == "insert") {
      int k;
      cin >> k;
      insert(k);
    }
  }

  return 0;
}

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

#define N 500000
int key[N];
int rt[N];
int lt[N];
int parent[N];
int np = 0;
int root = -1;

void insert(int k) {
  int y = -1;
  int x = root;
  while (x != -1) {
    y = x;
    if (k < key[x]) {
      x = lt[x];
    } else {
      x = rt[x];
    }
  }
  if (y == -1) {
    root = np;
  } else {
    if (k < key[y]) {
      lt[y] = np;
    } else {
      rt[y] = np;
    }
  }
  key[np] = k;
  parent[np] = y;
  np++;
}

void saiki1(int x) {
  if (lt[x] != -1)
    saiki1(lt[x]);
  cout << " " << key[x];
  if (rt[x] != -1)
    saiki1(rt[x]);
}

void saiki2(int x) {
  cout << " " << key[x];
  if (lt[x] != -1)
    saiki2(lt[x]);
  if (rt[x] != -1)
    saiki2(rt[x]);
}
void print() {
  saiki1(root);
  cout << endl;
  saiki2(root);
  cout << endl;
}

int main() {

  for (int i = 0; i < N; i++) {
    key[i] = -1;
    parent[i] = -1;
    lt[i] = -1;
    rt[i] = -1;
  }

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    //		cout << i << endl;
    string s;
    cin >> s;
    if (s == "print") {
      print();
    } else if (s == "insert") {
      int k;
      cin >> k;
      insert(k);
    }
  }

  return 0;
}
Problem p02283 (C++)

Buggy submission (status: Runtime Error):
/*
 * ALDS1_8_A.cpp
 *
 *  Created on: May 2, 2018
 *      Author: 13743
 */

#include <cstdio>
#include <cstdlib>

struct Node {
  int key;
  Node *pa, *lc, *rc;
};

Node *root = NULL;

void insert(int k) {
  Node *y = NULL;
  Node *x = root;
  Node *z = new Node;
  while (x != NULL) {
    y = x;
    if (k < y->key)
      x = y->lc;
    else
      x = y->rc;
  }
  z->pa = y;
  z->lc = NULL;
  z->rc = NULL;
  z->key = k;
  if (y == NULL) {
    root = z;
  } else if (k < y->key) {
    y->lc = z;
  } else {
    y->rc = z;
  }
}

void preorder(Node *u) {
  if (u == NULL)
    return;
  printf(" %d", u->key);
  preorder(u->lc);
  preorder(u->rc);
}

void inorder(Node *u) {
  if (u == NULL)
    return;
  inorder(u->lc);
  printf(" %d", u->key);
  inorder(u->rc);
}

void print() {
  inorder(root);
  printf("\n");
  preorder(root);
  printf("\n");
}

int main() {
  int m;
  scanf("%d", &m);
  char *s;
  int ke;
  for (int i = 0; i < m; i++) {
    scanf("%s", s);
    if (s[0] == 'i') {
      scanf("%d", &ke);
      insert(ke);
    } else {
      print();
    }
  }
}


Fixed submission:
/*
 * ALDS1_8_A.cpp
 *
 *  Created on: May 2, 2018
 *      Author: 13743
 */

#include <cstdio>
#include <cstdlib>

struct Node {
  int key;
  Node *pa, *lc, *rc;
};

Node *root = NULL;

void insert(int k) {
  Node *y = NULL;
  Node *x = root;
  Node *z = new Node;
  while (x != NULL) {
    y = x;
    if (k < y->key)
      x = y->lc;
    else
      x = y->rc;
  }
  z->pa = y;
  z->lc = NULL;
  z->rc = NULL;
  z->key = k;
  if (y == NULL) {
    root = z;
  } else if (k < y->key) {
    y->lc = z;
  } else {
    y->rc = z;
  }
}

void preorder(Node *u) {
  if (u == NULL)
    return;
  printf(" %d", u->key);
  preorder(u->lc);
  preorder(u->rc);
}

void inorder(Node *u) {
  if (u == NULL)
    return;
  inorder(u->lc);
  printf(" %d", u->key);
  inorder(u->rc);
}

void print() {
  inorder(root);
  printf("\n");
  preorder(root);
  printf("\n");
}

int main() {
  int m;
  scanf("%d", &m);
  char s[100];
  int ke;
  for (int i = 0; i < m; i++) {
    scanf("%s", s);
    if (s[0] == 'i') {
      scanf("%d", &ke);
      insert(ke);
    } else {
      print();
    }
  }
}
Problem p02284 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

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

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

bool find(int k, Node *u) {
  if (u == NIL)
    return false;
  if (u->key == k)
    return true;
  return find(k, u->left) || find(k, u->right);
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preoder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preoder(u->left);
  preoder(u->right);
}

int main() {
  int n;
  string s;

  cin >> n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    // scanf("%s", s);
    cin >> s;
    if (s == "print") {
      inorder(root);
      printf("\n");
      preoder(root);
      printf("\n");
    } else if (s == "insert") {
      int x;
      scanf("%d", &x);
      insert(x);
    } else if (s == "find") {
      int k;
      scanf("%d", &k);
      printf(("%s", find(k, root) ? "yes" : "no"));
      printf("\n");
    }
  }

  return 0;
}


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

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

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

bool find(int k, Node *u) {
  if (u == NIL)
    return false;
  if (u->key == k)
    return true;
  return (u->key > k) ? find(k, u->left) : find(k, u->right);
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preoder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preoder(u->left);
  preoder(u->right);
}

int main() {
  int n;
  string s;

  cin >> n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    // scanf("%s", s);
    cin >> s;
    if (s == "print") {
      inorder(root);
      printf("\n");
      preoder(root);
      printf("\n");
    } else if (s == "insert") {
      int x;
      scanf("%d", &x);
      insert(x);
    } else if (s == "find") {
      int k;
      scanf("%d", &k);
      printf(("%s", find(k, root) ? "yes" : "no"));
      printf("\n");
    }
  }

  return 0;
}
Problem p02284 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> T;
vector<long> v;

void in(long k) {
  if (k == -1)
    return;
  else {
    in(T[k][0]);
    cout << " " << v[k];
    in(T[k][1]);
    return;
  }
}

void pre(long k) {
  if (k == -1)
    return;
  else {
    cout << " " << v[k];
    pre(T[k][0]);
    pre(T[k][1]);
    return;
  }
}

int main() {
  string s;
  long n = 0, t = 0;

  cin >> n;

  T.assign(n, {-1, -1});
  v.assign(n, -2000000001);

  for (long i = 0; i < n; i++) {
    cin >> s;

    if (s == "insert") {
      cin >> v[t];

      long y = -1, x = 0;

      while (x != -1) {
        int m = 1;
        y = x;
        if (v[t] < v[x])
          m = 0;
        x = T[x][m];
      }

      int s = 1;

      if (i > 0 && y != -1) {
        if (v[t] < v[y])
          s = 0;
        T[y][s] = t;
      }
      t++;
    } else if (s == "print") {
      in(0);
      cout << endl;
      pre(0);
      cout << endl;
    } else if (s == "find") {
      long w = 0;
      cin >> w;
      std::vector<long>::iterator itr = find(v.begin(), v.end(), w);
      if (itr != v.end())
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}


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

vector<vector<int>> T;
vector<long> v;

void in(long k) {
  if (k == -1)
    return;
  else {
    in(T[k][0]);
    cout << " " << v[k];
    in(T[k][1]);
    return;
  }
}

void pre(long k) {
  if (k == -1)
    return;
  else {
    cout << " " << v[k];
    pre(T[k][0]);
    pre(T[k][1]);
    return;
  }
}

int main() {
  string s;
  long n = 0, t = 0;

  cin >> n;

  T.assign(n, {-1, -1});
  v.assign(n, -2000000001);

  for (long i = 0; i < n; i++) {
    cin >> s;

    if (s == "insert") {
      cin >> v[t];

      long y = -1, x = 0;

      while (x != -1) {
        int m = 1;
        y = x;
        if (v[t] < v[x])
          m = 0;
        x = T[x][m];
      }

      int s = 1;

      if (i > 0 && y != -1) {
        if (v[t] < v[y])
          s = 0;
        T[y][s] = t;
      }
      t++;
    } else if (s == "print") {
      in(0);
      cout << endl;
      pre(0);
      cout << endl;
    } else if (s == "find") {
      long w = 0;
      cin >> w;

      long x = 0;

      while (x != -1) {
        int m = 1;
        if (w == v[x])
          break;
        else if (w < v[x])
          m = 0;
        x = T[x][m];
      }

      if (x != -1)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02284 (C++)

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

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

Node *NIL = NULL;

void preorder(Node *v) {
  if (v == NIL)
    return;
  printf(" %d", v->key);
  preorder(v->left);
  preorder(v->right);
}

void inorder(Node *v) {
  if (v == NIL)
    return;
  inorder(v->left);
  printf(" %d", v->key);
  inorder(v->right);
}

void postorder(Node *v) {
  if (v == NIL)
    return;
  postorder(v->left);
  postorder(v->right);
  printf(" %d", v->key);
}

void insert(Node *&T, Node *z) {
  Node *y = NIL;
  Node *x = T;
  while (x != NIL) {
    y = x;
    if (z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->parent = y;

  if (y == NIL)
    T = z;
  else if (z->key < y->key)
    y->left = z;
  else
    y->right = z;
}

bool find(Node *T, int key) {
  Node *x = T;
  if (x == NIL)
    return false;
  if (x->key == key)
    return true;
  else if (key < x->key)
    return find(x->left, key);
  else
    return find(x->right, key);
}

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

  Node *T = NIL;
  for (int i = 0; i < n; ++i) {
    char cmd[20];
    int v;
    scanf("%s", cmd);
    if (cmd[0] == 'i') {
      scanf("%d", &v);
      Node *p = new Node;
      p->key = v;
      insert(T, p);
    } else if (cmd[0] == 'f') {
      scanf("%d", &v);
      printf("%s\n", find(T, v) ? "yes" : "no");
    } else if (cmd[0] == 'p') {
      inorder(T);
      printf("\n");
      preorder(T);
      printf("\n");
    }
  }

  return 0;
}

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

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

Node *NIL = NULL;

void preorder(Node *v) {
  if (v == NIL)
    return;
  printf(" %d", v->key);
  preorder(v->left);
  preorder(v->right);
}

void inorder(Node *v) {
  if (v == NIL)
    return;
  inorder(v->left);
  printf(" %d", v->key);
  inorder(v->right);
}

void postorder(Node *v) {
  if (v == NIL)
    return;
  postorder(v->left);
  postorder(v->right);
  printf(" %d", v->key);
}

void insert(Node *&T, Node *z) {
  Node *y = NIL;
  Node *x = T;
  while (x != NIL) {
    y = x;
    if (z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->parent = y;

  if (y == NIL)
    T = z;
  else if (z->key < y->key)
    y->left = z;
  else
    y->right = z;
}

bool find(Node *T, int key) {
  Node *x = T;
  if (x == NIL)
    return false;
  if (x->key == key)
    return true;
  else if (key < x->key)
    return find(x->left, key);
  else
    return find(x->right, key);
}

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

  Node *T = NIL;
  for (int i = 0; i < n; ++i) {
    char cmd[20];
    int v;
    scanf("%s", cmd);
    if (cmd[0] == 'i') {
      scanf("%d", &v);
      Node *p = new Node;
      p->key = v;
      p->left = NIL;
      p->right = NIL;
      insert(T, p);
    } else if (cmd[0] == 'f') {
      scanf("%d", &v);
      printf("%s\n", find(T, v) ? "yes" : "no");
    } else if (cmd[0] == 'p') {
      inorder(T);
      printf("\n");
      preorder(T);
      printf("\n");
    }
  }

  return 0;
}
Problem p02284 (C++)

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

struct node {
  int key;
  node *p, *l, *r;
};

int n;
node *N;

void tree_insert(node *z) {
  node *y = NULL;
  node *x = N;
  while (x != NULL) {
    y = x;
    if (x->key > z->key)
      x = x->l;
    else if (x->key < z->key)
      x = x->r;
  }
  z->p = y;
  if (y == NULL)
    N = z;
  else if (y->key < z->key)
    y->r = z;
  else
    y->l = z;
}

void tree_find(int num) {
  node *x = N;
  while (x != NULL) {
    if (x->key == num) {
      cout << "yes" << endl;
      return;
    }
    if (x->key > num)
      x = x->l;
    if (x->key < num)
      x = x->r;
  }
  cout << "no" << endl;
}

void in(node *x) {
  if (x->l != NULL)
    in(x->l);
  cout << " " << x->key;
  if (x->r != NULL)
    in(x->r);
}

void pre(node *x) {
  cout << " " << x->key;
  if (x->l != NULL)
    pre(x->l);
  if (x->r != NULL)
    pre(x->r);
}

void print() {
  node *x = N;
  in(x);
  cout << endl;
  pre(x);
  cout << endl;
}

int main() {
  N = NULL;
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "insert") {
      int num;
      cin >> num;
      node *z = new node();
      z->key = num;
      z->l = z->r = z->p = NULL;
      tree_insert(z);
    }
    if (s == "find") {
      int num;
      cin >> num;
      tree_find(num);
    }
    if (s == "print") {
      print();
    }
  }
  return 0;
}

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

struct node {
  int key;
  node *p, *l, *r;
};

int n;
node *N;

void tree_insert(node *z) {
  node *y = NULL;
  node *x = N;
  while (x != NULL) {
    y = x;
    if (x->key > z->key)
      x = x->l;
    else if (x->key < z->key)
      x = x->r;
  }
  z->p = y;
  if (y == NULL)
    N = z;
  else if (y->key < z->key)
    y->r = z;
  else
    y->l = z;
}

void tree_find(int num) {
  node *x = N;
  while (x != NULL) {
    if (x->key == num) {
      cout << "yes" << endl;
      return;
    }
    if (x->key > num)
      x = x->l;
    else
      x = x->r;
  }
  cout << "no" << endl;
}

void in(node *x) {
  if (x->l != NULL)
    in(x->l);
  cout << " " << x->key;
  if (x->r != NULL)
    in(x->r);
}

void pre(node *x) {
  cout << " " << x->key;
  if (x->l != NULL)
    pre(x->l);
  if (x->r != NULL)
    pre(x->r);
}

void print() {
  node *x = N;
  in(x);
  cout << endl;
  pre(x);
  cout << endl;
}

int main() {
  N = NULL;
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "insert") {
      int num;
      cin >> num;
      node *z = new node();
      z->key = num;
      z->l = z->r = z->p = NULL;
      tree_insert(z);
    }
    if (s == "find") {
      int num;
      cin >> num;
      tree_find(num);
    }
    if (s == "print") {
      print();
    }
  }
  return 0;
}
Problem p02287 (C++)

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

using namespace std;

typedef long long signed int ll;

ll a[251];
int h;

void rk(int n) {
  if (a[n * 2 + 1])
    cout << "right key = " << a[n * 2 + 1] << ", ";
}
void lk(int n) {
  if (a[n * 2])
    cout << "left key = " << a[n * 2] << ", ";
}
void pk(int n) {
  if (a[n / 2])
    cout << "parent key = " << a[n / 2] << ", ";
}

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

int main() {
  cin >> h;
  for (int i = 1; i <= h; i++)
    cin >> a[i];
  for (int i = 1; i <= h; i++)
    print(i);
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

typedef long long signed int ll;

ll a[510];
int h;

void rk(int n) {
  if (a[n * 2 + 1])
    cout << "right key = " << a[n * 2 + 1] << ", ";
}
void lk(int n) {
  if (a[n * 2])
    cout << "left key = " << a[n * 2] << ", ";
}
void pk(int n) {
  if (a[n / 2])
    cout << "parent key = " << a[n / 2] << ", ";
}

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

int main() {
  cin >> h;
  for (int i = 1; i <= h; i++)
    cin >> a[i];
  for (int i = 1; i <= h; i++)
    print(i);
  return 0;
}
Problem p02287 (C++)

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

Node A[251];

void heap(Node A[], 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;
    }
  }
}

int main() {
  int H = 0;
  cin >> H;
  for (int i = 1; i <= H; i++) {
    cin >> A[i].key;
    A[i].id = i;
  }
  heap(A, H);
  for (int i = 1; i <= H; i++) {
    cout << "node " << A[i].id << ": key = " << A[i].key << ", ";
    if (i == 1) {
      cout << "left key = " << A[i].left << ", right key = " << A[i].right
           << ", " << endl;
    } else {
      if (A[i].left == 0 && A[i].right != 0) {
        cout << "parent key = " << A[i].parent << ", right key = " << A[i].right
             << ", " << endl;
      } else if (A[i].left != 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", " << endl;
      } else if (A[i].left == 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", " << endl;
      } else {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", right key = " << A[i].right << ", " << endl;
      }
    }
  }
  return 0;
}


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

Node A[100000];

void heap(Node A[], 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;
    }
  }
}

int main() {
  int H = 0;
  cin >> H;
  for (int i = 1; i <= H; i++) {
    cin >> A[i].key;
    A[i].id = i;
  }
  heap(A, H);
  for (int i = 1; i <= H; i++) {
    cout << "node " << A[i].id << ": key = " << A[i].key << ", ";
    if (i == 1) {
      cout << "left key = " << A[i].left << ", right key = " << A[i].right
           << ", " << endl;
    } else {
      if (A[i].left == 0 && A[i].right != 0) {
        cout << "parent key = " << A[i].parent << ", right key = " << A[i].right
             << ", " << endl;
      } else if (A[i].left != 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", " << endl;
      } else if (A[i].left == 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", " << endl;
      } else {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", right key = " << A[i].right << ", " << endl;
      }
    }
  }
  return 0;
}
Problem p02288 (C++)

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

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

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

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

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

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

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

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


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;

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

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

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

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

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();
  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 (status: Runtime Error):
#include <iostream>
#include <stdio.h>

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

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

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

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

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


Fixed submission:
#include <iostream>
#include <stdio.h>

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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

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

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

using namespace std;

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

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