Problem p01549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, m;
  while (cin >> n) {
    map<string, vector<int>> v;
    stack<vector<int>> s;
    bool ans = true;
    for (int i = 0; i < n; i++) {
      string str;
      int a, b;
      cin >> str >> a >> b;
      for (int j = a; j <= b; j++)
        v[str].push_back(j);
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      string str;
      cin >> str;
      if (!ans)
        continue;
      if (str[0] >= '0' && str[0] <= '9') {
        vector<int> v2(1, 0);
        for (int j = 0; j < str.size(); j++) {
          v2[0] *= 10;
          v2[0] += str[j] - '0';
        }
        s.push(v2);
      } else if (str.size() == 1 && (str[0] == '+' || str[0] == '-' ||
                                     str[0] == '*' || str[0] == '/')) {
        vector<int> a, b, c;
        b = s.top();
        s.pop();
        a = s.top();
        s.pop();
        for (int j = 0; j < a.size(); j++) {
          for (int k = 0; k < b.size(); k++) {
            int res;
            if (str[0] == '+')
              res = (a[j] + b[k]) % 256;
            else if (str[0] == '-')
              res = (a[j] - b[k] + 256) % 256;
            else if (str[0] == '*')
              res = (a[j] * b[k]) % 256;
            else if (b[k] != 0)
              res = (a[j] / b[k]) % 256;
            else {
              ans = false;
              break;
            }
            vector<int>::iterator it = lower_bound(c.begin(), c.end(), res);
            if (c.empty() || *it != res)
              c.insert(it, res);
          }
          if (!ans)
            break;
        }
        s.push(c);
      } else
        s.push(v[str]);
    }
    cout << (ans ? "correct" : "error") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, m;
  while (cin >> n) {
    map<string, vector<int>> v;
    stack<vector<int>> s;
    bool ans = true;
    for (int i = 0; i < n; i++) {
      string str;
      int a, b;
      cin >> str >> a >> b;
      for (int j = a; j <= b; j++)
        v[str].push_back(j);
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      string str;
      cin >> str;
      if (!ans)
        continue;
      if (str[0] >= '0' && str[0] <= '9') {
        vector<int> v2(1, 0);
        for (int j = 0; j < str.size(); j++) {
          v2[0] *= 10;
          v2[0] += str[j] - '0';
        }
        s.push(v2);
      } else if (str.size() == 1 && (str[0] == '+' || str[0] == '-' ||
                                     str[0] == '*' || str[0] == '/')) {
        vector<int> a, b, c;
        b = s.top();
        s.pop();
        a = s.top();
        s.pop();
        for (int j = 0; j < a.size(); j++) {
          for (int k = 0; k < b.size(); k++) {
            int res;
            if (str[0] == '+')
              res = a[j] + b[k];
            else if (str[0] == '-')
              res = a[j] - b[k];
            else if (str[0] == '*')
              res = a[j] * b[k];
            else if (b[k] != 0)
              res = a[j] / b[k];
            else {
              ans = false;
              break;
            }
            vector<int>::iterator it = lower_bound(c.begin(), c.end(), res);
            if (c.empty() || *it != res)
              c.insert(it, res);
          }
          if (!ans)
            break;
        }
        s.push(c);
      } else
        s.push(v[str]);
    }
    cout << (ans ? "correct" : "error") << endl;
  }
}