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

#include <bits/stdc++.h>
using namespace std;
int N;
int s[100];
char t[100][10];
int u[100], v[100], w[100];

int num[1000];

int s2i(string str) {
  stringstream ss;
  ss << str;
  int res;
  ss >> res;
  return res;
}

int change(int x) {
  if (0 <= x) {
    return x;
  } else {
    return num[-x];
  }
}

void setvalue(int x, int y) {
  assert(x < 0);
  num[-x] = y;
}

int main() {

  set<char> st;
  map<int, int> lp;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &s[i]);
    assert(lp.count(s[i]) == 0);
    lp[s[i]] = i;

    scanf("%s", t[i]);
    string a, b, c;

    if (t[i][0] == 'A') {
      cin >> a >> b >> c;
      if ('a' <= a[0] && a[0] <= 'z')
        u[i] = -1 * (int)a[0], st.insert(a[0]);
      else
        assert(0);

      if ('a' <= b[0] && b[0] <= 'z')
        v[i] = -1 * (int)b[0], st.insert(b[0]);
      else
        v[i] = s2i(b);

      if ('a' <= c[0] && c[0] <= 'z')
        w[i] = -1 * (int)c[0], st.insert(c[0]);
      else
        w[i] = s2i(c);

    } else if (t[i][0] == 'S' && t[i][1] == 'U') {
      cin >> a >> b >> c;
      if ('a' <= a[0] && a[0] <= 'z')
        u[i] = -1 * (int)a[0], st.insert(a[0]);
      else
        assert(0);

      if ('a' <= b[0] && b[0] <= 'z')
        v[i] = -1 * (int)b[0], st.insert(b[0]);
      else
        v[i] = s2i(b);

      if ('a' <= c[0] && c[0] <= 'z')
        w[i] = -1 * (int)c[0], st.insert(c[0]);
      else
        w[i] = s2i(c);

    } else if (t[i][0] == 'S' && t[i][1] == 'E') {
      cin >> a >> b;
      if ('a' <= a[0] && a[0] <= 'z')
        u[i] = -1 * (int)a[0], st.insert(a[0]);
      else
        assert(0);

      if ('a' <= b[0] && b[0] <= 'z')
        v[i] = -1 * (int)b[0], st.insert(b[0]);
      else
        v[i] = s2i(b);

    } else if (t[i][0] == 'I') {
      cin >> a >> b;
      if ('a' <= a[0] && a[0] <= 'z')
        u[i] = -1 * (int)a[0], st.insert(a[0]);
      else
        assert(0);

      if ('a' <= b[0] && b[0] <= 'z')
        assert(0);
      else
        v[i] = s2i(b);

    } else if (t[i][0] == 'H') {

    } else
      assert(0);
  }

  int mx = 60000000;
  int p = 0, cc = 0;
  while (cc < mx) {

    if (t[p][0] == 'A') {
      int value = change(v[p]) + change(w[p]);
      if (value < 0 || 16 <= value)
        break;
      setvalue(u[p], value);
      p++;
    } else if (t[p][0] == 'S' && t[p][1] == 'U') {
      int value = change(v[p]) - change(w[p]);
      if (value < 0 || 16 <= value)
        break;
      setvalue(u[p], value);
      p++;
    } else if (t[p][0] == 'S' && t[p][1] == 'E') {
      int value = change(v[p]);
      if (value < 0 || 16 <= value)
        break;
      setvalue(u[p], value);
      p++;
    } else if (t[p][0] == 'I') {
      int value = change(u[p]);
      if (value > 0) {
        int next = change(v[p]);
        if (lp.count(next) == 0)
          break;
        p = lp[next];
      } else {
        p++;
      }

    } else if (t[p][0] == 'H') {
      break;
    } else
      assert(0);

    cc++;
    if (p == N)
      break;
  }

  if (cc < mx) {
    set<char>::iterator it;
    for (it = st.begin(); it != st.end(); it++) {
      int f = *it;
      printf("%c=%d\\n", *it, change(-f));
    }
  } else {
    printf("inf\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int N;
int s[100];
string t[100], u[100], v[100], w[100];

map<int, int> used;
map<string, int> mp;
set<string> st;

int change(string str) {
  if ('0' <= str[0] && str[0] <= '9') {
  } else {
    st.insert(str);
  }

  if (mp.count(str))
    return mp[str];
  stringstream ss;
  ss << str;
  int res;
  ss >> res;
  return res;
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    cin >> s[i] >> t[i];

    used[s[i]] = i;
    if (t[i] == "ADD" || t[i] == "SUB") {
      cin >> u[i] >> v[i] >> w[i];
      change(u[i]);
      change(v[i]);
      change(w[i]);
    } else if (t[i] == "SET" || t[i] == "IF") {
      cin >> u[i] >> v[i];
      change(u[i]);
      change(v[i]);
    } else if (t[i] == "HALT") {
    }
  }

  int p = 0, cc = 0;
  while (cc < 10000000 && p < N) {
    cc++;

    if (t[p] == "SET") {
      int value = change(v[p]);
      if (value < 0 || 16 <= value)
        break;
      mp[u[p]] = value;
      p++;

    } else if (t[p] == "ADD") {
      int value = change(v[p]) + change(w[p]);
      if (value < 0 || 16 <= value)
        break;
      mp[u[p]] = value;
      p++;

    } else if (t[p] == "SUB") {
      int value = change(v[p]) - change(w[p]);
      if (value < 0 || 16 <= value)
        break;
      mp[u[p]] = value;
      p++;

    } else if (t[p] == "IF") {

      if (change(u[p])) {
        if (used.count(change(v[p]))) {
          p = used[change(v[p])];
        } else
          break;

      } else
        p++;

    } else if (t[p] == "HALT") {
      break;
    }
  }

  if (cc < 10000000) {

    set<string>::iterator it;
    for (it = st.begin(); it != st.end(); it++)
      cout << *it << '=' << mp[*it] << endl;

  } else {
    cout << "inf" << endl;
  }
  return 0;
}