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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

void unq(vi &v) {
  sort(ALL(v));
  auto it = unique(ALL(v));
  v.erase(it, v.end());
}

int main() {
  int m, n;
  unordered_map<string, vi> mp;
  cin >> m;
  REP(i, m) {
    string c;
    int a, b;
    cin >> c >> a >> b;
    vi v;
    FOR(i, a, b + 1) v.push_back(i);
    mp[c] = v;
  }
  stack<vi> st;
  cin >> n;
  bool f = true;
  REP(i, n) {
    string s;
    cin >> s;
    if (s == "+") {
      vi a = st.top();
      st.pop();
      vi b = st.top();
      st.pop();
      vi v;
      REP(i, b.size()) REP(j, a.size()) v.push_back((b[i] + a[j]) % 256);
      unq(v);
      st.push(v);
    } else if (s == "-") {
      vi a = st.top();
      st.pop();
      vi b = st.top();
      st.pop();
      vi v;
      REP(i, b.size()) REP(j, a.size()) v.push_back((b[i] - a[j] + 256) % 256);
      unq(v);
      st.push(v);
    } else if (s == "*") {
      vi a = st.top();
      st.pop();
      vi b = st.top();
      st.pop();
      vi v;
      REP(i, b.size()) REP(j, a.size()) v.push_back((b[i] * a[j]) % 256);
      unq(v);
      st.push(v);
    } else if (s == "/") {
      vi a = st.top();
      st.pop();
      vi b = st.top();
      st.pop();
      vi v;
      REP(i, b.size()) REP(j, a.size()) {
        if (a[j] == 0) {
          f = false;
          goto end;
        } else
          v.push_back((b[i] / a[j]) % 256);
      }
      unq(v);
      st.push(v);
    } else if (isalpha(s[0])) {
      st.push(mp[s]);
    } else {
      int n = stoi(s);
      vi v = {n};
      st.push(v);
    }
  }
end:;
  puts(f ? "correct" : "error");
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

void unq(vi &v) {
  sort(ALL(v));
  auto it = unique(ALL(v));
  v.erase(it, v.end());
}

int main() {
  int m, n;
  unordered_map<char, vi> mp;
  cin >> m;
  REP(i, m) {
    char c;
    int a, b;
    cin >> c >> a >> b;
    vi v;
    FOR(i, a, b + 1) v.push_back(i);
    mp[c] = v;
  }
  stack<vi> st;
  cin >> n;
  bool f = true;
  REP(i, n) {
    string s;
    cin >> s;
    if (s == "+") {
      vi a = st.top();
      st.pop();
      vi b = st.top();
      st.pop();
      vi v;
      REP(i, b.size()) REP(j, a.size()) v.push_back((b[i] + a[j]) % 256);
      unq(v);
      st.push(v);
    } else if (s == "-") {
      vi a = st.top();
      st.pop();
      vi b = st.top();
      st.pop();
      vi v;
      REP(i, b.size()) REP(j, a.size()) v.push_back((b[i] - a[j]) % 256);
      unq(v);
      st.push(v);
    } else if (s == "*") {
      vi a = st.top();
      st.pop();
      vi b = st.top();
      st.pop();
      vi v;
      REP(i, b.size()) REP(j, a.size()) v.push_back((b[i] * a[j]) % 256);
      unq(v);
      st.push(v);
    } else if (s == "/") {
      vi a = st.top();
      st.pop();
      vi b = st.top();
      st.pop();
      vi v;
      REP(i, b.size()) REP(j, a.size()) {
        if (a[j] == 0) {
          f = false;
          goto end;
        } else
          v.push_back((b[i] / a[j]) % 256);
      }
      unq(v);
      st.push(v);
    } else if (isalpha(s[0])) {
      st.push(mp[s[0]]);
    } else {
      int n = stoi(s);
      vi v = {n};
      st.push(v);
    }
  }
end:;
  puts(f ? "correct" : "error");
  return 0;
}