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

#include <iostream>
#include <map>
#include <stack>
#include <stdlib.h>
#include <vector>
#define f first
#define s second
using namespace std;

int main() {
  int m, n, a, b;
  string s, ans = "correct";
  map<string, pair<int, int>> M;
  stack<vector<int>> S;

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> a >> b;
    M[s].f = a;
    M[s].s = b;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s == "+" || s == "-" || s == "*" || s == "/") {
      vector<int> V1 = S.top();
      S.pop();
      vector<int> V2 = S.top();
      S.pop();
      vector<int> V3;
      bool flag[256];
      for (int i = 0; i < 256; i++)
        flag[i] = false;
      if (s == "+") {
        for (int i = 0; i < V1.size(); i++) {
          for (int j = 0; j < V2.size(); j++) {
            int c = (V2[j] + V1[i]) % 256;
            if (flag[c])
              continue;
            V3.push_back(c);
            flag[c] = true;
          }
        }
      } else if (s == "-") {
        for (int i = 0; i < V1.size(); i++) {
          for (int j = 0; j < V2.size(); j++) {
            int c = (V2[j] - V1[i] + 256) % 256;
            if (flag[c])
              continue;
            V3.push_back(c);
            flag[c] = true;
          }
        }
      } else if (s == "*") {
        for (int i = 0; i < V1.size(); i++) {
          for (int j = 0; j < V2.size(); j++) {
            int c = (V2[j] * V1[i]) % 256;
            if (flag[c])
              continue;
            V3.push_back(c);
            flag[c] = true;
          }
        }
      } else {
        for (int i = 0; i < V1.size(); i++) {
          for (int j = 0; j < V2.size(); j++) {
            if (V1[i] == 0) {
              ans = "error";
              break;
            }
            int c = (V2[j] / V1[i]) % 256;
            if (flag[c])
              continue;
            V3.push_back(c);
            flag[c] = true;
          }
        }
      }
      S.push(V3);
    } else {
      pair<int, int> p = M[s];
      vector<int> V;
      if (p.f != 0 || p.s != 0) {
        for (int i = p.f; i <= p.s; i++)
          V.push_back(i);
      } else {
        V.push_back(atoi(s.c_str()));
      }
      S.push(V);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <stack>
#include <stdlib.h>
#include <vector>
#define f first
#define s second
using namespace std;

int main() {
  int m, n, a, b;
  string s, ans = "correct";
  map<string, pair<int, int>> M;
  stack<vector<int>> S;

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> a >> b;
    M[s].f = a;
    M[s].s = b;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s == "+" || s == "-" || s == "*" || s == "/") {
      vector<int> V1 = S.top();
      S.pop();
      vector<int> V2 = S.top();
      S.pop();
      vector<int> V3;
      if (s == "+") {
        for (int i = 0; i < V1.size(); i++) {
          for (int j = 0; j < V2.size(); j++) {
            int c = (V2[j] + V1[i]) % 256;
            for (int k = 0; k < V3.size(); k++)
              if (V3[k] == c)
                continue;
            V3.push_back(c);
          }
        }
      } else if (s == "-") {
        for (int i = 0; i < V1.size(); i++) {
          for (int j = 0; j < V2.size(); j++) {
            int c = (V2[j] - V1[i] + 256) % 256;
            for (int k = 0; k < V3.size(); k++)
              if (V3[k] == c)
                continue;
            V3.push_back(c);
          }
        }
      } else if (s == "*") {
        for (int i = 0; i < V1.size(); i++) {
          for (int j = 0; j < V2.size(); j++) {
            int c = (V2[j] * V1[i]) % 256;
            for (int k = 0; k < V3.size(); k++)
              if (V3[k] == c)
                continue;
            V3.push_back(c);
          }
        }
      } else {
        for (int i = 0; i < V1.size(); i++) {
          for (int j = 0; j < V2.size(); j++) {
            if (V1[i] == 0) {
              ans = "error";
              continue;
            }
            int c = (V2[j] / V1[i]) % 256;
            // cout<<c<<endl;
            for (int k = 0; k < V3.size(); k++)
              if (V3[k] == c)
                continue;
            V3.push_back(c);
          }
        }
      }
      S.push(V3);
    } else {
      pair<int, int> p = M[s];
      vector<int> V;
      if (p.f != 0 && p.s != 0) {
        for (int i = p.f; i <= p.s; i++)
          V.push_back(i);
      } else
        V.push_back(atoi(s.c_str()));
      S.push(V);
    }
  }
  cout << ans << endl;
  return 0;
}