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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N;
map<int, int> lines;
string op[51], v1[51], v2[51], v3[51];
int env[256];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<char> var_names;
  rep(i, N) {
    int line;
    cin >> line >> op[i];
    lines[line] = i;
    if (op[i] == "HALT") {
    } else if (op[i] == "SET" || op[i] == "IF")
      cin >> v1[i] >> v2[i];
    else
      cin >> v1[i] >> v2[i] >> v3[i];
    if (v1[i].size() && !('0' <= v1[i][0] && v1[i][0] <= '9'))
      var_names.pb(v1[i][0]);
    if (v2[i].size() && !('0' <= v2[i][0] && v2[i][0] <= '9'))
      var_names.pb(v2[i][0]);
    if (v3[i].size() && !('0' <= v3[i][0] && v3[i][0] <= '9'))
      var_names.pb(v3[i][0]);
  }
  op[N] = "HALT";
  rep(i, 256) env[i] = -1;
  for (char s : var_names)
    env[s] = 0;
  int steps = 0;
  int cur = 0;
  auto eval = [&](string s) {
    if ('0' <= s[0] && s[0] <= '9')
      return stoi(s);
    else
      return env[s[0]];
  };
  auto halt = [&]() {
    rep(c, 256) {
      if (env[c] == -1)
        continue;
      cout << (char)c << "=" << env[c] << "\\n";
    }
    exit(0);
  };
  auto assign = [&](string s, int val) {
    if (val < 0 || val >= 16)
      halt();
    env[s[0]] = val;
  };
  while (steps++ <= (1 << 20) * 30) {
    if (op[cur] == "ADD") {
      assign(v1[cur], eval(v2[cur]) + eval(v3[cur]));
    } else if (op[cur] == "SUB") {
      assign(v1[cur], eval(v2[cur]) - eval(v3[cur]));
    } else if (op[cur] == "SET") {
      assign(v1[cur], eval(v2[cur]));
    } else if (op[cur] == "IF") {
      if (lines.find(stoi(v2[cur])) == lines.end())
        halt();
      int dest = lines[stoi(v2[cur])];
      if (eval(v1[cur]) != 0) {
        cur = dest;
        continue;
      }
    } else if (op[cur] == "HALT")
      halt();
    cur++;
  }
  cout << "inf\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N;
map<int, int> lines;
string op[51], v1[51], v2[51], v3[51];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<string> var_names;
  rep(i, N) {
    int line;
    cin >> line >> op[i];
    lines[line] = i;
    if (op[i] == "HALT") {
    } else if (op[i] == "SET" || op[i] == "IF")
      cin >> v1[i] >> v2[i];
    else
      cin >> v1[i] >> v2[i] >> v3[i];
    if (!('0' <= v1[i][0] && v1[i][0] <= '9'))
      var_names.pb(v1[i]);
    if (!('0' <= v2[i][0] && v2[i][0] <= '9'))
      var_names.pb(v2[i]);
    if (!('0' <= v3[i][0] && v3[i][0] <= '9'))
      var_names.pb(v3[i]);
  }
  op[N] = "HALT";
  map<string, int> env;
  for (string s : var_names)
    if (s != "")
      env[s] = 0;
  int steps = 0;
  int cur = 0;
  auto eval = [&](string s) {
    if ('0' <= s[0] && s[0] <= '9')
      return stoi(s);
    else
      return env[s];
  };
  auto halt = [&]() {
    for (auto p : env)
      cout << p._1 << "=" << p._2 << "\\n";
    exit(0);
  };
  auto assign = [&](string s, int val) {
    if (val < 0 || val >= 16)
      halt();
    env[s] = val;
  };
  while (steps++ <= 157286400) {
    if (op[cur] == "ADD") {
      assign(v1[cur], eval(v2[cur]) + eval(v3[cur]));
    } else if (op[cur] == "SUB") {
      assign(v1[cur], eval(v2[cur]) - eval(v3[cur]));
    } else if (op[cur] == "SET") {
      assign(v1[cur], eval(v2[cur]));
    } else if (op[cur] == "IF") {
      if (lines.find(stoi(v2[cur])) == lines.end())
        halt();
      int dest = lines[stoi(v2[cur])];
      if (eval(v1[cur]) != 0) {
        cur = dest;
        continue;
      }
    } else if (op[cur] == "HALT")
      halt();
    cur++;
  }
  cout << "inf\\n";
  return 0;
}