Problem p00598 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

string e;
vector<int> sets[256];
vector<int> calculate(char ope, const vector<int> &left,
                      const vector<int> &right) {
  vector<int> res(left.size() + right.size());
  vector<int>::iterator end;
  if (ope == 'u')
    end = set_union(all(left), all(right), res.begin());
  else if (ope == 'i')
    end = set_intersection(all(left), all(right), res.begin());
  else if (ope == 'd')
    end = set_difference(all(left), all(right), res.begin());
  else if (ope == 's')
    end = set_symmetric_difference(all(left), all(right), res.begin());

  res.erase(end, res.end());
  return res;
}
vector<int> parse(int a, int b) {
  int p = b;
  for (int par = 0; p >= a; --p) {
    if (e[p] == '(')
      ++par;
    else if (e[p] == ')')
      --par;
    else if (par == 0 && strchr("uids", e[p]))
      break;
  }
  if (p < a) {
    if (e[a] == 'c')
      return calculate('s', sets['U'], parse(a + 1, b));
    else if (e[a] == '(')
      return parse(a + 1, b - 1);
    else
      return sets[e[a]];
  }

  return calculate(e[p], parse(a, p - 1), parse(p + 1, b));
}
int main() {
  while (true) {
    rep(i, 256) sets[i].clear();

    vector<int> universe;
    while (true) {
      char a;
      int n;
      if (!(cin >> a >> n))
        return 0;

      if (a == 'R')
        break;

      sets[a] = vector<int>();
      while (n--) {
        int t;
        cin >> t;
        sets[a].push_back(t);
        universe.push_back(t);
      }
      sort(all(sets[a]));
    }
    sort(all(universe));
    universe.erase(unique(all(universe)), universe.end());
    sets['U'] = universe;

    cin >> e;
    vector<int> res = parse(0, e.size() - 1);
    if (res.empty())
      cout << "NULL" << endl;
    else {
      for (int i = 0; i < res.size(); ++i) {
        cout << res[i];
        if (i + 1 < res.size())
          cout << ' ';
      }
      cout << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

// map<char, vector<int> > sets;
vector<int> sets[256];
vector<int> calculate(char ope, const vector<int> &left,
                      const vector<int> &right) {
  vector<int> res(left.size() + right.size());
  vector<int>::iterator end;
  if (ope == 'u')
    end = set_union(all(left), all(right), res.begin());
  else if (ope == 'i')
    end = set_intersection(all(left), all(right), res.begin());
  else if (ope == 'd')
    end = set_difference(all(left), all(right), res.begin());
  else if (ope == 's')
    end = set_symmetric_difference(all(left), all(right), res.begin());

  res.erase(end, res.end());
  return res;
}
// vector<int> parse(const string& e)
//{
//	int p = e.size() - 1;
//	for (int par = 0; p >= 0; --p)
//	{
//		if (e[p] == '(')
//			++par;
//		else if (e[p] == ')')
//			--par;
//		else if (par == 0 && strchr("uids", e[p]))
//			break;
//	}
//	if (p < 0)
//	{
//		if (e[0] == 'c')
//			return calculate('s', sets['U'], parse(e.substr(1)));
//		else if (e[0] == '(')
//			return parse(e.substr(1, e.size() - 2));
//		else
//			return sets[e[0]];
//	}
//
//	return calculate(e[p], parse(e.substr(0, p)), parse(e.substr(p + 1,
//e.size() - (p + 1))));
// }
string e;
vector<int> parse(int a, int b) {
  int p = b;
  for (int par = 0; p >= a; --p) {
    if (e[p] == '(')
      ++par;
    else if (e[p] == ')')
      --par;
    else if (par == 0 && strchr("uids", e[p]))
      break;
  }
  if (p < a) {
    if (e[a] == 'c')
      return calculate('s', sets['U'], parse(a + 1, b));
    else if (e[a] == '(')
      return parse(a + 1, b - 1);
    else
      return sets[e[a]];
  }

  return calculate(e[p], parse(a, p - 1), parse(p + 1, b));
}
int main() {
  while (!cin.eof()) {
    // sets.clear();
    rep(i, 256) sets[i].clear();

    vector<int> universe;
    while (true) {
      char a;
      int n;
      cin >> a >> n;
      if (a == 'R')
        break;

      sets[a] = vector<int>();
      while (n--) {
        int t;
        cin >> t;
        sets[a].push_back(t);
        universe.push_back(t);
      }
      sort(all(sets[a]));
    }
    sort(all(universe));
    universe.erase(unique(all(universe)), universe.end());
    sets['U'] = universe;

    cin >> e;
    vector<int> res = parse(0, e.size() - 1);
    if (res.empty())
      cout << "NULL" << endl;
    else {
      for (int i = 0; i < res.size(); ++i) {
        cout << res[i];
        if (i + 1 < res.size())
          cout << ' ';
      }
      cout << endl;
    }
  }
}