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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define F first
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define S second
#define ppins(a, b, c, d) PP(P(a, b), P(c, d))
#define pb push_back
#define My "NARIYOSHI_XX"
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;

bool calc(set<int> &B, set<int> &A, string &ope, stack<set<int>> &ele,
          map<string, set<int>> &index) {
  set<int> C;
  for (set<int>::iterator it_A = A.begin(); it_A != A.end(); it_A++) {
    for (set<int>::iterator it_B = B.begin(); it_B != B.end(); it_B++) {
      if (ope == "+") {
        C.insert(((*it_A) + (*it_B)) % 256);
      } else if (ope == "-") {
        C.insert(((*it_A) - (*it_B) + 256) % 256);
      } else if (ope == "*") {
        C.insert(((*it_A) * (*it_B)) % 256);
      } else if (ope == "/") {
        if (!(*it_B)) {

          return false;
        }
        C.insert(((*it_A) / (*it_B)) % 256);
      } else
        assert(false);
    }
  }
  index[My] = C;
  ele.push(C);
  return true;
}

int main() {
  int m, n;
  map<string, set<int>> index;
  stack<set<int>> ele;
  deque<string> st;
  bool fin = false;

  cin >> m;
  for (int i = 0; i < m; i++) {
    string name;
    int lb, ub;
    cin >> name >> lb >> ub;
    for (int j = lb; j <= ub; j++)
      index[name].insert(j);
  }

  cin >> n;
  for (int i = 0; i < n; i++) {
    string e;
    cin >> e;
    // cout <<"e = " << e << endl;
    if (fin)
      continue;
    if (e != "/" && e != "*" && e != "-" && e != "+") {
      if (index.find(e) != index.end()) {
        ele.push(index[e]);
      } else {
        set<int> FAGS;
        FAGS.insert((atoi)(e.c_str()));
        ele.push(FAGS);
      }
    } else {
      set<int> A, B;
      if (ele.empty()) {
        fin = true;
        continue;
      }
      B = ele.top(), ele.pop();
      if (ele.empty()) {
        fin = true;
        continue;
      }
      A = ele.top(), ele.pop();
      if (!calc(B, A, e, ele, index))
        fin = true;
    }
  }

  if (fin) {
    cout << "error" << endl;
    return 0;
  }

  cout << "correct" << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define F first
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define S second
#define ppins(a, b, c, d) PP(P(a, b), P(c, d))
#define pb push_back
#define My "NARIYOSHI_XX"
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;

bool calc(set<int> &B, set<int> &A, string ope, stack<set<int>> &ele,
          map<string, set<int>> &index) {
  set<int> C;
  for (set<int>::iterator it_B = B.begin(); it_B != B.end(); it_B++) {
    for (set<int>::iterator it_A = A.begin(); it_A != A.end(); it_A++) {
      if (ope == "+") {
        C.insert(((*it_A) + (*it_B)) % 256);
      } else if (ope == "-") {
        C.insert(((*it_A) - (*it_B) + 256) % 256);
      } else if (ope == "*") {
        C.insert(((*it_A) * (*it_B)) % 256);
      } else if (ope == "/") {
        if (!(*it_B)) {

          return false;
        }
        C.insert(((*it_A) / (*it_B)) % 256);
      } else
        assert(false);
    }
  }
  index[My] = C;
  ele.push(C);
  return true;
}

int main() {
  int m, n;
  map<string, set<int>> index;
  stack<set<int>> ele;
  deque<string> st;
  bool fin = false;

  cin >> m;
  for (int i = 0; i < m; i++) {
    string name;
    int lb, ub;
    cin >> name >> lb >> ub;
    for (int j = lb; j <= ub; j++)
      index[name].insert(j);
  }

  cin >> n;
  for (int i = 0; i < n; i++) {
    string e;
    cin >> e;

    if (e != "/" && e != "*" && e != "-" && e != "+") {
      if (index.find(e) != index.end()) {
        ele.push(index[e]);
      } else {
        set<int> FAGS;
        FAGS.insert((atoi)(e.c_str()));
        ele.push(FAGS);
      }
    } else {
      set<int> A, B;
      B = ele.top(), ele.pop();
      A = ele.top(), ele.pop();
      if (!calc(B, A, e, ele, index))
        fin = true;
    }
  }

  if (fin) {
    cout << "error" << endl;
    return 0;
  }

  cout << "correct" << endl;

  return 0;
}