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

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

struct Op {
  string op;
  string v[3];
};

int getValue(vector<int> &state, int id) {
  if (state[id] == -1)
    return state[id] = 0;
  return state[id];
}

int main() {
  int n;
  cin >> n;
  map<string, int> mp;
  vector<Op> vec;
  vector<int> state(26, -1);
  rep(i, n) {
    string line;
    cin >> line;
    mp[line] = i;
    Op op;
    cin >> op.op;
    if (op.op == "ADD" || op.op == "SUB")
      rep(j, 3) {
        cin >> op.v[j];
        if ('a' <= op.v[j][0] && op.v[j][0] <= 'z')
          state[op.v[j][0] - 'a'] = 0;
      }
    else if (op.op == "SET" || op.op == "IF")
      rep(j, 2) {
        cin >> op.v[j];
        if ('a' <= op.v[j][0] && op.v[j][0] <= 'z')
          state[op.v[j][0] - 'a'] = 0;
      }
    vec.push_back(op);
  }
  // set<vector<int>> used[n];
  auto t1 = chrono::system_clock::now();
  int cur = 0;
  while (1) {
    if (cur >= n) {
      break;
    }
    /*
    if( used[cur].count(state) ) {
      puts("inf");
      return 0;
    }
    used[cur].insert(state);
    */
    auto t2 = chrono::system_clock::now();
    auto diff = t2 - t1;
    auto milli = chrono::duration_cast<chrono::milliseconds>(t2 - t1);
    if (milli.count() > 3000) {
      puts("inf");
      return 0;
    }

    if (vec[cur].op == "ADD") {
      if ('a' <= vec[cur].v[2][0] && vec[cur].v[2][0] <= 'z') {
        int var1 = vec[cur].v[0][0] - 'a';
        int var2 = vec[cur].v[1][0] - 'a';
        int var3 = vec[cur].v[2][0] - 'a';
        getValue(state, var1);
        int tmp = state[var1];
        state[var1] = getValue(state, var2) + getValue(state, var3);
        if (state[var1] < 0 || state[var1] >= 16) {
          state[var1] = tmp;
          break;
        }
      } else {
        int var1 = vec[cur].v[0][0] - 'a';
        int var2 = vec[cur].v[1][0] - 'a';
        int con = atoi(vec[cur].v[2].c_str());
        getValue(state, var1);
        int tmp = state[var1];
        state[var1] = getValue(state, var2) + con;
        if (state[var1] < 0 || state[var1] >= 16) {
          state[var1] = tmp;
          break;
        }
      }
    } else if (vec[cur].op == "SUB") {
      if ('a' <= vec[cur].v[2][0] && vec[cur].v[2][0] <= 'z') {
        int var1 = vec[cur].v[0][0] - 'a';
        int var2 = vec[cur].v[1][0] - 'a';
        int var3 = vec[cur].v[2][0] - 'a';
        getValue(state, var1);
        int tmp = state[var1];
        state[var1] = getValue(state, var2) - getValue(state, var3);
        if (state[var1] < 0 || state[var1] >= 16) {
          state[var1] = tmp;
          break;
        }
      } else {
        int var1 = vec[cur].v[0][0] - 'a';
        int var2 = vec[cur].v[1][0] - 'a';
        int con = atoi(vec[cur].v[2].c_str());
        getValue(state, var1);
        int tmp = state[var1];
        state[var1] = getValue(state, var2) - con;
        if (state[var1] < 0 || state[var1] >= 16) {
          state[var1] = tmp;
          break;
        }
      }
    } else if (vec[cur].op == "SET") {
      if ('a' <= vec[cur].v[1][0] && vec[cur].v[1][0] <= 'z') {
        int var1 = vec[cur].v[0][0] - 'a';
        int var2 = vec[cur].v[1][0] - 'a';
        getValue(state, var1);
        state[var1] = getValue(state, var2);
      } else {
        int var1 = vec[cur].v[0][0] - 'a';
        int con = atoi(vec[cur].v[1].c_str());
        getValue(state, var1);
        int tmp = state[var1];
        state[var1] = con;
        if (state[var1] < 0 || state[var1] >= 16) {
          state[var1] = tmp;
          break;
        }
      }
    } else if (vec[cur].op == "IF") {
      int var = vec[cur].v[0][0] - 'a';
      if (getValue(state, var)) {
        string dest = vec[cur].v[1];
        if (!mp.count(dest))
          break;
        cur = mp[dest];
        continue;
      }
    } else {
      break;
    }
    ++cur;
  }
  rep(i, 26) {
    if (state[i] >= 0) {
      cout << (char)('a' + i) << "=" << state[i] << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

struct Op {
  string op;
  string v[3];
};

int getValue(vector<int> &state, int id) {
  if (state[id] == -1)
    return state[id] = 0;
  return state[id];
}

int main() {
  int n;
  cin >> n;
  map<string, int> mp;
  vector<Op> vec;
  vector<int> state(26, -1);
  rep(i, n) {
    string line;
    cin >> line;
    mp[line] = i;
    Op op;
    cin >> op.op;
    if (op.op == "ADD" || op.op == "SUB")
      rep(j, 3) {
        cin >> op.v[j];
        if ('a' <= op.v[j][0] && op.v[j][0] <= 'z')
          state[op.v[j][0] - 'a'] = 0;
      }
    else if (op.op == "SET" || op.op == "IF")
      rep(j, 2) {
        cin >> op.v[j];
        if ('a' <= op.v[j][0] && op.v[j][0] <= 'z')
          state[op.v[j][0] - 'a'] = 0;
      }
    vec.push_back(op);
  }
  set<vector<int>> used[n];

  int cur = 0;
  while (1) {
    if (cur >= n) {
      break;
    }
    if (used[cur].count(state)) {
      puts("inf");
      return 0;
    }
    used[cur].insert(state);

    if (vec[cur].op == "ADD") {
      if ('a' <= vec[cur].v[2][0] && vec[cur].v[2][0] <= 'z') {
        int var1 = vec[cur].v[0][0] - 'a';
        int var2 = vec[cur].v[1][0] - 'a';
        int var3 = vec[cur].v[2][0] - 'a';
        getValue(state, var1);
        int tmp = state[var1];
        state[var1] = getValue(state, var2) + getValue(state, var3);
        if (state[var1] < 0 || state[var1] >= 16) {
          state[var1] = tmp;
          break;
        }
      } else {
        int var1 = vec[cur].v[0][0] - 'a';
        int var2 = vec[cur].v[1][0] - 'a';
        int con = atoi(vec[cur].v[2].c_str());
        getValue(state, var1);
        int tmp = state[var1];
        state[var1] = getValue(state, var2) + con;
        if (state[var1] < 0 || state[var1] >= 16) {
          state[var1] = tmp;
          break;
        }
      }
    } else if (vec[cur].op == "SUB") {
      if ('a' <= vec[cur].v[2][0] && vec[cur].v[2][0] <= 'z') {
        int var1 = vec[cur].v[0][0] - 'a';
        int var2 = vec[cur].v[1][0] - 'a';
        int var3 = vec[cur].v[2][0] - 'a';
        getValue(state, var1);
        int tmp = state[var1];
        state[var1] = getValue(state, var2) - getValue(state, var3);
        if (state[var1] < 0 || state[var1] >= 16) {
          state[var1] = tmp;
          break;
        }
      } else {
        int var1 = vec[cur].v[0][0] - 'a';
        int var2 = vec[cur].v[1][0] - 'a';
        int con = atoi(vec[cur].v[2].c_str());
        getValue(state, var1);
        int tmp = state[var1];
        state[var1] = getValue(state, var2) - con;
        if (state[var1] < 0 || state[var1] >= 16) {
          state[var1] = tmp;
          break;
        }
      }
    } else if (vec[cur].op == "SET") {
      if ('a' <= vec[cur].v[1][0] && vec[cur].v[1][0] <= 'z') {
        int var1 = vec[cur].v[0][0] - 'a';
        int var2 = vec[cur].v[1][0] - 'a';
        getValue(state, var1);
        state[var1] = getValue(state, var2);
      } else {
        int var1 = vec[cur].v[0][0] - 'a';
        int con = atoi(vec[cur].v[1].c_str());
        getValue(state, var1);
        int tmp = state[var1];
        state[var1] = con;
        if (state[var1] < 0 || state[var1] >= 16) {
          state[var1] = tmp;
          break;
        }
      }
    } else if (vec[cur].op == "IF") {
      int var = vec[cur].v[0][0] - 'a';
      if (getValue(state, var)) {
        string dest = vec[cur].v[1];
        if (!mp.count(dest))
          break;
        cur = mp[dest];
        continue;
      }
    } else {
      break;
    }
    ++cur;
  }
  rep(i, 26) {
    if (state[i] >= 0) {
      cout << (char)('a' + i) << "=" << state[i] << endl;
    }
  }
  return 0;
}