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

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

Error: 0

Fixed submission:

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void maxHeap(int *keys, int size, int cur) {
  if (cur * 2 > size) {
    // noop
  } else if (cur * 2 + 1 > size && keys[cur * 2] > keys[cur]) {
    swap(keys[cur * 2], keys[cur]);
  } else {
    int l = keys[cur * 2];
    int r = keys[1 + cur * 2];
    if (l > r && l > keys[cur]) {
      swap(keys[cur * 2], keys[cur]);
      maxHeap(keys, size, cur * 2);
    } else if (r > keys[cur]) {
      swap(keys[1 + cur * 2], keys[cur]);
      maxHeap(keys, size, 1 + cur * 2);
    }
  }
}
void fixHeap(int *keys, int cur) {
  if (cur / 2 >= 1 && keys[cur] > keys[cur / 2]) {
    swap(keys[cur], keys[cur / 2]);
    fixHeap(keys, cur / 2);
  }
}

int main() {
  int node = 0;
  int keys[2000000];
  for (; 1;) {
    char *com = new char[16];
    scanf("%s", com);

    if (com[0] == 'i') {
      scanf("%d", &keys[++node]);
      fixHeap(keys, node);
    } else if (com[1] == 'x') {
      printf("%d\\n", keys[1]);
      keys[1] = keys[node--];
      maxHeap(keys, node, 1);
    } else
      break;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void maxHeap(int *keys, int size, int cur) {
  if (cur * 2 > size) {
    // noop
  } else if (cur * 2 + 1 > size && keys[cur * 2] > keys[cur]) {
    swap(keys[cur * 2], keys[cur]);
  } else {
    int l = keys[cur * 2];
    int r = keys[1 + cur * 2];
    if (l > r && l > keys[cur]) {
      swap(keys[cur * 2], keys[cur]);
      maxHeap(keys, size, cur * 2);
    } else if (r > keys[cur]) {
      swap(keys[1 + cur * 2], keys[cur]);
      maxHeap(keys, size, 1 + cur * 2);
    }
  }
}
void fixHeap(int *keys, int cur) {
  if (cur / 2 >= 1 && keys[cur] > keys[cur / 2]) {
    swap(keys[cur], keys[cur / 2]);
    fixHeap(keys, cur / 2);
  }
}

int main() {
  int node = 0;
  int keys[2000000];
  for (; 1;) {
    string com;
    cin >> com;

    if (com[0] == 'i') {
      cin >> keys[++node];
      fixHeap(keys, node);
    } else if (com[1] == 'x') {
      cout << keys[1] << endl;
      keys[1] = keys[node--];
      maxHeap(keys, node, 1);
    } else
      break;
  }

  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

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


#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define INF 1000000000007
#define ESP 1e-11
using namespace std;
typedef long long int ll;

vector<ll> a(2000005, 0);

ll flag = 1;

void push(ll x) {
  ll i = flag++;
  while (i > 1) {
    ll p = i / 2;
    if (a[p] >= x)
      break;
    a[i] = a[p];
    i = p;
  }
  a[i] = x;
}

ll pop() {
  ll ans = a[1];
  ll hoge = a[--flag];
  ll i = 1;
  while (i * 2 <= flag) {
    ll l = 2 * i;
    ll r = 2 * i + 1;
    if (l < flag and a[l] < a[r]) {
      l = r;
    }
    if (a[l] <= hoge)
      break;
    a[i] = a[l];
    i = l;
  }
  a[i] = hoge;
  return ans;
}

int main() {
  string s;
  while (cin >> s) {
    if (s == "end")
      break;
    if (s == "extract") {
      cout << pop() << endl;
    } else {
      ll n;
      cin >> n;
      push(n);
    }
  }
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define INF 1000000000007
#define ESP 1e-11
using namespace std;
typedef long long int ll;

vector<ll> a;

void heapify(ll i) {
  if (i >= a.size() / 2)
    return;
  ll l = 0, r = 0, large = a[i], flag = -1;
  if (2 * i + 1 < a.size())
    l = a[2 * i + 1];
  if (2 * (i + 1) < a.size())
    r = a[2 * (i + 1)];
  if (large < l) {
    a[i] = l;
    a[2 * i + 1] = large;
    large = l;
    flag = 2 * i + 1;
  }
  if (large < r) {
    a[i] = r;
    a[2 * (i + 1)] = large;
    large = r;
    flag = 2 * (i + 1);
  }
  if (large == a[i]) {
    return;
  }
  return heapify(flag);
}

void heap_max(ll size) {
  for (ll i = size / 2 - 1; i >= 0; i--) {
    heapify(i);
  }
  return;
}

int main() {
  while (1) {
    string s;
    cin >> s;
    if (s == "end")
      break;
    if (s == "extract") {
      heap_max(a.size());
      printf("%lld\\n", a[0]);
      a[0] = -1;
      continue;
    }
    if (s == "insert") {
      ll n;
      scanf("%lld", &n);
      a.push_back(n);
    }
  }
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 2000005
#define INF 2000000010
int n = 0;
int a[N];
void swap(int *a, int *b) {
  int t;
  t = *a;
  *a = *b;
  *b = t;
}
void maxHeapify(int i) {
  int l = 2 * i;
  int r = 2 * i + 1;
  int maxi = i;
  if (l <= n && a[l] > a[maxi]) {
    maxi = l;
  }
  if (r <= n && a[r] > a[maxi]) {
    maxi = r;
  }
  if (maxi != i) {
    swap(&a[i], &a[maxi]);
    maxHeapify(maxi);
  }
}
void insert(int key) {
  a[++n] = key;
  int temp = n;
  while (temp > 1 && a[temp / 2] < key) {
    swap(&a[temp], &a[temp / 2]);
    temp /= 2;
  }
}
int extract() {
  if (n < 1)
    return 0;
  int res = a[1];
  a[1] = a[n--];
  maxHeapify(1);
  return res;
}
int main() {
  char s[10];
  int k;
  while (scanf("%s", s), strcmp(s, "end")) {
    if (s[0] == 'i') {
      scanf("%d", &k);
      insert(k);
    } else if (s[0] == 'e') {
      int p = extract();
      printf("%d\\n", p);
    }
  }
  return 0;
}


Error: -11

Fixed submission:

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

#define MAX 2000005
#define INFITY (1 << 30)

int H, A[MAX];

void maxheap(int i) {
  int l = 2 * i;
  int r = 2 * i + 1;
  int largest;
  if (l <= H && A[l] > A[i])
    largest = l;
  else
    largest = i;
  if (r <= H && A[r] > A[largest])
    largest = r;

  if (largest != i) {
    swap(A[i], A[largest]);
    maxheap(largest);
  }
}

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 / 2], A[i]);
    i = i / 2;
  }
}
void insert(int key) {
  H++;
  A[H] = -INFITY;
  increasekey(H, key);
}
int extract() {
  int maxv;
  if (H < 1)
    return -INFITY;
  maxv = A[1];
  A[1] = A[H];
  H--;
  maxheap(1);
  return maxv;
}

int main() {
  int key;
  char com[10];
  while (1) {
    scanf("%s", com);
    if (com[0] == 'e' && com[1] == 'n')
      break;
    if (com[0] == 'i') {
      scanf("%d", key);
      insert(key);
    } else {
      cout << extract() << endl;
    }
  }
}
Problem p02289 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
  priority_queue<int> Q;
  while (true) {
    int num;
    char oper[1000];
    scanf("%s", oper);
    if (oper[2] == 'd')
      break;
    else if (oper[0] == 'i') {
      scanf("%d", &num);
      Q.push(num);
    } else if (oper[1] == 'x') {
      int ex = Q.top();
      Q.pop();
      cout << ex << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
using namespace std;

int main() {
  priority_queue<int> Q;
  while (cin) {
    int num;
    string oper;
    cin >> oper;
    if (oper == "end")
      break;
    else if (oper == "insert") {
      cin >> num;
      Q.push(num);
    } else if (oper == "extract") {
      int ex = Q.top();
      Q.pop();
      cout << ex << endl;
    }
  }
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

ll a[20000000] = {NIL};
int h;

ll rk(int n) {
  if (n * 2 + 1 <= h)
    return a[n * 2 + 1];
  return NIL;
}
ll lk(int n) {
  if (n * 2 <= h)
    return a[n * 2];
  return NIL;
}
ll pk(int n) {
  if (n / 2 <= h)
    return a[n / 2];
  return NIL;
}

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

void heaplify(int n) {
  int l = n;
  if (a[l] < rk(n))
    l = n * 2 + 1;
  if (a[l] < lk(n))
    l = n * 2;
  if (l != n) {
    swap(a[l], a[n]);
    heaplify(l);
  }
}

void ins(int v) {
  a[h] = v;
  int d = h;
  h++;
  while (pk(d) < a[d]) {
    swap(a[d / 2], a[d]);
    d = d / 2;
  }
}

ll ext() {
  ll res = a[1];
  h--;
  a[1] = a[h];
  a[h] = NIL;
  heaplify(1);
  return res;
}

int main() {
  h = 1;
  a[0] = -NIL;
  string s;
  int t;
  while (!cin.eof()) {
    cin >> s;
    if (s == "extract")
      cout << ext() << endl;
    else {
      cin >> t;
      ins(t);
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

ll a[500010] = {NIL};
int h;

ll rk(int n) {
  if (n * 2 + 1 <= h)
    return a[n * 2 + 1];
  return NIL;
}
ll lk(int n) {
  if (n * 2 <= h)
    return a[n * 2];
  return NIL;
}
ll pk(int n) {
  if (n / 2 <= h)
    return a[n / 2];
  return NIL;
}

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

void heaplify(int n) {
  int l = n;
  if (a[l] < rk(n))
    l = n * 2 + 1;
  if (a[l] < lk(n))
    l = n * 2;
  if (l != n) {
    swap(a[l], a[n]);
    heaplify(l);
  }
}

void ins(int v) {
  a[h] = v;
  int d = h;
  h++;
  while (pk(d) < a[d]) {
    swap(a[d / 2], a[d]);
    d = d / 2;
  }
}

ll ext() {
  ll res = a[1];
  h--;
  a[1] = a[h];
  a[h] = NIL;
  heaplify(1);
  return res;
}

int main() {
  h = 1;
  a[0] = -NIL;
  string s;
  int t;
  while (!cin.eof()) {
    cin >> s;
    if (s == "extract")
      cout << ext() << endl;
    else {
      cin >> t;
      ins(t);
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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

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

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

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

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

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

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

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, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#define MAX 5000001
#define INFTY (1 << 30)

using namespace std;
int n = 0, arr[MAX];

void swapNode(int index1, int index2) {
  int temp = arr[index1];
  arr[index1] = arr[index2];
  arr[index2] = temp;
}

void maxHeapify(int i) {
  // if(i > n) return;
  int left = 2 * i;
  int right = 2 * i + 1;
  int largest = i;
  if (left <= n && right <= n) {
    int larger = arr[left] > arr[right] ? left : right;
    largest = arr[largest] > arr[larger] ? largest : larger;
  } else if (left <= n) {
    largest = arr[largest] > arr[left] ? largest : left;
  } else if (right <= n) {
    largest = arr[largest] > arr[right] ? largest : right;
  }
  if (largest != i) {
    swapNode(i, largest);
    maxHeapify(largest);
  }
}

void insertKey(int i, int key) {
  if (key < arr[i])
    return;
  arr[i] = key;
  while (i > 1 && arr[i / 2] < arr[i]) {
    swapNode(i, i / 2);
    i = i / 2;
  }
}

int insert(int key) {
  n++;
  arr[n] = -INFTY;
  insertKey(n, key);
}

int extract() {
  if (n < 1)
    return -INFTY;
  int res = arr[1];
  arr[1] = arr[n--];
  maxHeapify(1);
  return res;
}

int main() {
  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);
      insert(key);

    } else {
      printf("%d\\n", extract());
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#define MAX 500001

using namespace std;
int n = 0, arr[MAX];

void swapNode(int index1, int index2) {
  int temp = arr[index1];
  arr[index1] = arr[index2];
  arr[index2] = temp;
}

void maxHeapify(int i) {
  // if(i > n) return;
  int left = 2 * i;
  int right = 2 * i + 1;
  int largest = i;
  if (left <= n && right <= n) {
    int larger = arr[left] > arr[right] ? left : right;
    largest = arr[largest] > arr[larger] ? largest : larger;
  } else if (left <= n) {
    largest = arr[largest] > arr[left] ? largest : left;
  } else if (right <= n) {
    largest = arr[largest] > arr[right] ? largest : right;
  }
  if (largest != i) {
    swapNode(i, largest);
    maxHeapify(largest);
  }
}

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

  char com[10];
  while (1) {
    scanf("%s", com);
    if (com[0] == 'e' && com[1] == 'n')
      break;
    else if (com[0] == 'i') {
      n++;
      scanf("%d", &arr[n]);
    } else {
      for (int i = n / 2; i >= 1; i--) {
        maxHeapify(i);
      }
      printf("%d\\n", arr[1]);
      arr[1] = arr[n];
      n--;
      maxHeapify(1);
    }
  }
  return 0;
}
Problem p02289 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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