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

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

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

int main() {
  const LL mod = 1000000007LL;

  int pra[128] = {}, prs[128] = {};
  pra['|'] = 15;
  pra['&'] = 25;
  pra[')'] = 5;

  prs['|'] = 16;
  prs['&'] = 26;
  prs['~'] = 34;
  prs['('] = 4;

  string s;
  cin >> s;
  s += ')';

  vector<pii> v(1, pii('(', -1));
  v.reserve(s.size());

  for (size_t i = 0; i < s.size(); ++i) {
    int c = s[i];
    if (c == 'x') {
      v.push_back(pii(1, 1));
    } else if (!isdigit(c)) {
      if (c != '(' && c != '~') {
        while (v.size() > 1) {
          int op = v[v.size() - 2].first;

          if (prs[op] < pra[c]) {
            break;
          }

          pii y = v.back();
          v.pop_back();
          v.pop_back();

          if (op == '~') {
            v.push_back(pii(y.second, y.first));
          } else if (op == '&') {
            pii x = v.back();
            LL tr = (LL)x.first * y.first;
            LL fl = (LL)x.first * y.second + x.second * LL(y.first + y.second);
            v.back() = pii(tr % mod, fl % mod);
          } else if (op == '|') {
            pii x = v.back();
            LL tr = (LL)x.second * y.first + x.first * LL(y.first + y.second);
            LL fl = (LL)x.second * y.second;
            v.back() = pii(tr % mod, fl % mod);
          } else {
            throw op;
          }
        }
      }

      if (c == ')') {
        v[v.size() - 2] = v.back();
        v.pop_back();
      } else {
        v.push_back(pii(c, -1));
      }
    }
  }

  cout << v[0].first << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

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

int main() {
  const LL mod = 1000000007LL;

  int pra[128] = {}, prs[128] = {};
  pra['|'] = 15;
  pra['&'] = 25;
  pra['~'] = 35;
  pra[')'] = 5;

  prs['|'] = 16;
  prs['&'] = 26;
  prs['~'] = 34;
  prs['('] = 4;

  string s;
  cin >> s;
  s += ')';

  vector<pii> v(1, pii('(', -1));
  v.reserve(s.size());

  for (size_t i = 0; i < s.size(); ++i) {
    int c = s[i];
    if (c == 'x') {
      v.push_back(pii(1, 1));
    } else if (!isdigit(c)) {
      if (c != '(') {
        if (!pra[c])
          return 0;
        while (v.size() > 2) {
          int op = v[v.size() - 2].first;
          if (!prs[op])
            while (1)
              ;
          if (prs[op] < pra[c]) {
            break;
          }

          pii y = v.back();
          pii x = v[v.size() - 3];
          v.pop_back();
          v.pop_back();

          if (op == '~') {
            v.push_back(pii(y.second, y.first));
          } else if (op == '&') {
            LL tr = (LL)x.first * y.first;
            LL fl = (LL)x.first * y.second + x.second * LL(y.first + y.second);
            v.back() = pii(tr % mod, fl % mod);
          } else if (op == '|') {
            LL tr = x.second * y.first + x.first * LL(y.first + y.second);
            LL fl = (LL)x.second * y.second;
            v.back() = pii(tr % mod, fl % mod);
          } else {
            throw op;
          }
        }
      }

      if (c == ')') {
        v[v.size() - 2] = v.back();
        v.pop_back();
      } else {
        v.push_back(pii(c, -1));
      }
    }
  }

  cout << v[0].first << endl;
}