Problem p00598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

vector<int> g_sets[5];
vector<int> g_all;

struct Result {
  vector<int> value;
  int p;
};

Result expr(const string &s, int p);
Result factor(const string &s, int p);

Result expr(const string &s, int p) {
  Result r = factor(s, p);
  while (islower(s[r.p])) {
    Result q = factor(s, r.p + 1);
    vector<int> temp;
    if (s[r.p] == 'u') {
      set_union(r.value.begin(), r.value.end(), q.value.begin(), q.value.end(),
                back_inserter(temp));
    } else if (s[r.p] == 'i') {
      set_intersection(r.value.begin(), r.value.end(), q.value.begin(),
                       q.value.end(), back_inserter(temp));
    } else if (s[r.p] == 'd') {
      set_difference(r.value.begin(), r.value.end(), q.value.begin(),
                     q.value.end(), back_inserter(temp));
    } else if (s[r.p] == 's') {
      set_symmetric_difference(r.value.begin(), r.value.end(), q.value.begin(),
                               q.value.end(), back_inserter(temp));
    }
    r.value = temp;
    r.p = q.p;
  }
  return r;
}

Result factor(const string &s, int p) {
  bool complement = false;
  while (s[p] == 'c') {
    complement = !complement;
    ++p;
  }
  Result r;
  if (isupper(s[p])) {
    r.value = g_sets[s[p] - 'A'];
    r.p = p + 1;
  } else {
    Result q = expr(s, p + 1);
    r.value = q.value;
    r.p = q.p + 1;
  }
  if (complement) {
    vector<int> temp;
    set_difference(g_all.begin(), g_all.end(), r.value.begin(), r.value.end(),
                   back_inserter(temp));
    r.value = temp;
  }
  return r;
}

int main() {
  while (!cin.eof()) {
    g_all.clear();
    while (true) {
      string s;
      int n;
      if (!(cin >> s >> n)) {
        return 0;
      }
      if (s == "R" && n == 0) {
        break;
      }
      int index = s[0] - 'A';
      g_sets[index].clear();
      while (n--) {
        int x;
        cin >> x;
        g_sets[index].push_back(x);
        g_all.push_back(x);
      }
      sort(g_sets[index].begin(), g_sets[index].end());
    }
    sort(g_all.begin(), g_all.end());
    g_all.erase(unique(g_all.begin(), g_all.end()), g_all.end());
    string q;
    cin >> q;
    vector<int> result = expr(q, 0).value;
    if (result.size() == 0) {
      cout << "NULL" << endl;
    } else {
      for (int i = 0; i < result.size(); ++i) {
        cout << result[i];
        if (i + 1 == result.size()) {
          cout << endl;
        } else {
          cout << " ";
        }
      }
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

vector<int> g_sets[5];
vector<int> g_all;

struct Result {
  vector<int> value;
  int p;
};

Result expr(const string &s, int p);
Result factor(const string &s, int p);

Result expr(const string &s, int p) {
  Result r = factor(s, p);
  while (islower(s[r.p])) {
    Result q = factor(s, r.p + 1);
    vector<int> temp;
    if (s[r.p] == 'u') {
      set_union(r.value.begin(), r.value.end(), q.value.begin(), q.value.end(),
                back_inserter(temp));
    } else if (s[r.p] == 'i') {
      set_intersection(r.value.begin(), r.value.end(), q.value.begin(),
                       q.value.end(), back_inserter(temp));
    } else if (s[r.p] == 'd') {
      set_difference(r.value.begin(), r.value.end(), q.value.begin(),
                     q.value.end(), back_inserter(temp));
    } else if (s[r.p] == 's') {
      set_symmetric_difference(r.value.begin(), r.value.end(), q.value.begin(),
                               q.value.end(), back_inserter(temp));
    }
    r.value = temp;
    r.p = q.p;
  }
  return r;
}

Result factor(const string &s, int p) {
  bool complement = false;
  while (s[p] == 'c') {
    complement = !complement;
    ++p;
  }
  Result r;
  if (isupper(s[p])) {
    r.value = g_sets[s[p] - 'A'];
    r.p = p + 1;
  } else {
    Result q = expr(s, p + 1);
    r.value = q.value;
    r.p = q.p + 1;
  }
  if (complement) {
    vector<int> temp;
    set_difference(g_all.begin(), g_all.end(), r.value.begin(), r.value.end(),
                   back_inserter(temp));
    r.value = temp;
  }
  return r;
}

int main() {
  while (!cin.eof()) {
    while (true) {
      string s;
      int n;
      cin >> s >> n;
      if (s == "R" && n == 0) {
        break;
      }
      int index = s[0] - 'A';
      g_sets[index].clear();
      while (n--) {
        int x;
        cin >> x;
        g_sets[index].push_back(x);
        g_all.push_back(x);
      }
      sort(g_sets[index].begin(), g_sets[index].end());
    }
    sort(g_all.begin(), g_all.end());
    g_all.erase(unique(g_all.begin(), g_all.end()), g_all.end());
    string q;
    cin >> q;
    vector<int> result = expr(q, 0).value;
    if (result.size() == 0) {
      cout << "NULL" << endl;
    } else {
      for (int i = 0; i < result.size(); ++i) {
        cout << result[i];
        if (i + 1 == result.size()) {
          cout << endl;
        } else {
          cout << " ";
        }
      }
    }
  }
  return 0;
}