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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define repit(i, a)                                                            \\
  for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i)

#define all(u) (u).begin(), (u).end()
#define rall(u) (u).rbegin(), (u).rend()
#define UNIQUE(u) (u).erase(unique(all(u)), (u).end())

#define pb push_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9
#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef vector<int> vi;

bool num[128][256];
int m, n;
map<string, pair<int, int>> bound;
string op = "+-*/";

int main() {
  memset(num, 0, sizeof(num));
  cin >> m;
  rep(i, m) {
    string name;
    int lb, ub;
    cin >> name >> lb >> ub;
    bound[name] = mp(lb, ub);
  }
  cin >> n;
  stack<int> st;
  rep(i, n) {
    string e;
    cin >> e;
    if (op.find(e) == string::npos) {
      if (bound.find(e) != bound.end())
        for (int j = bound[e].first; j <= bound[e].second; j++)
          num[i][j] = true;
      else
        num[i][atoi(e.c_str()) & 255] = true;
    } else {
      int b = st.top();
      st.pop();
      int a = st.top();
      st.pop();
      if (e == "+") {
        rep(j, 256) rep(k, 256) {
          num[i][(j + k) % 256] |= num[a][j] & num[b][k];
        }
      }
      if (e == "-") {
        rep(j, 256) rep(k, 256) {
          num[i][(j - k + 256) % 256] |= num[a][j] & num[b][k];
        }
      }
      if (e == "*") {
        rep(j, 256) rep(k, 256) {
          num[i][(j * k) % 256] |= num[a][j] & num[b][k];
        }
      }
      if (e == "/") {
        if (num[b][0]) {
          cout << "error\\n";
          return 0;
        }
        rep(j, 256) repi(k, 1, 256) {
          num[i][(j / k) % 256] |= num[a][j] & num[b][k];
        }
      }
    }
    st.push(i);
  }
  cout << "correct\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define rep(i, b) for (int i = 0; i < (b); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
#define repd(i, a, b) for (int i = (a); i >= (b); i--)
#define ALL(u) (u).begin(), (u).end()
#define INF 100000000

using namespace std;
typedef long long ll;

int main() {
  int n, m;
  cin >> n;
  vector<int> lb(n), ub(n);
  map<char, int> id;
  rep(i, n) {
    char name;
    cin >> name >> lb[i] >> ub[i];
    id[name] = i;
  }
  cin >> m;
  vector<string> e(m);
  rep(i, m) cin >> e[i];

  vector<int> val(n);
  rep(i, n) val[i] = lb[i];
  bool end = false;
  int a, b;

  while (!end) {
    vector<int> stk;
    rep(j, m) {
      // rep(k, stk.size()) cout << stk[k] << " ";
      // cout << endl;
      if (e[j][0] == '+' && e[j].size() == 1) {
        b = stk.back();
        stk.pop_back();
        a = stk.back();
        stk.pop_back();
        stk.push_back((a + b) % 256);
      } else if (e[j][0] == '-' && e[j].size() == 1) {
        b = stk.back();
        stk.pop_back();
        a = stk.back();
        stk.pop_back();
        stk.push_back((a - b + 256) % 256);
      } else if (e[j][0] == '*') {
        b = stk.back();
        stk.pop_back();
        a = stk.back();
        stk.pop_back();
        stk.push_back((a * b) % 256);
      } else if (e[j][0] == '/') {
        b = stk.back();
        stk.pop_back();
        a = stk.back();
        stk.pop_back();
        if (b == 0) {
          cout << "error\\n";
          return 0;
        }
        stk.push_back((a / b) % 256);
      } else if ((e[j][0] >= '0' && e[j][0] <= '9') ||
                 (e[j].size() > 1 && (e[j][1] >= '0' && e[j][1] <= '9'))) {
        stk.push_back(atoi(e[j].c_str()));
      } else {
        stk.push_back(val[id[e[j][0]]]);
      }
    }

    val[0]++;
    int i = 0;
    while (val[i] > ub[i]) {
      if (i >= n - 1) {
        end = true;
        break;
      }
      val[i] = lb[i];
      val[i + 1]++;
      i++;
    }
  }
  cout << "correct\\n";
  return 0;
}