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

#include "bits/stdc++.h"
using namespace std;
map<char, set<int>> mp;
string test = "uids";
set<int> calc(const set<int> &l, const set<int> &r, char c) {
  set<int> ans;
  if (c == 'u') {
    ans = l;
    for (auto n : r)
      ans.emplace(n);
  } else if (c == 'i') {
    for (auto &&n : l) {
      if (r.find(n) != r.end())
        ans.emplace(n);
    }
  } else if (c == 'd') {
    for (auto &&n : l) {
      if (r.find(n) == r.end())
        ans.emplace(n);
    }
  } else if (c == 's') {
    for (auto &&n : l) {
      if (r.find(n) == r.end())
        ans.emplace(n);
    }
    for (auto &&n : r) {
      if (l.find(n) == l.end())
        ans.emplace(n);
    }
  }
  return ans;
}
string st;
int a;
set<int> expr();
set<int> exp() {
  if (st[a] == '(') {
    a++;
    auto n = expr();
    return n;
  } else if (st[a] == 'c') {
    a++;
    auto n = exp();
    n = calc(mp['U'], n, 'd');
    return n;
  } else {
    char c(st[a]);
    assert(mp.find(c) != mp.end());

    set<int> n(mp[c]);
    a++;
    return n;
  }
}
set<int> expr() {
  set<int> l(exp());
  while (1) {
    if (a == st.size() || st[a] == ')') {
      a++;
      return l;
    } else {
      char op = st[a];
      assert(test.find(op) != string::npos);
      a++;
      set<int> r(exp());
      l = calc(l, r, op);
    }
  }
}
int main() {
  char c;
  int n;
  while (cin >> c >> n) {
    if (c == 'R') {
      set<int> v;
      for (auto m : mp) {
        for (auto n : m.second) {
          v.insert(n);
        }
      }
      mp['U'] = v;
      cin >> st;
      auto ans = expr();
      for (auto aa : ans) {
        cout << aa;
        if (*prev(ans.end()) == aa) {
          cout << endl;
        } else
          cout << " ";
      }
      if (ans.empty()) {
        cout << "NULL" << endl;
      }
      mp.clear();
      a = 0;
    } else {
      for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        mp[c].emplace(a);
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
map<char, set<int>> mp;
string test = "uids";
set<int> calc(const set<int> &l, const set<int> &r, char c) {
  set<int> ans;
  if (c == 'u') {
    ans = l;
    for (auto n : r)
      ans.emplace(n);
  } else if (c == 'i') {
    for (auto &&n : l) {
      if (r.find(n) != r.end())
        ans.emplace(n);
    }
  } else if (c == 'd') {
    for (auto &&n : l) {
      if (r.find(n) == r.end())
        ans.emplace(n);
    }
  } else if (c == 's') {
    for (auto &&n : l) {
      if (r.find(n) == r.end())
        ans.emplace(n);
    }
    for (auto &&n : r) {
      if (l.find(n) == l.end())
        ans.emplace(n);
    }
  }
  return ans;
}
string st;
int a;
set<int> expr();
set<int> exp() {
  if (st[a] == '(') {
    a++;
    auto n = expr();
    return n;
  } else if (st[a] == 'c') {
    a++;
    char c(st[a]);
    // assert(mp.find(c) != mp.end());
    set<int> n(mp[c]);
    n = calc(mp['U'], n, 'd');
    a++;
    return n;
  } else {
    char c(st[a]);
    // assert(mp.find(c) != mp.end());

    set<int> n(mp[c]);
    a++;
    return n;
  }
}
set<int> expr() {
  set<int> l(exp());
  while (1) {
    if (a == st.size() || st[a] == ')') {
      a++;
      return l;
    } else {
      char op = st[a];
      assert(test.find(op) != string::npos);
      a++;
      set<int> r(exp());
      l = calc(l, r, op);
    }
  }
}
int main() {
  char c;
  int n;
  while (cin >> c >> n) {
    if (c == 'R') {
      set<int> v;
      for (auto m : mp) {
        for (auto n : m.second) {
          v.insert(n);
        }
      }
      mp['U'] = v;
      cin >> st;
      auto ans = expr();
      for (auto aa : ans) {
        cout << aa;
        if (*prev(ans.end()) == aa)
          cout << endl;
        else
          cout << " ";
      }
      if (ans.empty())
        cout << endl;
      mp.clear();
      a = 0;
    } else {
      for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        mp[c].emplace(a);
      }
    }
  }
  return 0;
}