Problem p00598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> V;
V A[10], U;
string S;

V Union(V a, V b) {
  for (int i = 0; i < (int)b.size(); i++)
    a.push_back(b[i]);
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
  return a;
}

V Inter(V a, V b) {
  V res;
  int n = a.size(), m = b.size();
  for (int i = 0; i < n; i++) {
    int flg = 0;
    for (int j = 0; j < m; j++)
      flg |= a[i] == b[j];
    if (flg)
      res.push_back(a[i]);
  }

  sort(res.begin(), res.end());
  return res;
}

V Diff(V a, V b) {
  V res;
  int n = a.size();
  int m = b.size();
  for (int i = 0; i < n; i++) {
    int flg = 1;
    for (int j = 0; j < m; j++)
      flg &= a[i] != b[j];
    if (flg)
      res.push_back(a[i]);
  }

  sort(res.begin(), res.end());
  return res;
}

V Sdiff(V a, V b) { return Union(Diff(a, b), Diff(b, a)); }

V Comp(V a) {
  V res;
  int n = U.size(), m = a.size();
  for (int i = 0; i < n; i++) {
    int flg = 1;
    for (int j = 0; j < m; j++)
      flg &= U[i] != a[j];
    if (flg)
      res.push_back(U[i]);
  }
  sort(res.begin(), res.end());
  return res;
}

int p;
V bnf();
V getV() {
  char ch = S[p];
  V res;
  if (ch == '(')
    p++, res = bnf(), p++;
  else if (isupper(ch))
    p++, res = A[ch - 'A'];
  else if (ch == 'c')
    p++, res = Comp(getV());
  else
    assert(isupper(ch));
  return res;
}

V bnf() {
  V res = getV();
  while (1) {
    char ch = S[p];
    if (ch == '(')
      p++, res = bnf(), p++;
    else if (ch == 'c')
      p++, res = Comp(getV());
    else if (ch == 'u')
      p++, res = Union(res, getV());
    else if (ch == 'i')
      p++, res = Inter(res, getV());
    else if (ch == 'd')
      p++, res = Diff(res, getV());
    else if (ch == 's')
      p++, res = Sdiff(res, getV());
    else if (p < S.size() && ch != ')')
      assert(ch == ')');
    else
      break;
  }
  return res;
}

int main() {

  while (1) {
    U.clear();
    while (1) {
      char ch;
      assert('A' <= ch && ch <= 'E');
      int n;
      if (!(cin >> ch >> n))
        exit(0);
      if (ch == 'R' && n == 0)
        break;
      int idx = ch - 'A';
      A[idx].resize(n);
      for (int i = 0; i < n; i++)
        cin >> A[idx][i], U.push_back(A[idx][i]);
      sort(A[idx].begin(), A[idx].end());
      A[idx].erase(unique(A[idx].begin(), A[idx].end()), A[idx].end());
    }

    sort(U.begin(), U.end());
    U.erase(unique(U.begin(), U.end()), U.end());
    cin >> S;
    p = 0;
    V ans = bnf();

    for (int i = 0; i < ans.size(); i++) {
      if (i)
        cout << " ";
      cout << ans[i];
    }
    if (ans.size() == 0)
      cout << "NULL";
    cout << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> V;
V A[10], U;
string S;

V Union(V a, V b) {
  for (int i = 0; i < (int)b.size(); i++)
    a.push_back(b[i]);
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
  return a;
}

V Inter(V a, V b) {
  V res;
  int n = a.size(), m = b.size();
  for (int i = 0; i < n; i++) {
    int flg = 0;
    for (int j = 0; j < m; j++)
      flg |= a[i] == b[j];
    if (flg)
      res.push_back(a[i]);
  }

  sort(res.begin(), res.end());
  return res;
}

V Diff(V a, V b) {
  V res;
  int n = a.size();
  int m = b.size();
  for (int i = 0; i < n; i++) {
    int flg = 1;
    for (int j = 0; j < m; j++)
      flg &= a[i] != b[j];
    if (flg)
      res.push_back(a[i]);
  }

  sort(res.begin(), res.end());
  return res;
}

V Sdiff(V a, V b) { return Union(Diff(a, b), Diff(b, a)); }

V Comp(V a) {
  V res;
  int n = U.size(), m = a.size();
  for (int i = 0; i < n; i++) {
    int flg = 1;
    for (int j = 0; j < m; j++)
      flg &= U[i] != a[j];
    if (flg)
      res.push_back(U[i]);
  }
  sort(res.begin(), res.end());
  return res;
}

int p;
V bnf();
V getV() {
  char ch = S[p];
  V res;
  if (ch == '(')
    p++, res = bnf(), p++;
  else if (isupper(ch))
    p++, res = A[ch - 'A'];
  else if (ch == 'c')
    p++, res = Comp(getV());
  else
    assert(isupper(ch));
  return res;
}

V bnf() {
  V res = getV();
  while (1) {
    char ch = S[p];
    if (ch == '(')
      p++, res = bnf(), p++;
    else if (ch == 'c')
      p++, res = Comp(getV());
    else if (ch == 'u')
      p++, res = Union(res, getV());
    else if (ch == 'i')
      p++, res = Inter(res, getV());
    else if (ch == 'd')
      p++, res = Diff(res, getV());
    else if (ch == 's')
      p++, res = Sdiff(res, getV());
    else if (p < S.size() && ch != ')')
      assert(ch == ')');
    else
      break;
  }
  return res;
}

int main() {

  while (1) {
    U.clear();
    while (1) {
      char ch;
      int n;
      if (!(cin >> ch >> n))
        exit(0);
      if (ch == 'R' && n == 0)
        break;
      int idx = ch - 'A';
      A[idx].resize(n);
      for (int i = 0; i < n; i++)
        cin >> A[idx][i], U.push_back(A[idx][i]);
      sort(A[idx].begin(), A[idx].end());
      A[idx].erase(unique(A[idx].begin(), A[idx].end()), A[idx].end());
    }

    sort(U.begin(), U.end());
    U.erase(unique(U.begin(), U.end()), U.end());
    cin >> S;
    p = 0;
    V ans = bnf();

    for (int i = 0; i < ans.size(); i++) {
      if (i)
        cout << " ";
      cout << ans[i];
    }
    if (ans.size() == 0)
      cout << "NULL";
    cout << endl;
  }

  return 0;
}