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

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string>

void max_heapify(int *A, int i, int H) {
  int l = 2 * i, r = 2 * i + 1;
  if (l > H && r > H)
    return;
  int maxid = i, x;
  if (l <= H)
    maxid = (A[l] > A[maxid] ? l : maxid);
  if (r <= H)
    maxid = (A[r] > A[maxid] ? r : maxid);
  if (maxid == i)
    return;
  x = A[maxid];
  A[maxid] = A[i];
  A[i] = x;
  max_heapify(A, maxid, H);
}

int main() {
  int A[201];
  int i, H, key;
  char cmd[8];
  scanf("%d", &H);
  for (i = 1; i <= H; i++)
    scanf("%d", &A[i]);
  H = 0;

  while (1) {
    scanf("%s", cmd);
    if (cmd[2] == 'd')
      break;             // end.
    if (cmd[2] == 't') { // extract.
      if (H == 0)
        continue;
      printf("%d\n", A[1]);
      A[1] = A[H];
      H--;
      if (H > 0)
        max_heapify(A, 1, H);
    } else {
      scanf("%d", &key); // H++;   // insert key.
      i = ++H;
      while (i > 1) {
        if (A[i / 2] > key)
          break;
        A[i] = A[i / 2];
        i /= 2;
      };
      A[i] = key;
    }
  };
  return 0;
}

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

void max_heapify(int *A, int i, int H) {
  int l = 2 * i, r = 2 * i + 1;
  if (l > H && r > H)
    return;
  int maxid = i, x;
  if (l <= H)
    maxid = (A[l] > A[maxid] ? l : maxid);
  if (r <= H)
    maxid = (A[r] > A[maxid] ? r : maxid);
  if (maxid == i)
    return;
  x = A[maxid];
  A[maxid] = A[i];
  A[i] = x;
  max_heapify(A, maxid, H);
}

int main() {
  int A[2000001];
  int i, H, key;
  char cmd[8];
  scanf("%d", &H);
  for (i = 1; i <= H; i++)
    scanf("%d", &A[i]);
  H = 0;

  while (1) {
    scanf("%s", cmd);
    if (cmd[2] == 'd')
      break;             // end.
    if (cmd[2] == 't') { // extract.
      if (H == 0)
        continue;
      printf("%d\n", A[1]);
      A[1] = A[H];
      H--;
      if (H > 0)
        max_heapify(A, 1, H);
    } else {
      scanf("%d", &key); // H++;   // insert key.
      i = ++H;
      while (i > 1) {
        if (A[i / 2] > key)
          break;
        A[i] = A[i / 2];
        i /= 2;
      };
      A[i] = key;
    }
  };
  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 2000000
#define INFTY (1 << 30)

int H, A[MAX + 1];

void maxHeap(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[large], A[i]);
    maxHeap(large);
  }
}

int extract() {
  int maxv;
  if (H < 1)
    return -INFTY;
  maxv = A[1];
  A[1] = A[H--];
  maxHeap(1);
  return maxv;
}
void increasekey(int i, int key) {
  if (key < A[i])
    return;
  A[i] = key;
  while (i > 1 && A[i / 2] < A[i]) {
    swap(A[i], A[i / 2]);
    i = i / 2;
  }
}

int insert(int key) {
  H++;
  A[H] = -INFTY;
  increasekey(H, key);
}

int main() {
  int key;
  char com[10];
  while (1) {
    cin >> com;
    if (com == "end")
      break;
    if (com == "insert") {
      cin >> key;
      insert(key);
    } else {
      cout << extract() << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 2000000
#define INFTY (1 << 30)

int H, A[MAX + 1];

void maxHeap(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[large], A[i]);
    maxHeap(large);
  }
}

int extract() {
  int maxv;
  if (H < 1)
    return -INFTY;
  maxv = A[1];
  A[1] = A[H--];
  maxHeap(1);
  return maxv;
}
void increasekey(int i, int key) {
  if (key < A[i])
    return;
  A[i] = key;
  while (i > 1 && A[i / 2] < A[i]) {
    swap(A[i], A[i / 2]);
    i = i / 2;
  }
}

int insert(int key) {
  H++;
  A[H] = -INFTY;
  increasekey(H, key);
}

int main() {
  int key;
  string com;
  while (1) {
    cin >> com;
    if (com == "end")
      break;
    if (com == "insert") {
      cin >> key;
      insert(key);
    } else {
      cout << extract() << endl;
    }
  }
  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
using namespace std;
int val, size_, heap[2097168];
char c[9];
inline void insert(int x) {
  int ptr = size_++;
  heap[ptr] = x;
  while (ptr > 1) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
}
inline int extract() {
  int ret = heap[1], ptr = 1;
  while ((ptr << 1) + 1 < size_) {
    ptr <<= 1;
    if (heap[ptr] > heap[ptr + 1])
      swap(heap[ptr >> 1], heap[ptr]);
    else
      swap(heap[ptr + 1], heap[ptr >> 1]), ptr++;
  }
  heap[ptr] = heap[--size_], heap[size_] = 0;
  while (ptr > 1) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
  return ret;
}
int main() {
  size_ = 1;
  while (scanf("%s", c) != 3) {
    if (c[0] == 'i')
      scanf("%d", &val), insert(val);
    else
      printf("%d\n", extract());
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
int val, size_, heap[2097168];
char c[9];
inline void insert(int x) {
  int ptr = size_++;
  heap[ptr] = x;
  while (ptr > 1) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
}
inline int extract() {
  int ret = heap[1], ptr = 1;
  while ((ptr << 1) + 1 < size_) {
    ptr <<= 1;
    if (heap[ptr] > heap[ptr + 1])
      swap(heap[ptr >> 1], heap[ptr]);
    else
      swap(heap[ptr + 1], heap[ptr >> 1]), ptr++;
  }
  heap[ptr] = heap[--size_], heap[size_] = 0;
  while (ptr > 1) {
    if (heap[ptr >> 1] < heap[ptr])
      swap(heap[ptr], heap[ptr >> 1]);
    ptr >>= 1;
  }
  return ret;
}
int main() {
  size_ = 1;
  while (scanf("%s", c), c[3]) {
    if (c[0] == 'i')
      scanf("%d", &val), insert(val);
    else
      printf("%d\n", extract());
  }
  return 0;
}
Problem p02289 (C++)

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

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

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

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

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

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

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

void insert(int k) {
  id++;
  int i = id;
  p[id] = k;
  for (;;) {
    if (i != 1 && p[parent(i)] <= p[i]) {
      q = p[parent(i)];
      p[parent(i)] = p[i];
      p[i] = q;
      i = parent(i);
    } else {
      break;
    }
  }
}

int extract() {
  int ret = p[1];
  p[1] = p[id];
  id--;
  maxHeapify(1);
  return (ret);
}

int main() {
  for (;;) {
    char s_[100];
    scanf("%s", s_);
    string s = s_;
    if (s == "end") {
      break;
    } else if (s == "insert") {
      int k;
      scanf("%d", &k);
      insert(k);
    } else if (s == "extract") {
      printf("%d\n", extract());
    }
  }
  return 0;
}

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

int p[2000000];
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));
    }
  }
}

