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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

enum OpeType_t {
  OPE_ADD_VAR,
  OPE_ADD_CON,
  OPE_SUB_VAR,
  OPE_SUB_CON,
  OPE_SET_VAR,
  OPE_SET_CON,
  OPE_IF,
  OPE_HALT
};

int main() {
  int n;
  cin >> n;

  int m = 0;
  int maxLoop = n;
  vector<OpeType_t> opeType(n);
  vector<vector<int>> arg(n);
  vector<int> varIndex(128, -1);
  vector<int> lineIndex(1001, -1);
  for (int i = 0; i < n; ++i) {
    int line;
    string opeStr;
    cin >> line >> opeStr;
    lineIndex[line] = i;

    int argNum;
    if (opeStr == "ADD") {
      opeType[i] = OPE_ADD_VAR;
      argNum = 3;
    } else if (opeStr == "SUB") {
      opeType[i] = OPE_SUB_VAR;
      argNum = 3;
    } else if (opeStr == "SET") {
      opeType[i] = OPE_SET_VAR;
      argNum = 2;
    } else if (opeStr == "IF") {
      opeType[i] = OPE_IF;
      argNum = 2;
    } else {
      opeType[i] = OPE_HALT;
      argNum = 0;
    }

    arg[i].resize(argNum);
    for (int j = 0; j < argNum; ++j) {
      string s;
      cin >> s;
      if (isdigit(s[0])) {
        arg[i][j] = stoi(s);
        if (opeType[i] != OPE_IF)
          opeType[i] = (OpeType_t)((int)opeType[i] + 1);
      } else {
        if (varIndex[s[0]] == -1) {
          varIndex[s[0]] = m;
          ++m;
          maxLoop *= 16;
        }
        arg[i][j] = varIndex[s[0]];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (opeType[i] == OPE_IF)
      arg[i][1] = lineIndex[arg[i][1]];
  }

  int curr = 0;
  vector<int> var(m, 0);
  while (--maxLoop >= 0) {
    bool isEnd = false;
    vector<int> prevVar = var;

    switch (opeType[curr]) {
    case OPE_ADD_VAR:
      var[arg[curr][0]] = var[arg[curr][1]] + var[arg[curr][2]];
      ++curr;
      break;
    case OPE_ADD_CON:
      var[arg[curr][0]] = var[arg[curr][1]] + arg[curr][2];
      ++curr;
      break;
    case OPE_SUB_VAR:
      var[arg[curr][0]] = var[arg[curr][1]] - var[arg[curr][2]];
      ++curr;
      break;
    case OPE_SUB_CON:
      var[arg[curr][0]] = var[arg[curr][1]] - arg[curr][2];
      ++curr;
      break;
    case OPE_SET_VAR:
      var[arg[curr][0]] = var[arg[curr][1]];
      ++curr;
      break;
    case OPE_SET_CON:
      var[arg[curr][0]] = arg[curr][1];
      ++curr;
      break;
    case OPE_IF:
      if (var[arg[curr][0]] == 0)
        ++curr;
      else
        curr = arg[curr][1];
      break;
    case OPE_HALT:
      isEnd = true;
      break;
    }

    if (!(0 <= curr && curr < n))
      isEnd = true;
    for (int i = 0; i < m; ++i) {
      if (!(0 <= var[i] && var[i] < 16)) {
        var[i] = prevVar[i];
        isEnd = true;
      }
    }
    if (isEnd) {
      for (int i = 0; i < 128; ++i) {
        if (varIndex[i] != -1)
          cout << (char)i << '=' << var[varIndex[i]] << endl;
      }
      return 0;
    }
  }

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

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

enum OpeType_t {
  OPE_ADD_VAR,
  OPE_ADD_CON,
  OPE_SUB_VAR,
  OPE_SUB_CON,
  OPE_SET_VAR,
  OPE_SET_CON,
  OPE_IF,
  OPE_HALT
};

int main() {
  int n;
  cin >> n;

  int m = 0;
  int maxLoop = n;
  vector<OpeType_t> opeType(n);
  vector<vector<int>> arg(n);
  vector<int> varIndex(128, -1);
  vector<int> lineIndex(1001, -1);
  for (int i = 0; i < n; ++i) {
    int line;
    string opeStr;
    cin >> line >> opeStr;
    lineIndex[line] = i;

    int argNum;
    if (opeStr == "ADD") {
      opeType[i] = OPE_ADD_VAR;
      argNum = 3;
    } else if (opeStr == "SUB") {
      opeType[i] = OPE_SUB_VAR;
      argNum = 3;
    } else if (opeStr == "SET") {
      opeType[i] = OPE_SET_VAR;
      argNum = 2;
    } else if (opeStr == "IF") {
      opeType[i] = OPE_IF;
      argNum = 2;
    } else {
      opeType[i] = OPE_HALT;
      argNum = 0;
    }

    arg[i].resize(argNum);
    for (int j = 0; j < argNum; ++j) {
      string s;
      cin >> s;
      if (isdigit(s[0])) {
        arg[i][j] = stoi(s);
        if (opeType[i] != OPE_IF)
          opeType[i] = (OpeType_t)((int)opeType[i] + 1);
      } else {
        if (varIndex[s[0]] == -1) {
          varIndex[s[0]] = m;
          ++m;
          maxLoop *= 16;
        }
        arg[i][j] = varIndex[s[0]];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (opeType[i] == OPE_IF)
      arg[i][1] = lineIndex[arg[i][1]];
  }

  int curr = 0;
  vector<int> var(m, 0);
  while (--maxLoop >= 0) {
    bool isEnd = false;
    vector<int> var2 = var;

    switch (opeType[curr]) {
    case OPE_ADD_VAR:
      var[arg[curr][0]] = var[arg[curr][1]] + var[arg[curr][2]];
      ++curr;
      break;
    case OPE_ADD_CON:
      var[arg[curr][0]] = var[arg[curr][1]] + arg[curr][2];
      ++curr;
      break;
    case OPE_SUB_VAR:
      var[arg[curr][0]] = var[arg[curr][1]] - var[arg[curr][2]];
      ++curr;
      break;
    case OPE_SUB_CON:
      var[arg[curr][0]] = var[arg[curr][1]] - arg[curr][2];
      ++curr;
      break;
    case OPE_SET_VAR:
      var[arg[curr][0]] = var[arg[curr][1]];
      ++curr;
      break;
    case OPE_SET_CON:
      var[arg[curr][0]] = arg[curr][1];
      ++curr;
      break;
    case OPE_IF:
      if (var[arg[curr][0]] == 0)
        ++curr;
      else
        curr = arg[curr][1];
      break;
    case OPE_HALT:
      isEnd = true;
      break;
    }

    if (n <= curr)
      isEnd = true;
    for (int i = 0; i < m; ++i) {
      if (!(0 <= var[i] && var[i] < 16)) {
        var[i] = var2[i];
        isEnd = true;
      }
    }
    if (isEnd) {
      for (int i = 0; i < 128; ++i) {
        if (varIndex[i] != -1)
          cout << (char)i << '=' << var[varIndex[i]] << endl;
      }
      return 0;
    }
  }

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