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

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    REP(WW, n) cerr << v[WW] << ' ';                                           \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, WW, HH)                                                      \\
  {                                                                            \\
    REP(W_, WW) {                                                              \\
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

string str;
ll n, ans, place;

ll check(int l, int r, vector<ll> &v, vector<char> op) {
  ll ret = 0;
  ll tmp = 0;

  for (int i = l; i <= r; i++) {
    if (i == l) {
      tmp = v[i];
    } else {
      if (op[i - 1] == '+') {
        ret += tmp;
        tmp = v[i];
      } else {
        tmp *= v[i];
      }
    }
    if (ret + tmp > INF)
      return INF + 1;
  }
  return ret + tmp;
}

ll dfs() {
  place++;
  vector<ll> v;
  vector<char> op;
  vector<ll> mul;
  vector<ll> it;
  vector<ll> sum(1, 0);
  while (1) {
    if (str[place] == '(') {
      ll tmp = dfs();
      if (tmp == 1 && op.size() > 0 && op.back() == '*' && v.back() == 1) {
        op.pop_back();
        mul.back()++;
      } else {
        v.EB(tmp);
        it.EB(sum.size() - 1);
        mul.EB(1);
      }
    }
    if (isdigit(str[place])) {
      if (str[place] == '1' && op.size() > 0 && op.back() == '*' &&
          v.back() == 1) {
        op.pop_back();
        mul.back()++;
      } else {
        v.EB(str[place] - '0');
        it.EB(sum.size() - 1);
        mul.EB(1);
      }
    } else if (str[place] == '*') {
      op.EB(str[place]);
    } else if (str[place] == '+') {
      op.EB(str[place]);
      sum.EB(0);
    } else {
      break;
    }
    place++;
  }
  place++;
  int r = 1;
  ll now = v[0];
  sum[0] = v[0];
  // cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
  // SHOW1d(v,v.size());
  // SHOW1d(op,op.size());
  // SHOW1d(mul,mul.size());
  // cout << "now " << now << endl;

  REP(i, v.size()) {
    if (i > r) {
      now = v[i];
      sum[it[i]] = v[i];
      r = i + 1;
    }
    // cout << "now " << now << endl;
    while (now < n && r < v.size() &&
           now + max(sum[it[r]] * v[r], v[r]) - sum[it[r]] <= n) {
      now -= sum[it[r]];
      sum[it[r]] = max(sum[it[r]] * v[r], v[r]);
      now += sum[it[r]];
      r++;
      if (now == n)
        break;
    }
    // cout << " " << i << " " << sum[it[i]] << " " << r << " " << sum[it[r-1]]
    // << " " << now << endl; SHOW1d(sum,sum.size());
    if (now == n) {
      if (n == 1)
        ans += max(mul[i] * (mul[i] + 1) / 2, (ll)1);
      else if (it[i] == it[r - 1])
        ans += mul[i];
      else
        ans += mul[i] * mul[r - 1];
      if (r < v.size() && op[r - 1] == '*' && v[r] == 1)
        ans += mul[i] * mul[r];
    }
    now -= sum[it[i]];
    if (it[i] != it[i + 1] || i + 1 == r)
      sum[it[i]] = 0;
    else
      sum[it[i]] /= v[i];
    now += sum[it[i]];
    // cout << "ans " << ans << endl;
  }
  return check(0, v.size() - 1, v, op);
}

int main() {

  while (cin >> n, n) {
    ans = 0;
    place = 0;
    cin >> str;
    str = "(" + str + ")";
    dfs();
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    REP(WW, n) cerr << v[WW] << ' ';                                           \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, WW, HH)                                                      \\
  {                                                                            \\
    REP(W_, WW) {                                                              \\
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

string str;
ll n, ans, place;

ll check(int l, int r, vector<int> &v, vector<char> op) {
  ll ret = 0;
  ll tmp = 0;

  for (int i = l; i <= r; i++) {
    if (i == l) {
      tmp = v[i];
    } else {
      if (op[i - 1] == '+') {
        ret += tmp;
        tmp = v[i];
      } else {
        tmp *= v[i];
      }
    }
    if (ret + tmp > INF)
      return INF + 1;
  }
  return ret + tmp;
}

ll dfs() {
  place++;
  vector<int> v;
  vector<char> op;
  vector<ll> mul;
  while (1) {
    if (str[place] == '(') {
      v.EB(dfs());
      mul.EB(1);
    }
    if (isdigit(str[place])) {
      if (str[place] == '1' && op.size() > 0 && op.back() == '*') {
        op.pop_back();
        mul.back()++;
      } else {
        v.EB(str[place] - '0');
        mul.EB(1);
      }
    } else if (str[place] == ')') {
      break;
    } else {
      op.EB(str[place]);
    }
    place++;
  }
  place++;
  int r = 0;
  REP(i, v.size()) {
    while (r < v.size() - 1 && check(i, r, v, op) < n)
      r++;
    if (check(i, r, v, op) == n) {
      if (i == r)
        ans += mul[i];
      else
        ans += mul[i] * mul[r];
    }
  }
  return check(0, v.size() - 1, v, op);
}

int main() {

  while (cin >> n, n) {
    ans = 0;
    place = 0;
    cin >> str;
    str = "(" + str + ")";
    dfs();
    cout << ans << endl;
  }

  return 0;
}