void insert(int k) {
  id++;
  int i = id;
  p[id] = k;
  for (;;) {
    if (i != 1 && p[parent(i)] <= p[i]) {
      q = p[parent(i)];
      p[parent(i)] = p[i];
      p[i] = q;
      i = parent(i);
    } else {
      break;
    }
  }
}

int extract() {
  int ret = p[1];
  p[1] = p[id];
  id--;
  maxHeapify(1);
  return (ret);
}

int main() {
  for (;;) {
    char s_[100];
    scanf("%s", s_);
    string s = s_;
    if (s == "end") {
      break;
    } else if (s == "insert") {
      int k;
      scanf("%d", &k);
      insert(k);
    } else if (s == "extract") {
      printf("%d\n", extract());
    }
  }
  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_V 5000000
int heap[MAX_V]{0};
int heap_size = 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 i) { return heap_size > left(i); }
bool has_right(int i) { return heap_size > right(i); }

void max_heapify(int i) {
  int l, r, largest;
  l = left(i);
  r = right(i);
  if (has_left(i) && heap[l] > heap[i])
    largest = l;
  else
    largest = i;
  if (has_right(i) && heap[r] > heap[largest])
    largest = r;
  if (largest != i) {
    swap(heap[i], heap[largest]);
    max_heapify(largest);
  }
}
void heap_increase_key(int i, int key) {
  if (key < heap[i])
    return;
  heap[i] = key;
  while (i > 0 && heap[parent(i)] < heap[i]) {
    swap(heap[i], heap[parent(i)]);
    i = parent(i);
  }
}
void max_heap_insert(int key) {
  heap[heap_size] = 0x80000000;
  heap_size++;
  heap_increase_key(heap_size - 1, key);
}
int heap_extract_max() {
  if (heap_size < 1)
    return -1;
  int max = heap[0];
  heap[0] = heap[--heap_size];
  max_heapify(0);
  return max;
}
int main() {
  for (;;) {
    string str;
    cin >> str;
    if (str == "end")
      break;
    else if (str == "insert") {
      int key;
      cin >> key;
      max_heap_insert(key);
    } else if (str == "extract") {
      int item = heap_extract_max();
      cout << item << "\n";
    }
  }
}

Fixed submission:
#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_V 5000000
int heap[MAX_V]{0};
int heap_size = 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 i) { return heap_size > left(i); }
bool has_right(int i) { return heap_size > right(i); }

void max_heapify(int i) {
  int l, r, largest;
  l = left(i);
  r = right(i);
  if (has_left(i) && heap[l] > heap[i])
    largest = l;
  else
    largest = i;
  if (has_right(i) && heap[r] > heap[largest])
    largest = r;
  if (largest != i) {
    swap(heap[i], heap[largest]);
    max_heapify(largest);
  }
}
void heap_increase_key(int i, int key) {
  if (key < heap[i])
    return;
  heap[i] = key;
  while (i > 0 && heap[parent(i)] < heap[i]) {
    swap(heap[i], heap[parent(i)]);
    i = parent(i);
  }
}
void max_heap_insert(int key) {
  heap[heap_size] = 0x80000000;
  heap_size++;
  heap_increase_key(heap_size - 1, key);
}
int heap_extract_max() {
  if (heap_size < 1)
    return -1;
  int max = heap[0];
  heap[0] = heap[--heap_size];
  max_heapify(0);
  return max;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for (;;) {
    string str;
    cin >> str;
    if (str == "end")
      break;
    else if (str == "insert") {
      int key;
      cin >> key;
      max_heap_insert(key);
    } else if (str == "extract") {
      int item = heap_extract_max();
      cout << item << "\n";
    }
  }
}
Problem p02289 (C++)

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

using namespace std;

#define MAX 1000000

class PriorityQueue {
public:
  int size;
  int buffer[MAX + 1];

