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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int vari[3];
string s;

string regenerate(string t) {
  int n = t.length();
  string ret = "";
  rep(i, 0, n) {
    if (i < n - 1 && t[i] == '-' && t[i + 1] == '-')
      i++;
    else
      ret += t[i];
  }
  return ret;
}

int opnot(int m) { return 2 - m; }

vector<vector<int>> opmul{{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
vector<vector<int>> oppls{{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};

int retvari(char c) {
  string chk = "PQR";
  if (chk.find(c) != string::npos)
    return vari[chk.find(c)];
  else
    return c - '0';
}

int solve(int l, int r) {
  if (r - l == 1)
    return retvari(s[l]);
  else {
    int cnt = 0, nl = -1, nr = -1;
    int vl, vr;
    rep(i, l, r) {
      if (s[i] == '(') {
        cnt++;
      }
      if (s[i] == ')') {
        cnt--;
      }
      if (s[i] == '*' && cnt == 0) {
        vl = solve(l, i);
        vr = solve(i + 1, r);
        return opmul[vl][vr];
      }
      if (s[i] == '+' && cnt == 0) {
        vl = solve(l, i);
        vr = solve(i + 1, r);
        return oppls[vl][vr];
      }
    }
  }

  if (s[l] == '-')
    return opnot(solve(l + 1, r));
  else
    return solve(l + 1, r - 1);
}

signed main() {
  while (cin >> s, s != ".") {
    s = regenerate(s);
    int n = s.length(), cnt = 0;
    rep(i, 0, 3) rep(j, 0, 3) rep(k, 0, 3) {
      vari[0] = i, vari[1] = j, vari[2] = k;
      if (solve(0, n) == 2)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int vari[3];
string s;

string regenerate(string t) {
  int n = t.length();
  string ret = "";
  rep(i, 0, n) {
    if (i < n - 1 && t[i] == '-' && t[i + 1] == '-')
      i++;
    else
      ret += t[i];
  }
  return ret;
}

int opnot(int m) { return 2 - m; }

vector<vector<int>> opmul{{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
vector<vector<int>> oppls{{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};

int retvari(char c) {
  string chk = "PQR";
  if (chk.find(c) != string::npos)
    return vari[chk.find(c)];
  else
    return c - '0';
}

int solve(int l, int r) {
  if (r - l == 1)
    return retvari(s[l]);
  else if (s[l] == '-' && r - l == 2)
    return opnot(retvari(s[l + 1]));
  else {
    int cnt = 0, nl = -1, nr = -1;
    int vl, vr;
    rep(i, l, r) {
      if (s[i] == '(') {
        cnt++;
      }
      if (s[i] == ')') {
        cnt--;
      }
      if (s[i] == '*' && cnt == 0) {
        vl = solve(l, i);
        vr = solve(i + 1, r);
        return opmul[vl][vr];
      }
      if (s[i] == '+' && cnt == 0) {
        vl = solve(l, i);
        vr = solve(i + 1, r);
        return oppls[vl][vr];
      }
    }
  }
  return solve(l + 1, r - 1);
}

signed main() {
  while (cin >> s, s != ".") {
    s = regenerate(s);
    int n = s.length(), cnt = 0;
    rep(i, 0, 3) rep(j, 0, 3) rep(k, 0, 3) {
      vari[0] = i, vari[1] = j, vari[2] = k;
      if (solve(0, n) == 2)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}