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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
vector<ll> v;

int main(void) {
  vector<ll> v;
  int n, k;
  ll t[100000];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    if (i != 0) {
      v.push_back(t[i] - t[i - 1] - 1);
    }
  }
  sort(v.begin(), v.end());
  ll ans = n;
  for (int i = 0; i < n - k; i++) {
    ans += v[i];
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
vector<ll> v;

int main(void) {
  vector<ll> v;
  int n, k;
  ll t[10000];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    if (i != 0) {
      v.push_back(t[i] - t[i - 1] - 1);
    }
  }
  sort(v.begin(), v.end());
  ll ans = n;
  for (int i = 0; i < n - k; i++) {
    ans += v[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p00570 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
vector<ll> v;

int main(void) {
  vector<ll> v;
  int n, k;
  ll t[1000000];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    if (i != 0) {
      v.push_back(t[i] - t[i - 1] - 1);
    }
  }
  sort(v.begin(), v.end());
  ll ans = n;
  for (int i = 0; i < n - k; i++) {
    ans += v[i];
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
vector<ll> v;

int main(void) {
  vector<ll> v;
  int n, k;
  ll t[10000];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    if (i != 0) {
      v.push_back(t[i] - t[i - 1] - 1);
    }
  }
  sort(v.begin(), v.end());
  ll ans = n;
  for (int i = 0; i < n - k; i++) {
    ans += v[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p00570 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int a[100001];
vector<int> v;
int main(void) {
  int n;
  int k;
  cin >> n >> k;
  int i;
  int x, y;
  for (i = 0; i < n; i++)
    cin >> a[i];
  int cnt = 0;
  for (i = 0; i < n - 1; i++) {
    x = a[i];
    x++;
    y = a[i + 1] - x;
    // cout<<y<<endl;
    if (y != 0)
      v.push_back(y);
  }
  sort(v.begin(), v.end());
  int m = v.size() - (k - 1);
  int sum = n;
  // cout<<m<<endl;
  for (i = 0; i < m; i++) {
    sum += v[i];
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[100001];
int main(void) {
  int n;
  int k;
  cin >> n >> k;
  int i;
  int x, y;
  priority_queue<int> q;
  for (i = 0; i < n; i++)
    cin >> a[i];
  int cnt = 0;
  for (i = 0; i < n - 1; i++) {
    x = a[i];
    x++;
    y = a[i + 1] - x;
    // cout<<y<<endl;
    if (y != 0)
      q.push(y);
  }
  for (i = 0; i < k - 1; i++)
    q.pop();
  int sum = n;
  while (!q.empty()) {
    sum += q.top();
    q.pop();
  }
  cout << sum << endl;
  return 0;
}
Problem p00571 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(ll arg_size, ll arg_value) {
    size = arg_size;
    value = arg_value;
  }
  ll size, value;
  bool operator<(const struct Info &arg) const { return size < arg.size; }
};

vector<Info> info;
ll ruisekiwa[500001], minus_value[500001];

int main() {

  int N;
  scanf("%d", &N);

  info.push_back(Info(0, 0));

  ll size, value;

  for (int i = 1; i <= N; i++) {
    scanf("%lld %lld", &size, &value);
    info.push_back(Info(size, value));
  }

  sort(info.begin(), info.end());

  ruisekiwa[0] = 0;
  minus_value[0] = 0;

  for (int i = 1; i <= N; i++) {
    ruisekiwa[i] = info[i].value;
    ruisekiwa[i] += ruisekiwa[i - 1];
    minus_value[i] = ruisekiwa[i - 1] - info[i].size;
  }

  for (int i = 2; i <= N; i++) {
    minus_value[i] = min(minus_value[i], minus_value[i - 1]);
  }

  ll ans = -99999999999999999;

  for (int i = N; i >= 1; i--) {
    ans = max(ans, ruisekiwa[i] - info[i].size - minus_value[i]);
  }

  printf("%lld\\n", ans);

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(ll arg_size, ll arg_value) {
    size = arg_size;
    value = arg_value;
  }
  ll size, value;
  bool operator<(const struct Info &arg) const { return size < arg.size; }
};

vector<Info> info;
ll ruisekiwa[100001], minus_value[100001];

int main() {

  int N;
  scanf("%d", &N);

  info.push_back(Info(0, 0));

  ll size, value;

  for (int i = 1; i <= N; i++) {
    scanf("%lld %lld", &size, &value);
    info.push_back(Info(size, value));
  }

  sort(info.begin(), info.end());

  ruisekiwa[0] = 0;
  minus_value[0] = 0;

  for (int i = 1; i <= N; i++) {
    ruisekiwa[i] = info[i].value;
    ruisekiwa[i] += ruisekiwa[i - 1];
    minus_value[i] = ruisekiwa[i - 1] - info[i].size;
  }

  for (int i = 2; i <= N; i++) {
    minus_value[i] = min(minus_value[i], minus_value[i - 1]);
  }

  ll ans = -99999999999999999;

  for (int i = N; i >= 1; i--) {
    ans = max(ans, ruisekiwa[i] - info[i].size - minus_value[i]);
  }

  printf("%lld\\n", ans);

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long ll;
#define MAX_N 500000
struct goods {
  ll val, siz;
};

bool cmp(const goods &a, const goods &b) { return a.siz < b.siz; }

goods g[MAX_N + 1];
ll cs[MAX_N + 1];
int main() {
  ll S;
  ll ANS;
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> g[i].siz >> g[i].val;
  }

  /*
  for (int i = 1; i <= N-1; i++) {
          for (int j = i + 1; j <= N; j++) {
                  if (g[i].siz > g[j].siz) {
                          goods temp;
                          temp = g[i];
                          g[i] = g[j];
                          g[j] = temp;
                  }
          }
  }
  */

  sort(g + 1, g + N + 1, cmp);

  S = g[1].val + g[N].val;
  ANS = S - (g[N].siz - g[1].siz);
  cs[0] = 0;
  for (int i = 1; i <= N; i++) {
    cs[i] = cs[i - 1] + g[i].val;
  }
  ll MIN = 0xffffffffffff;
  for (int i = 1; i <= N; i++) {
    if (cs[i - 1] - g[i].siz < MIN)
      MIN = cs[i - 1] - g[i].siz;
    ANS = max(ANS, cs[i] - g[i].siz - MIN);
  }
  cout << ANS << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long ll;
#define MAX_N 500000
struct goods {
  ll val, siz;
};

bool cmp(const goods &a, const goods &b) { return a.siz < b.siz; }

goods g[MAX_N + 1];
ll cs[MAX_N + 1];
int main() {
  ll S;
  ll ANS;
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> g[i].siz >> g[i].val;
  }

  /*
  for (int i = 1; i <= N-1; i++) {
          for (int j = i + 1; j <= N; j++) {
                  if (g[i].siz > g[j].siz) {
                          goods temp;
                          temp = g[i];
                          g[i] = g[j];
                          g[j] = temp;
                  }
          }
  }
  */

  sort(g + 1, g + N + 1, cmp);

  S = g[1].val + g[N].val;
  ANS = S - (g[N].siz - g[1].siz);
  cs[0] = 0;
  for (int i = 1; i <= N; i++) {
    cs[i] = cs[i - 1] + g[i].val;
  }

  for (int i = 1; i <= N; i++) {

    for (int j = i; j <= N; j++) {
      if (i == j) {

        ANS = max(ANS, g[i].val);

      } else {

        ANS = max(ANS, cs[j] - cs[i - 1] - (g[j].siz - g[i].siz));
      }
    }
  }
  cout << ANS << endl;
  return 0;
}
Problem p00572 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
#define Max(a, b) a = max(a, b);
#define lol(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int h, w;
int dp[3010][3];
char m[3010][3010];
int main() {
  cin >> h >> w;
  lol(i, h) lol(j, w) cin >> m[i][j];
  int ans = 0;
  for (int k = 0; k <= h + w - 2; k++) {
    memset(dp, 0, sizeof(dp));
    int last = 0;
    for (int y = 0; y <= k; y++) {
      int x = k - y;
      if (x >= w || y >= h) {
        continue;
      }
      bool tate = false, yoko = false;
      if (y > 0)
        if (m[y - 1][x] == 'R' && m[y][x] == 'G' && m[y + 1][x] == 'W')
          tate = 1;
      if (x > 0)
        if (m[y][x - 1] == 'R' && m[y][x] == 'G' && m[y][x + 1] == 'W')
          yoko = 1;
      Max(dp[last + 1][0], dp[last][0]);
      Max(dp[last + 1][0], dp[last][1]);
      Max(dp[last + 1][0], dp[last][2]);
      if (tate) {
        Max(dp[last + 1][1], dp[last][0] + 1);
        Max(dp[last + 1][1], dp[last][1] + 1);
      }
      if (yoko) {
        Max(dp[last + 1][2], dp[last][0] + 1);
        Max(dp[last + 1][2], dp[last][2] + 1);
      }
      last++;
    }
    ans += max(dp[last][0], max(dp[last][1], dp[last][2]));
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
#define Max(a, b) a = max(a, b);
#define lol(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int h, w;
int dp[3010][3];
char m[3010][3010];
int main() {
  cin >> h >> w;
  lol(i, h) lol(j, w) cin >> m[i][j];
  int ans = 0;
  for (int k = 0; k <= h + w - 2; k++) {
    memset(dp, 0, sizeof(dp));
    for (int y = 0; y <= k; y++) {
      int x = k - y;
      bool tate = false, yoko = false;
      if (y > 0)
        if (m[y - 1][x] == 'R' && m[y][x] == 'G' && m[y + 1][x] == 'W')
          tate = 1;
      if (x > 0)
        if (m[y][x - 1] == 'R' && m[y][x] == 'G' && m[y][x + 1] == 'W')
          yoko = 1;
      Max(dp[y + 1][0], dp[y][0]);
      Max(dp[y + 1][0], dp[y][1]);
      Max(dp[y + 1][0], dp[y][2]);
      if (tate) {
        Max(dp[y + 1][1], dp[y][0] + 1);
        Max(dp[y + 1][1], dp[y][1] + 1);
      }
      if (yoko) {
        Max(dp[y + 1][2], dp[y][0] + 1);
        Max(dp[y + 1][2], dp[y][2] + 1);
      }
    }
    ans += max(dp[k + 1][0], max(dp[k + 1][1], dp[k + 1][2]));
  }
  cout << ans << endl;
  return 0;
}
Problem p00578 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
int main(int, char **) {
  int num = 0;
  std::cin >> num;
  int buff = 0;
  int pre = 0;
  std::map<int, int> count;
  std::cin >> pre;
  for (int c = 0; num - 1 > c; ++c) {
    std::cin >> buff;
    if (pre > buff) {
      count[pre] -= 1;
      count[buff] += 1;
    }
    pre = buff;
  }
  count[pre] -= 1;
  count[0] += 1;

  int max_num = 0;
  int current_num = 0;
  for (auto itr = count.begin(); count.end() != itr; ++itr) {
    current_num += itr->second;
    if (max_num < current_num) {
      max_num = current_num;
    }
  }
  std::cout << max_num << std::endl;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <unordered_map>
int main(int, char **) {
  int num = 0;
  std::cin >> num;
  int buff = 0;
  int pre = 0;
  std::unordered_map<int, int> count;
  std::cin >> pre;
  for (int c = 0; num - 1 > c; ++c) {
    std::cin >> buff;
    if (pre > buff) {
      for (int h = buff; h < pre; ++h) {
        count[h] += 1;
      }
    }
    pre = buff;
  }
  for (int h = pre - 1; h >= 0; --h) {
    count[h] += 1;
  }

  int max_num = 0;
  for (auto itr = count.begin(); count.end() != itr; ++itr) {
    if (max_num < itr->second) {
      max_num = itr->second;
    }
  }
  std::cout << max_num << std::endl;
}
Problem p00587 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int SIZE = 128;
bool A[SIZE], B[SIZE], C[SIZE];

int getPos(const string &s) {
  int p = 0;
  for (int i = 0; i < (int)s.length(); i++) {
    if (s[i] == '(')
      p++;
    else if (s[i] == ')')
      p--;
    if (p == 0 && s[i] == ',')
      return i;
  }
  return -1;
}

void parse(const string &s, bool isA, int idx) {
  if (s == "")
    return;
  if (idx < 0 || idx >= SIZE)
    return;
  if (isA)
    A[idx] = true;
  else
    B[idx] = true;
  string nex = s.substr(1, s.length() - 2);
  int pos = getPos(nex);
  string left = nex.substr(0, pos);
  string right = nex.substr(pos + 1);
  parse(left, isA, 2 * idx + 1);
  parse(right, isA, 2 * idx + 2);
}

string makeTree(int idx) {
  if (!C[idx])
    return "";
  string res = "";
  if (2 * idx + 1 < SIZE)
    res += makeTree(2 * idx + 1);
  res += ",";
  if (2 * idx + 2 < SIZE)
    res += makeTree(2 * idx + 2);
  res = "(" + res + ")";
  return res;
}

int main() {

  char c;
  string a, b;
  while (cin >> c >> a >> b) {
    memset(A, false, sizeof(A));
    memset(B, false, sizeof(B));
    memset(C, false, sizeof(C));
    parse(a, true, 0);
    parse(b, false, 0);
    if (c == 'i')
      for (int i = 0; i < SIZE; i++)
        C[i] = A[i] && B[i];
    else
      for (int i = 0; i < SIZE; i++)
        C[i] = A[i] || B[i];
    cout << makeTree(0) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int SIZE = 128;
bool A[SIZE], B[SIZE], C[SIZE];

int getPos(const string &s) {
  int p = 0;
  for (int i = 0; i < (int)s.length(); i++) {
    if (s[i] == '(')
      p++;
    else if (s[i] == ')')
      p--;
    if (p == 0 && s[i] == ',')
      return i;
  }
  return -1;
}

void parse(const string &s, bool isA, int idx) {
  if (s == "")
    return;
  if (isA)
    A[idx] = true;
  else
    B[idx] = true;
  string nex = s.substr(1, s.length() - 2);
  int pos = getPos(nex);
  if (pos == -1)
    return;
  string left = nex.substr(0, pos);
  string right = nex.substr(pos + 1);
  parse(left, isA, 2 * idx + 1);
  parse(right, isA, 2 * idx + 2);
}

string makeTree(int idx) {
  if (!C[idx])
    return "";
  string res = "";
  if (2 * idx + 1 < SIZE)
    res += makeTree(2 * idx + 1);
  res += ",";
  if (2 * idx + 2 < SIZE)
    res += makeTree(2 * idx + 2);
  res = "(" + res + ")";
  return res;
}

int main() {

  char c;
  string a, b;
  while (cin >> c >> a >> b) {
    memset(A, false, sizeof(A));
    memset(B, false, sizeof(B));
    memset(C, false, sizeof(C));
    parse(a, true, 0);
    parse(b, false, 0);
    if (c == 'i')
      for (int i = 0; i < SIZE; i++)
        C[i] = A[i] && B[i];
    else
      for (int i = 0; i < SIZE; i++)
        C[i] = A[i] || B[i];
    cout << makeTree(0) << endl;
  }
  return 0;
}
Problem p00587 (C++) — buggy submission, verdict: Runtime Error

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

struct Node {
  Node *ch[2];
  Node() { ch[0] = ch[1] = NULL; }
};

void build(Node *node, const string &s, int &i) {
  if (s[i] == '(') {
    i++; // (
    node->ch[0] = new Node();
    build(node->ch[0], s, i);
    i++; // ,
    node->ch[1] = new Node();
    build(node->ch[1], s, i);
    i++; // )
  }
}

void print(Node *node) {
  if (node == NULL || (node->ch[0] == NULL && node->ch[1] == NULL))
    return;
  cout << "(";
  print(node->ch[0]);
  cout << ",";
  print(node->ch[1]);
  cout << ")";
}

void erase(Node *node) {
  for (int i = 0; i < 2; i++)
    if (node->ch[i])
      erase(node->ch[i]);
  delete node;
}

void Intersection(Node *res, Node *a, Node *b) {
  for (int i = 0; i < 2; i++)
    if (a && a->ch[i] && b && b->ch[i]) {
      res->ch[i] = new Node();
      Intersection(res->ch[i], a->ch[i], b->ch[i]);
    }
}

void Union(Node *res, Node *a, Node *b) {
  for (int i = 0; i < 2; i++)
    if ((a && a->ch[i]) || (b && b->ch[i])) {
      res->ch[i] = new Node();
      Union(res->ch[i], (a ? a->ch[i] : NULL), (b ? b->ch[i] : NULL));
    }
}

int main() {
  for (string cmd, A, B; cin >> cmd >> A >> B;) {
    Node *roota = new Node();
    Node *rootb = new Node();
    Node *rootc = new Node();

    int i = 0;
    build(roota, A, i);

    i = 0;
    build(rootb, B, i);

    if (cmd[0] == 'i')
      Intersection(rootc, roota, rootb);
    else
      Union(rootc, roota, rootb);

    print(rootc);
    cout << endl;

    erase(roota);
    erase(rootb);
    erase(rootc);
  }
}

Error: 0

Fixed submission:

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

struct Node {
  Node *ch[2];
  Node() { ch[0] = ch[1] = NULL; }
};

void build(Node *node, const string &s, int &i) {
  if (s[i] == '(') {
    i++; // (
    node->ch[0] = new Node();
    build(node->ch[0], s, i);
    i++; // ,
    node->ch[1] = new Node();
    build(node->ch[1], s, i);
    i++; // )
  }
}

void print(Node *node) {
  if (node == NULL || (node->ch[0] == NULL && node->ch[1] == NULL))
    return;
  cout << "(";
  print(node->ch[0]);
  cout << ",";
  print(node->ch[1]);
  cout << ")";
}

void erase(Node *node) {
  for (int i = 0; i < 2; i++)
    if (node->ch[i])
      erase(node->ch[i]);
  delete node;
}

void Intersection(Node *res, Node *a, Node *b) {
  for (int i = 0; i < 2; i++)
    if (a && a->ch[i] && b && b->ch[i]) {
      res->ch[i] = new Node();
      Intersection(res->ch[i], a->ch[i], b->ch[i]);
    }
}

void Union(Node *res, Node *a, Node *b) {
  for (int i = 0; i < 2; i++)
    if ((a && a->ch[i]) || (b && b->ch[i])) {
      res->ch[i] = new Node();
      Union(res->ch[i], a->ch[i], b->ch[i]);
    }
}

int main() {
  for (string cmd, A, B; cin >> cmd >> A >> B;) {
    Node *roota = new Node();
    Node *rootb = new Node();
    Node *rootc = new Node();

    int i = 0;
    build(roota, A, i);

    i = 0;
    build(rootb, B, i);

    if (cmd[0] == 'i')
      Intersection(rootc, roota, rootb);
    else
      Union(rootc, roota, rootb);

    print(rootc);
    cout << endl;

    erase(roota);
    erase(rootb);
    erase(rootc);
  }
}
Problem p00587 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define MAX 1000000
using namespace std;
string a, b, z;
int d;
string ans;
bool flgA[MAX];
bool flgB[MAX];

void rec(int x) {

  ans.push_back('(');

  if (flgA[x * 2] == true) {
    rec(x * 2);
  }
  ans.push_back(',');
  if (flgA[x * 2 + 1] == true) {
    rec(x * 2 + 1);
  }

  ans.push_back(')');
}

int main() {
  while (cin >> z) {

    cin >> a;
    for (int i = 0; i < MAX; i++)
      flgA[i] = flgB[i] = false;
    flgA[1] = flgB[1] = true;

    d = 1;
    for (int i = 0; i < (int)a.size(); i++) {
      if (d >= MAX)
        continue;
      if (a[i] == '(') {
        flgA[d] = true;
        d *= 2;
      } else if (a[i] == ',') {
        d = d + 1;
      } else {
        d /= 2;
      }
    }

    cin >> b;
    d = 1;
    for (int i = 0; i < (int)b.size(); i++) {
      if (d >= MAX)
        continue;
      if (b[i] == '(') {
        flgB[d] = true;
        d *= 2;
      } else if (b[i] == ',') {
        d = d + 1;
      } else {
        d /= 2;
      }
    }

    for (int i = 0; i < MAX; i++) {
      if (flgB[i] == false) {
        if (z == "i") {
          flgA[i] = false;
        }
      } else {
        if (z == "u") {
          flgA[i] = true;
        }
      }
    }

    ans = "";
    rec(1);
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
string a, b, z;
int d;
string ans;
bool flgA[5000000];
bool flgB[5000000];

void rec(int x) {

  ans.push_back('(');

  if (flgA[x * 2] == true) {
    rec(x * 2);
  }
  ans.push_back(',');
  if (flgA[x * 2 + 1] == true) {
    rec(x * 2 + 1);
  }

  ans.push_back(')');
}

int main() {
  while (cin >> z) {

    cin >> a;
    for (int i = 0; i < 5000000; i++)
      flgA[i] = flgB[i] = false;
    flgA[1] = flgB[1] = true;

    d = 1;
    for (int i = 0; i < (int)a.size(); i++) {
      if (a[i] == '(') {
        flgA[d] = true;
        d *= 2;
      } else if (a[i] == ',') {
        d = d + 1;
      } else {
        d /= 2;
      }
    }

    cin >> b;
    d = 1;
    for (int i = 0; i < (int)b.size(); i++) {
      if (b[i] == '(') {
        flgB[d] = true;
        d *= 2;
      } else if (b[i] == ',') {
        d = d + 1;
      } else {
        d /= 2;
      }
    }

    for (int i = 0; i < 5000000; i++) {
      if (flgB[i] == false) {
        if (z == "i") {
          flgA[i] = false;
        }
      } else {
        if (z == "u") {
          flgA[i] = true;
        }
      }
    }

    ans = "";
    rec(1);
    cout << ans << endl;
  }
  return 0;
}
Problem p00587 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

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

Node *make_tree(Node *t, int *n, string s) {
  if (s.empty())
    return NULL;

  int paren = 0;
  int i;
  for (i = 1; paren != 0 || s[i] != ','; ++i) {
    if (s[i] == '(')
      ++paren;
    else if (s[i] == ')')
      --paren;
  }

  int curN = (*n)++;
  t[curN].left = make_tree(t, n, s.substr(1, i - 1));
  t[curN].right = make_tree(t, n, s.substr(i + 1, s.size() - 2 - i));

  return t + curN;
}

void print_intersection(Node *a, Node *b) {
  if (a == NULL || b == NULL)
    return;
  putchar('(');
  print_intersection(a->left, b->left);
  putchar(',');
  print_intersection(a->right, b->right);
  putchar(')');
}
void print_union(Node *a, Node *b) {
  if (a == NULL && b == NULL)
    return;
  putchar('(');
  print_union(a ? a->left : NULL, b ? b->left : NULL);
  putchar(',');
  print_union(a ? a->right : NULL, b ? b->right : NULL);
  putchar(')');
}

int main() {
  char ope;
  string a, b;
  while (cin >> ope >> a >> b) {
    const int TreeSize = 128;
    Node treeA[TreeSize], treeB[TreeSize];
    memset(treeA, 0, sizeof(treeA));
    memset(treeB, 0, sizeof(treeB));

    int n = 0;
    Node *first = make_tree(treeA, &n, a);
    n = 0;
    Node *second = make_tree(treeB, &n, b);

    if (ope == 'u')
      print_union(first, second);
    else
      print_intersection(first, second);
    printf("\\n");
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

void make_tree(bool *tree, int node, string s) {
  if (s.empty())
    return;

  tree[node] = true;

  int paren = 0;
  int i;
  for (i = 1; paren != 0 || s[i] != ','; ++i) {
    if (s[i] == '(')
      ++paren;
    else if (s[i] == ')')
      --paren;
  }
  make_tree(tree, node * 2 + 1, s.substr(1, i - 1));
  make_tree(tree, node * 2 + 2, s.substr(i + 1, s.size() - 2 - i));
}

string to_s(bool *tree, int node) {
  if (!tree[node])
    return "";
  else
    return "(" + to_s(tree, node * 2 + 1) + "," + to_s(tree, node * 2 + 2) +
           ")";
}

int main() {
  char ope;
  string s1, s2;
  const int TreeSize = 256;
  bool tree1[TreeSize], tree2[TreeSize], tree[TreeSize];

  while (cin >> ope >> s1 >> s2) {
    fill(tree1, tree1 + TreeSize, false);
    fill(tree2, tree2 + TreeSize, false);

    make_tree(tree1, 0, s1);
    make_tree(tree2, 0, s2);

    if (ope == 'i')
      for (int i = 0; i < TreeSize; ++i)
        tree[i] = tree1[i] & tree2[i];
    else
      for (int i = 0; i < TreeSize; ++i)
        tree[i] = tree1[i] | tree2[i];

    cout << to_s(tree, 0) << endl;
  }

  return 0;
}