  PriorityQueue() { size = 0; }

  void insert(int x) {
    size++;
    buffer[size] = x;
    upHeap(size);
  }

  int get() {
    int v = buffer[1];
    buffer[1] = buffer[size];
    size--;
    downHeap(1);
    return v;
  }

private:
  void upHeap(int cursor) {
    int parent;

    while (1) {
      parent = cursor / 2;
      if (parent < 1)
        break;

      if (buffer[parent] < buffer[cursor]) {
        swap(buffer[parent], buffer[cursor]);
      } else
        break;

      cursor = parent;
    }
  }

  void downHeap(int cursor) {
    int child;

    while (1) {
      if (cursor > size / 2)
        break;

      child = cursor * 2;
      if (child < size && buffer[child] < buffer[child + 1]) {
        child++;
      }

      if (buffer[cursor] < buffer[child]) {
        swap(buffer[cursor], buffer[child]);
      } else
        break;

      cursor = child;
    }
  }
};

int main(void) {
  PriorityQueue PQ = PriorityQueue();
  int x;
  string command;

  while (1) {
    cin >> command;

    if (command == "insert") {
      cin >> x;
      PQ.insert(x);
    } else if (command == "extract") {
      cout << PQ.get() << endl;
    } else
      break;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

#define MAX 2000000

class PriorityQueue {
public:
  int size;
  int buffer[MAX + 1];

  PriorityQueue() { size = 0; }

  void insert(int x) {
    size++;
    buffer[size] = x;
    upHeap(size);
  }

  int get() {
    int v = buffer[1];
    buffer[1] = buffer[size];
    size--;
    downHeap(1);
    return v;
  }

private:
  void upHeap(int cursor) {
    int parent;

    while (1) {
      parent = cursor / 2;
      if (parent < 1)
        break;

      if (buffer[parent] < buffer[cursor]) {
        swap(buffer[parent], buffer[cursor]);
      } else
        break;

      cursor = parent;
    }
  }

  void downHeap(int cursor) {
    int child;

    while (1) {
      if (cursor > size / 2)
        break;

      child = cursor * 2;
      if (child < size && buffer[child] < buffer[child + 1]) {
        child++;
      }

      if (buffer[cursor] < buffer[child]) {
        swap(buffer[cursor], buffer[child]);
      } else
        break;

      cursor = child;
    }
  }
};

int main(void) {
  PriorityQueue PQ = PriorityQueue();
  int x;
  string command;

  while (1) {
    cin >> command;

    if (command == "insert") {
      cin >> x;
      PQ.insert(x);
    } else if (command == "extract") {
      cout << PQ.get() << endl;
    } else
      break;
  }

  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>

int main() {
  std::multiset<int> S;
  while (1) {
    std::string Command;
    std::cin >> Command;
    if (Command[0] == 'i') {
      int Value;
      std::cin >> Value;
      S.insert(Value);
    } else if (Command[1] == 'x') {
      std::set<int>::reverse_iterator It = S.rbegin();
      std::cout << *It << std::endl;
      S.erase(--(It.base()));
    } else {
      break;
    }
  }
  return 0;
}

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

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::multiset<int> S;
  while (1) {
    std::string Command;
    std::cin >> Command;
    if (Command[0] == 'i') {
      int Value;
      std::cin >> Value;
      S.insert(Value);
    } else if (Command[1] == 'x') {
      std::set<int>::reverse_iterator It = S.rbegin();
      std::cout << *It << std::endl;
      S.erase(--(It.base()));
    } else {
      break;
    }
  }
  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
#define INFTY (1 << 30)
#define MAX 500000

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

int maxOf3Node(int data[], int base) {
  int l = left(base);
  int r = right(base);
  int leargest;
  if (l <= N && data[l] > data[base])
    leargest = l;
  else
    leargest = base;
  if (r <= N && data[r] > data[leargest])
    leargest = r;
  return leargest;
}

void maxHeapify(int data[], int i) {
  int leargest = maxOf3Node(data, i);
  if (leargest != i) {
    swap(data[leargest], data[i]);
    maxHeapify(data, leargest);
  }
}

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

void insert(int data[], int key) {
  N++;
  data[N] = key;
  int i = N;
  while (i > 1 && data[parent(i)] < data[i]) {
    swap(data[parent(i)], data[i]);
    i = parent(i);
  }
}

int extract(int data[]) {
  int max = data[1];
  data[1] = data[N];
  N--;
  maxHeapify(data, 1);
  return max;
}

void trace(int data[]) {
  for (int i = 1; i <= N; i++)
    cout << " " << data[i];
  cout << endl;
}

int main() {
  int data[MAX + 1];
  int x;
  char com[20];

  while (true) {
    scanf("%s", com);
    if (com[0] == 'i') { // insert
      scanf("%d", &x);
      insert(data, x);
    } else if (com[1] == 'x') { // extract
      int max = extract(data);
      cout << max << endl;
    } else if (com[0] == 'e') { // end
      break;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
#define INFTY (1 << 30)
#define MAX 2000000

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

int maxOf3Node(int data[], int base) {
  int l = left(base);
  int r = right(base);
  int leargest;
  if (l <= N && data[l] > data[base])
    leargest = l;
  else
    leargest = base;
  if (r <= N && data[r] > data[leargest])
    leargest = r;
  return leargest;
}

void maxHeapify(int data[], int i) {
  int leargest = maxOf3Node(data, i);
  if (leargest != i) {
    swap(data[leargest], data[i]);
    maxHeapify(data, leargest);
  }
}

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

void insert(int data[], int key) {
  N++;
  data[N] = key;
  int i = N;
  while (i > 1 && data[parent(i)] < data[i]) {
    swap(data[parent(i)], data[i]);
    i = parent(i);
  }
}

int extract(int data[]) {
  int max = data[1];
  data[1] = data[N];
  N--;
  maxHeapify(data, 1);
  return max;
}

void trace(int data[]) {
  for (int i = 1; i <= N; i++)
    cout << " " << data[i];
  cout << endl;
}

int main() {
  int data[MAX + 1];
  int x;
  char com[20];

  while (true) {
    scanf("%s", com);
    if (com[0] == 'i') { // insert
      scanf("%d", &x);
      insert(data, x);
    } else if (com[1] == 'x') { // extract
      int max = extract(data);
      cout << max << endl;
    } else if (com[0] == 'e') { // end
      break;
    }
  }

  return 0;
}
Problem p02289 (C++)

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

int main() {
  priority_queue<int> PQ;
  char com[10];
  int key;

  while (1) {
    if (com[0] == 'e' && com[1] == 'n')
      break;
    else if (com[0] == 'i') {
      scanf("%d", &key);
      PQ.push(key);
    } else {
      printf("%d\n", PQ.top());
      PQ.pop();
    }
  }

  return 0;
}


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

int main() {
  priority_queue<int> PQ;
  char com[10];
  int key;

  while (1) {
    scanf("%s", com);
    if (com[0] == 'e' && com[1] == 'n')
      break;
    else if (com[0] == 'i') {
      scanf("%d", &key);
      PQ.push(key);
    } else {
      printf("%d\n", PQ.top());
      PQ.pop();
    }
  }

  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int num = 2000000;
int a[num];
int h;
inline int pa(int i) { return i / 2; }
void maxHeapify(int i) {
  int l = i * 2;
  int r = i * 2 + 1;
  int lg;
  if (l <= h && a[i] < a[l])
    lg = l;
  else
    lg = i;
  if (r <= h && a[lg] < a[r])
    lg = r;
  if (lg != i) {
    swap(a[i], a[lg]);
    maxHeapify(lg);
  }
}
inline void insert(int k) {
  h++;
  a[h] = k;
  int i = h;
  while (i > 1) {
    if (a[h] > a[pa(h)])
      swap(a[h], a[pa(h)]);
    i = pa(h);
  }
}
inline void extract() {
  printf("%d", a[1]);
  printf("\n");
  a[1] = a[h];
  h--;
  maxHeapify(1);
}
int main() {
  char com[10];
  int key;
  for (int i = 1;; i++) {
    scanf("%s", &com);
    if (com[0] == 'i') {
      scanf("%d", &key);
      insert(key);
    } else if (com[1] == 'x')
      extract();
    else
      break;
  }
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int num = 2000000;
int a[num];
int h;
inline int pa(int i) { return i / 2; }
void maxHeapify(int i) {
  int l = i * 2;
  int r = i * 2 + 1;
  int lg;
  if (l <= h && a[i] < a[l])
    lg = l;
  else
    lg = i;
  if (r <= h && a[lg] < a[r])
    lg = r;
  if (lg != i) {
    swap(a[i], a[lg]);
    maxHeapify(lg);
  }
}
inline void insert(int k) {
  h++;
  a[h] = k;
  int i = h;
  while (i > 1 && a[i] > a[pa(i)]) {
    swap(a[i], a[pa(i)]);
    i = pa(i);
  }
}
inline void extract() {
  printf("%d", a[1]);
  printf("\n");
  a[1] = a[h];
  h--;
  maxHeapify(1);
}
int main() {
  char com[10];
  int key;
  for (int i = 1;; i++) {
    scanf("%s", &com);
    if (com[0] == 'i') {
      scanf("%d", &key);
      insert(key);
    } else if (com[1] == 'x')
      extract();
    else
      break;
  }
  return 0;
}
Problem p02289 (C++)

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

typedef long long ll;

void maxHeap(vector<ll> &h, int i) {
  int left = i * 2 + 1;
  int right = i * 2 + 2;
  int largest = i;
  if (left < h.size() && h[left] > h[largest])
    largest = left;
  if (right < h.size() && h[right] > h[largest])
    largest = right;

  if (largest != i) {
    swap(h[largest], h[i]);
    maxHeap(h, largest);
  }
}

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

void input(vector<ll> &heap, int i) {
  int sub = i + 1;
  if (sub <= 1)
    return;
  if (heap[sub - 1] > heap[sub / 2 - 1]) {
    swap(heap[sub - 1], heap[sub / 2 - 1]);
    input(heap, sub / 2 - 1);
  }
}

void printheap(const vector<ll> &h) {
  cout << "size: " << h.size() << endl;
  ;
  for (int i = 0; i < h.size(); i++)
    cout << " " << h[i];
  cout << "\n";
}

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

  vector<ll> heap;
  string str;
  ll num;
  cin >> str;
  while (str != "end") {
    if (str == "insert") {
      cin >> num;
      heap.push_back(num);
      input(heap, heap.size() - 1);
    } else if (str == "print") {
      printheap(heap);
    } else {
      if (!heap.empty()) {
        cout << heap.front() << endl;
        heap[0] = heap.back();
        heap.pop_back();
        buildMaxHeap(heap);
      }
    }
    cin >> str;
  }
  return 0;
}


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

typedef long long ll;

void maxHeap(vector<ll> &h, int i) {
  int left = i * 2 + 1;
  int right = i * 2 + 2;
  int largest = i;
  if (left < h.size() && h[left] > h[largest])
    largest = left;
  if (right < h.size() && h[right] > h[largest])
    largest = right;

  if (largest != i) {
    swap(h[largest], h[i]);
    maxHeap(h, largest);
  }
}

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

void input(vector<ll> &heap, int i) {
  int sub = i + 1;
  if (sub <= 1)
    return;
  if (heap[sub - 1] > heap[sub / 2 - 1]) {
    swap(heap[sub - 1], heap[sub / 2 - 1]);
    input(heap, sub / 2 - 1);
  }
}

void printheap(const vector<ll> &h) {
  cout << "size: " << h.size() << endl;
  ;
  for (int i = 0; i < h.size(); i++)
    cout << " " << h[i];
  cout << "\n";
}

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

  vector<ll> heap;
  string str;
  ll num;
  cin >> str;
  while (str != "end") {
    if (str == "insert") {
      cin >> num;
      heap.push_back(num);
      input(heap, heap.size() - 1);
    } else if (str == "print") {
      printheap(heap);
    } else {
      if (!heap.empty()) {
        cout << heap.front() << endl;
        heap[0] = heap.back();
        heap.pop_back();
        maxHeap(heap, 0);
      }
    }
    cin >> str;
  }
  return 0;
}
Problem p02289 (C++)

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

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 n) {
  int l_idx, r_idx, temp;
  l_idx = left(i + 1) - 1;
  r_idx = right(i + 1) - 1;
  temp = i;

  if (r_idx < n) {
    if (A[r_idx] > A[i]) {
      temp = r_idx;
    }
  }
  if (l_idx < n) {
    if (A[l_idx] > A[temp]) {
      temp = l_idx;
    }
  }
  if (temp != i) {
    std::swap(A[i], A[temp]);
    maxHeapify(A, temp, n);
  }
}

void compareParent(int *A, int i, int n) {
  int p;
  if (i != 0) {
    p = parent(i + 1) - 1;
    if (A[i] > A[p]) {
      std::swap(A[i], A[p]);
      compareParent(A, p, n);
    }
  }
}

int main(void) {
  int n;
  int A[200000];
  n = 0;
  char op[10];
  while (1) {
    scanf("%s", op);
    if (op[2] == 'd') {
      break;
    } else if (op[2] == 's') {
      scanf("%d", &A[n]);
      n++;
      compareParent(A, n - 1, n);
    } else {
      printf("%d\n", A[0]);
      std::swap(A[n - 1], A[0]);
      n--;
      maxHeapify(A, 0, n);
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>

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 n) {
  int l_idx, r_idx, temp;
  l_idx = left(i + 1) - 1;
  r_idx = right(i + 1) - 1;
  temp = i;

  if (r_idx < n) {
    if (A[r_idx] > A[i]) {
      temp = r_idx;
    }
  }
  if (l_idx < n) {
    if (A[l_idx] > A[temp]) {
      temp = l_idx;
    }
  }
  if (temp != i) {
    std::swap(A[i], A[temp]);
    maxHeapify(A, temp, n);
  }
}

void compareParent(int *A, int i, int n) {
  int p;
  if (i != 0) {
    p = parent(i + 1) - 1;
    if (A[i] > A[p]) {
      std::swap(A[i], A[p]);
      compareParent(A, p, n);
    }
  }
}

int main(void) {
  int n;
  int A[2000000];
  n = 0;
  char op[10];
  while (1) {
    scanf("%s", op);
    if (op[2] == 'd') {
      break;
    } else if (op[2] == 's') {
      scanf("%d", &A[n]);
      n++;
      compareParent(A, n - 1, n);
    } else {
      printf("%d\n", A[0]);
      std::swap(A[n - 1], A[0]);
      n--;
      maxHeapify(A, 0, n);
    }
  }
  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
void Insert(int);
void Increase(int, int);
int Extract(void);
void maxHeapify(int i);
int H[1000];
int n = 0;
int main() {
  int key, n = 0;
  char ch[10];

  while (1) {
    cin >> ch;
    if (ch[0] == 'i') {
      cin >> key;
      Insert(key);
    } else if (ch[0] == 'e' && ch[1] == 'x') {
      cout << Extract() << endl;
    } else {
      break;
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << H[i] << endl;
  }
  return 0;
}

void Insert(int key) {
  n += 1;
  H[n] = -1;
  Increase(n, key);
}
void Increase(int i, int key) {
  int tmp;
  H[i] = key;
  while (i > 1 && H[i / 2] < H[i]) {
    tmp = H[i];
    H[i] = H[i / 2];
    H[i / 2] = tmp;
    i = i / 2;
  }
}

int Extract() {
  int max = H[1];
  H[1] = H[n];
  n--;
  maxHeapify(1);
  return max;
}
void maxHeapify(int i) {
  int r, l, largest, tmp;
  l = i * 2;
  r = i * 2 + 1;

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

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

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

    maxHeapify(largest);
  }
}

Fixed submission:
#include <iostream>
using namespace std;
void Insert(int);
void Increase(int, int);
int Extract(void);
void maxHeapify(int i);
int H[1000000];
int n = 0;
int main() {
  int key, n = 0;
  char ch[10];

  while (1) {
    cin >> ch;
    if (ch[0] == 'i') {
      cin >> key;
      Insert(key);
    } else if (ch[0] == 'e' && ch[1] == 'x') {
      cout << Extract() << endl;
    } else {
      break;
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << H[i] << endl;
  }
  return 0;
}

void Insert(int key) {
  n += 1;
  H[n] = -1;
  Increase(n, key);
}
void Increase(int i, int key) {
  int tmp;
  H[i] = key;
  while (i > 1 && H[i / 2] < H[i]) {
    tmp = H[i];
    H[i] = H[i / 2];
    H[i / 2] = tmp;
    i = i / 2;
  }
}

int Extract() {
  int max = H[1];
  H[1] = H[n];
  n--;
  maxHeapify(1);
  return max;
}
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 p02289 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;
int N;
int H[2000001];

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

void Maximize(int i) {
  int l = left(i);
  int r = right(i);

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

  if (largest != i) {
    swap(H[largest], H[i]);
    Maximize(largest);
  }
}

void Minimize(int i) {
  int p = parent(i);

  if (p > 0 && H[p] < H[i]) {
    swap(H[p], H[i]);
    Minimize(p);
  }
}

void extract() {
  if (N == 0)
    return;

  cout << H[1] << endl;
  H[1] = H[N];
  N--;
  Maximize(1);
}

void insert(int x) {
  N++;
  H[N] = x;
  Minimize(N);
}

int main() {
  int command;
  N = 0;

  while (true) {
    char com[16];
    scanf("%d", com);
    if (com[2] == 'd')
      break;

    if (com[0] == 'i') {
      int num;
      cin >> num;
      insert(num);
    } else {
      extract();
    }
  }

  return 0;
}

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

using namespace std;
int N;
int H[2000001];

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

void Maximize(int i) {
  int l = left(i);
  int r = right(i);

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

  if (largest != i) {
    swap(H[largest], H[i]);
    Maximize(largest);
  }
}

void Minimize(int i) {
  int p = parent(i);

  if (p > 0 && H[p] < H[i]) {
    swap(H[p], H[i]);
    Minimize(p);
  }
}

void extract() {
  if (N == 0)
    return;

  cout << H[1] << endl;
  H[1] = H[N];
  N--;
  Maximize(1);
}

void insert(int x) {
  N++;
  H[N] = x;
  Minimize(N);
}

int main() {
  int command;
  N = 0;

  while (true) {
    char com[16];
    scanf("%s", com);
    if (com[2] == 'd')
      break;

    if (com[0] == 'i') {
      int num;
      cin >> num;
      insert(num);
    } else {
      extract();
    }
  }

  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX = 2000000;
int S[MAX + 1];
int H = 0;

void maxHeapify(int A[], int n, int i) {

  int largest = i;
  int l = i * 2;
  int r = i * 2 + 1;

  if (l <= n && A[l] > A[i])
    largest = l;
  if (r <= n && A[r] > A[largest])
    largest = r;

  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(A, n, largest);
  }
}
void buildMaxHeap(int A[], int n) {
  for (int i = n / 2; i >= 1; --i)
    maxHeapify(A, n, i);
}
void insert(int S[], int k) {
  ++H;
  S[H] = k;

  int i = H;
  while (i > 1 && S[i / 2] < S[i]) {
    swap(S[i / 2], S[i]);
    i = i / 2;
  }
}

int extract(int S[]) {
  int ret = S[1];
  swap(S[1], S[H]);
  --H;
  buildMaxHeap(S, H);

  return ret;
}

int main() {

  while (1) {
    char order[7];
    scanf("%s", order);

    int k;
    if (strcmp(order, "insert") == 0) {
      scanf("%d", &k);
      insert(S, k);
    } else if (strcmp(order, "extract") == 0) {
      cout << extract(S) << endl;
    } else {
      break;
    }
  }
  return 0;
}

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

using namespace std;

const int MAX = 2000000;
int S[MAX + 1];
int H = 0;

void maxHeapify(int A[], int n, int i) {

  int largest = i;
  int l = i * 2;
  int r = i * 2 + 1;

  if (l <= n && A[l] > A[i])
    largest = l;
  if (r <= n && A[r] > A[largest])
    largest = r;

  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(A, n, largest);
  }
}
void buildMaxHeap(int A[], int n) {
  for (int i = n / 2; i >= 1; --i)
    maxHeapify(A, n, i);
}
void insert(int S[], int k) {
  ++H;
  S[H] = k;

  int i = H;
  while (i > 1 && S[i / 2] < S[i]) {
    swap(S[i / 2], S[i]);
    i = i / 2;
  }
}

int extract(int S[]) {
  int ret = S[1];
  swap(S[1], S[H]);
  --H;
  maxHeapify(S, H, 1);

  return ret;
}

int main() {

  while (1) {
    char order[7];
    scanf("%s", order);

    int k;
    if (strcmp(order, "insert") == 0) {
      scanf("%d", &k);
      insert(S, k);
    } else if (strcmp(order, "extract") == 0) {
      cout << extract(S) << endl;
    } else {
      break;
    }
  }
  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#define MAX_NODES 2000000
#define INFTY 2000000010

using namespace std;
// think easy make easy
// think stupid

int n = 0; // num of nodes

// iを根とするMaxヒープをつくる関数.初期状態はiを根とする二分木
void maxHeapify(int A[], int i) { // i is index of root
  int l, r;
  int largest;
  int tmp;

  l = 2 * i;
  r = l + 1;

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

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

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

void buildMaxHeap(int A[]) { // AをMaxヒープにする関数
  for (int i = n / 2; i > 0;
       i--) { // 1~n/2が左右の子少なくとも一方が存在する範囲
    maxHeapify(A, i);
  }
}

void heapIncreasingKey(int A[]) {
  int tmp;
  int tmp_n;
  for (int i = n; i > 1; i = i / 2) {
    if (A[i / 2] < A[i]) {
      tmp = A[i];
      A[i] = A[i / 2];
      A[i / 2] = tmp;
    } else
      break;
  }
}

void insert(int A[], int num) {
  n++;
  A[n] = num;
  heapIncreasingKey(A);
}

void extract(int A[]) {
  cout << A[1] << endl;
  n--;
  A[1] = A[n];
  n--;
  maxHeapify(A, 1);
}

void swap() {}
int main() {
  // 1オリジン(添え字が1からはじまる)のためナル文字分の+1に加えさらに+1する
  int H[MAX_NODES + 1 + 1];
  int num;
  char type[100];

  while (true) {
    scanf("%s", &type);

    if (type[0] == 'i') {
      scanf("%d", &num);
      insert(H, num);
    } else if (type[1] == 'x') {
      extract(H);
    } else
      break;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#define MAX_NODES 2000000
#define INFTY 2000000010

using namespace std;
// think easy make easy
// think stupid

int n = 0; // num of nodes

// iを根とするMaxヒープをつくる関数.初期状態はiを根とする二分木
void maxHeapify(int A[], int i) { // i is index of root
  int l, r;
  int largest;
  int tmp;

  l = 2 * i;
  r = l + 1;

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

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

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

void buildMaxHeap(int A[]) { // AをMaxヒープにする関数
  for (int i = n / 2; i > 0;
       i--) { // 1~n/2が左右の子少なくとも一方が存在する範囲
    maxHeapify(A, i);
  }
}

void heapIncreasingKey(int A[]) {
  int tmp;
  int tmp_n;
  for (int i = n; i > 1; i = i / 2) {
    if (A[i / 2] < A[i]) {
      tmp = A[i];
      A[i] = A[i / 2];
      A[i / 2] = tmp;
    } else
      break;
  }
}

void insert(int A[], int num) {
  n++;
  A[n] = num;
  heapIncreasingKey(A);
}

void extract(int A[]) {
  cout << A[1] << endl;
  A[1] = A[n];
  n--;
  maxHeapify(A, 1);
}

void swap() {}
int main() {
  // 1オリジン(添え字が1からはじまる)のためナル文字分の+1に加えさらに+1する
  int H[MAX_NODES + 1 + 1];
  int num;
  char type[100];

  while (true) {
    scanf("%s", &type);

    if (type[0] == 'i') {
      scanf("%d", &num);
      insert(H, num);
    } else if (type[1] == 'x') {
      extract(H);
    } else
      break;
  }

  return 0;
}
Problem p02289 (C++)

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

int n;

int parent(int i);
int left(int i);
int right(int i);
void maxHeap(int node[], int i);
void buildMaxHeap(int node[]);
void insert(int node[], int k);
int extractMax(int node[]);

int main() {
  int k, node[MAX];
  char com[10];

  n = 0;

  while (1) {
    cin >> com;
    if (com[2] == 'd')
      break;
    else if (com[0] == 'e')
      cout << extractMax(node) << endl;
    else {
      cin >> k;
      insert(node, k);
    }
  }
  return 0;
}

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

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

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

void maxHeap(int node[], int i) {
  int r, l, largest;

  if (left(i) <= n)
    l = left(i);
  else
    l = i;
  if (right(i) <= n)
    r = right(i);
  else
    r = i;

  if (node[l] < node[r])
    largest = r;
  else
    largest = l;

  if (node[largest] < node[i])
    largest = i;

  if (largest != i) {
    swap(node[i], node[largest]);
    maxHeap(node, largest);
  }
}

void buildMaxHeap(int node[]) {
  int i;
  for (i = n / 2; i > 0; i--)
    maxHeap(node, i);
}

void insert(int node[], int k) {
  int i;
  node[++n] = k;
  i = n;
  while (i > 1 && node[i] > node[i / 2]) {
    swap(node[i], node[i / 2]);
    i /= 2;
  }
}

int extractMax(int node[]) {
  int max;
  max = node[1];
  node[1] = node[n--];
  maxHeap(node, 1);
  return max;
}

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

int n;

int parent(int i);
int left(int i);
int right(int i);
void maxHeap(int node[], int i);
void buildMaxHeap(int node[]);
void insert(int node[], int k);
int extractMax(int node[]);

int main() {
  int k, node[MAX];
  char com[10];

  n = 0;

  while (1) {
    cin >> com;
    if (com[2] == 'd')
      break;
    else if (com[0] == 'e')
      cout << extractMax(node) << endl;
    else {
      cin >> k;
      insert(node, k);
    }
  }
  return 0;
}

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

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

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

void maxHeap(int node[], int i) {
  int r, l, largest;

  if (left(i) <= n)
    l = left(i);
  else
    l = i;
  if (right(i) <= n)
    r = right(i);
  else
    r = i;

  if (node[l] < node[r])
    largest = r;
  else
    largest = l;

  if (node[largest] < node[i])
    largest = i;

  if (largest != i) {
    swap(node[i], node[largest]);
    maxHeap(node, largest);
  }
}

void buildMaxHeap(int node[]) {
  int i;
  for (i = n / 2; i > 0; i--)
    maxHeap(node, i);
}

void insert(int node[], int k) {
  int i;
  node[++n] = k;
  i = n;
  while (i > 1 && node[i] > node[i / 2]) {
    swap(node[i], node[i / 2]);
    i /= 2;
  }
}

int extractMax(int node[]) {
  int max;
  max = node[1];
  node[1] = node[n--];
  maxHeap(node, 1);
  return max;
}
Problem p02289 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <limits.h>
#include <string>
using namespace std;
long int A[500000];

int n;

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

void heapIncreaseKey(int i, int key) {
  if (key < A[i])
    return;
  A[i] = key;
  while (i > 1 && A[parent(i)] < A[i]) {
    swap(A[i], A[parent(i)]);
    i = parent(i);
  }
}

void maxHeapInsert(int key) {
  n++;
  A[n] = INT_MIN;
  heapIncreaseKey(n, key);
}

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

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

int main() {
  int id;
  string cmd;
  A[0] = 0;
  do {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> id;
      maxHeapInsert(id);
    }
    if (cmd == "extract") {
      cout << heapExtractMax() << endl;
      ;
    }
  } while (cmd != "end");

  return 0;
}

Fixed submission:
#include <iostream>
#include <limits.h>
#include <string>
using namespace std;
long int A[2000000];

int n;

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

void heapIncreaseKey(int i, int key) {
  if (key < A[i])
    return;
  A[i] = key;
  while (i > 1 && A[parent(i)] < A[i]) {
    swap(A[i], A[parent(i)]);
    i = parent(i);
  }
}

void maxHeapInsert(int key) {
  n++;
  A[n] = INT_MIN;
  heapIncreaseKey(n, key);
}

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

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

int main() {
  int id;
  string cmd;
  A[0] = 0;
  do {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> id;
      maxHeapInsert(id);
    }
    if (cmd == "extract") {
      cout << heapExtractMax() << endl;
      ;
    }
  } while (cmd != "end");

  return 0;
}
Problem p02289 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

int main() {
  priority_queue<int> PQ;
  while (true) {
    string command;
    cin >> command;
    if (command[0] == 'i') {
      int key;
      scanf("%d", &key);
      PQ.push(key);
    } else if (command[1] == 'x') {
      printf("%d\n", PQ.top());
      PQ.pop();
    } else {
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

int main() {
  priority_queue<int> PQ;
  while (true) {
    char command[10];
    cin >> command;
    if (command[0] == 'i') {
      int key;
      scanf("%d", &key);
      PQ.push(key);
    } else if (command[1] == 'x') {
      printf("%d\n", PQ.top());
      PQ.pop();
    } else {
      break;
    }
  }
  return 0;
}
Problem p02289 (C++)

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

#define NUM 100000
#define NIL -1
int A[NUM];
int heap_n = 0;

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

void upHeapify(int num) {
  if (num == 1) {
    return;
  }

  if (A[num] > A[num / 2]) {
    int tmp = A[num];
    A[num] = A[num / 2];
    A[num / 2] = tmp;
    upHeapify(num / 2);
  }
}

void downHeapify(int num) {
  int m = max(A[num], max(A[num * 2], A[num * 2 + 1]));

  if (m == A[num * 2]) {
    swap(A[num * 2], A[num]);
    downHeapify(num * 2);
    return;
  }
  if (m == A[num * 2 + 1]) {
    swap(A[num * 2 + 1], A[num]);
    downHeapify(num * 2 + 1);
    return;
  }
}

void insert_(int num) {
  A[heap_n + 1] = num;
  heap_n++;
  upHeapify(heap_n);
}

void pop_() {
  A[1] = A[heap_n];
  A[heap_n] = NIL;
  heap_n--;
  downHeapify(1);
}

int main(void) {
  init();
  char buf[100];
  int num;

  while (1) {
    scanf("%s", buf);
    if (!strcmp(buf, "end")) {
      break;
    }

    if (!strcmp(buf, "insert")) {
      scanf("%d", &num);
      insert_(num);
    }

    if (!strcmp(buf, "extract")) {
      printf("%d\n", A[1]);
      pop_();
    }

    if (!strcmp(buf, "print")) {
      for (int i = 1; i <= heap_n; i++) {
        printf("%d ", A[i]);
      }
    }
  }
}


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

#define NUM 4000000
#define NIL -1
int A[NUM];
int heap_n = 0;

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

void upHeapify(int num) {
  if (num == 1) {
    return;
  }

  if (A[num] > A[num / 2]) {
    int tmp = A[num];
    A[num] = A[num / 2];
    A[num / 2] = tmp;
    upHeapify(num / 2);
  }
}

void downHeapify(int num) {
  int m = max(A[num], max(A[num * 2], A[num * 2 + 1]));

  if (m == A[num * 2]) {
    swap(A[num * 2], A[num]);
    downHeapify(num * 2);
    return;
  }
  if (m == A[num * 2 + 1]) {
    swap(A[num * 2 + 1], A[num]);
    downHeapify(num * 2 + 1);
    return;
  }
}

void insert_(int num) {
  A[heap_n + 1] = num;
  heap_n++;
  upHeapify(heap_n);
}

void pop_() {
  A[1] = A[heap_n];
  A[heap_n] = NIL;
  heap_n--;
  downHeapify(1);
}

int main(void) {
  init();
  char buf[100];
  int num;

  while (1) {
    scanf("%s", buf);
    if (!strcmp(buf, "end")) {
      break;
    }

    if (!strcmp(buf, "insert")) {
      scanf("%d", &num);
      insert_(num);
    }

    if (!strcmp(buf, "extract")) {
      printf("%d\n", A[1]);
      pop_();
    }

    if (!strcmp(buf, "print")) {
      for (int i = 1; i <= heap_n; i++) {
        printf("%d ", A[i]);
      }
    }
  }
}