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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
const int mod = 1000000007;
const int N = 1000005;

char s[N];
struct node {
  LL a, b, c;
  int p;
} fuck;
struct nima {
  vector<node> t;
  bool flag;
  char c;
  nima(){};
  nima(vector<node> _t, bool _flag, char _c) { t = _t, flag = _flag, c = _c; };
};
LL sum[N];

node cal(vector<node> tr, char c, int i) {
  node tmp;
  int sz = tr.size();
  LL res = 0, fac = 1;
  sum[sz] = 1;
  for (int j = sz - 1; j >= 0; j--)
    sum[j] = (sum[j + 1] * tr[j].c) % mod;
  tmp.c = sum[0];
  if (c == '|') {
    for (int j = 0; j < sz; j++) {
      res = (res + ((((fac * tr[j].a) % mod) * sum[j + 1]) % mod)) % mod;
      fac = (fac * tr[j].b) % mod;
    }
  }
  tmp.a = res;
  tmp.b = tmp.c - tmp.a;
  tmp.b = ((tmp.b % mod) + mod) % mod;
  tmp.p = i;
  return tmp;
}

int dep;
stack<nima> ss;
vector<node> tr;
node dfs(int x, int l) {
  tr.clear();
  char c = 'a';
  bool flag = true;
  for (int i = x; i < l; i++) {
    if (s[i] == '(') {
      ss.push(nima(tr, flag, c));
      c = 'a';
      flag = true;
      tr.clear();
    } else if (s[i] == ')') {
      if (c == '&') {
        int sz = tr.size();
        //                if(sz<=1) while(1);
        tr[sz - 2].a = (tr[sz - 2].a * tr[sz - 1].a) % mod;
        tr[sz - 2].c = (tr[sz - 2].c * tr[sz - 1].c) % mod;
        tr[sz - 2].b = tr[sz - 2].c - tr[sz - 2].a;
        tr[sz - 2].b = ((tr[sz - 2].b % mod) + mod) % mod;
        tr.erase(tr.end() - 1);
      }
      c = '|';
      node tmp = cal(tr, c, i);
      nima tt = ss.top();
      tr = tt.t, flag = tt.flag, c = tt.c;
      ss.pop();
      if (!flag) {
        swap(tmp.a, tmp.b);
        flag = true;
      }
      tr.push_back(tmp);
      i = tmp.p;
    } else if (s[i] == 'x') {
      i++;
      while (i < l && s[i] >= '0' && s[i] <= '9')
        i++;
      i--;
      node tmp;
      tmp.a = tmp.b = 1;
      tmp.c = 2;
      if (!flag) {
        flag = true;
        swap(tmp.a, tmp.b);
      }
      tr.push_back(tmp);
    } else if (s[i] == '~')
      flag = !flag;
    else {
      if (c == 'a')
        c = s[i];
      else if (c == '&') {
        int sz = tr.size();
        if (sz <= 1)
          while (1)
            ;
        tr[sz - 2].a = (tr[sz - 2].a * tr[sz - 1].a) % mod;
        tr[sz - 2].c = (tr[sz - 2].c * tr[sz - 1].c) % mod;
        tr[sz - 2].b = tr[sz - 2].c - tr[sz - 2].a;
        tr[sz - 2].b = ((tr[sz - 2].b % mod) + mod) % mod;
        tr.erase(tr.end() - 1);
        c = s[i];
      } else
        c = s[i];
      if (c == '|') {
        node tmp = cal(tr, c, i);
        tr.clear();
        tr.push_back(tmp);
      }
    }
  }
  if (c == '&') {
    int sz = tr.size();
    //        if(sz<=1) while(1);
    tr[sz - 2].a = (tr[sz - 2].a * tr[sz - 1].a) % mod;
    tr[sz - 2].c = (tr[sz - 2].c * tr[sz - 1].c) % mod;
    tr[sz - 2].b = tr[sz - 2].c - tr[sz - 2].a;
    tr[sz - 2].b = ((tr[sz - 2].b % mod) + mod) % mod;
    tr.erase(tr.end() - 1);
  }
  c = '|';
  return cal(tr, c, l - 1);
}

int main() {
  //    freopen("in","r",stdin);
  while (scanf("%s", s) != EOF) {
    dep = 0;
    while (!ss.empty())
      ss.pop();
    int l = strlen(s);
    node res = dfs(0, l);
    cout << res.a << endl;
  }
  return 0;
}

// x0x1x2x3x4x5x6(x7&(x8&~x9))|(x10|x11&x12)|~x13x14x15

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
const int mod = 1000000007;
const int N = 1000005;

char s[N];
struct node {
  LL a, b, c;
  int p;
} fuck;
struct nima {
  vector<node> t;
  bool flag;
  char c;
  nima(){};
  nima(vector<node> _t, bool _flag, char _c) { t = _t, flag = _flag, c = _c; };
};
LL sum[N];

node cal(vector<node> tr, char c, int i) {
  node tmp;
  int sz = tr.size();
  LL res = 0, fac = 1;
  sum[sz] = 1;
  for (int j = sz - 1; j >= 0; j--)
    sum[j] = (sum[j + 1] * tr[j].c) % mod;
  tmp.c = sum[0];
  if (c == '|') {
    for (int j = 0; j < sz; j++) {
      res = (res + ((((fac * tr[j].a) % mod) * sum[j + 1]) % mod)) % mod;
      fac = (fac * tr[j].b) % mod;
    }
  }
  tmp.a = res;
  tmp.b = tmp.c - tmp.a;
  tmp.b = ((tmp.b % mod) + mod) % mod;
  tmp.p = i;
  return tmp;
}

int dep;
stack<nima> ss;
vector<node> tr;
node dfs(int x, int l) {
  tr.clear();
  char c = 'a';
  bool flag = true;
  for (int i = x; i < l; i++) {
    if (s[i] == '(') {
      ss.push(nima(tr, flag, c));
      tr.clear();
    } else if (s[i] == ')') {
      if (c == '&') {
        int sz = tr.size();
        if (sz <= 1)
          while (1)
            ;
        tr[sz - 2].a = (tr[sz - 2].a * tr[sz - 1].a) % mod;
        tr[sz - 2].c = (tr[sz - 2].c * tr[sz - 1].c) % mod;
        tr[sz - 2].b = tr[sz - 2].c - tr[sz - 2].a;
        tr[sz - 2].b = ((tr[sz - 2].b % mod) + mod) % mod;
        tr.erase(tr.end() - 1);
      }
      c = '|';
      node tmp = cal(tr, c, i);
      nima tt = ss.top();
      tr = tt.t, flag = tt.flag, c = tt.c;
      ss.pop();
      if (!flag) {
        swap(tmp.a, tmp.b);
        flag = true;
      }
      tr.push_back(tmp);
      i = tmp.p;
    } else if (s[i] == 'x') {
      i++;
      while (i < l && s[i] >= '0' && s[i] <= '9')
        i++;
      i--;
      node tmp;
      tmp.a = tmp.b = 1;
      tmp.c = 2;
      if (!flag) {
        flag = true;
        swap(tmp.a, tmp.b);
      }
      tr.push_back(tmp);
    } else if (s[i] == '~')
      flag = !flag;
    else {
      if (c == 'a')
        c = s[i];
      else if (c == '&') {
        int sz = tr.size();
        if (sz <= 1)
          while (1)
            ;
        tr[sz - 2].a = (tr[sz - 2].a * tr[sz - 1].a) % mod;
        tr[sz - 2].c = (tr[sz - 2].c * tr[sz - 1].c) % mod;
        tr[sz - 2].b = tr[sz - 2].c - tr[sz - 2].a;
        tr[sz - 2].b = ((tr[sz - 2].b % mod) + mod) % mod;
        tr.erase(tr.end() - 1);
        c = s[i];
      } else
        c = s[i];
      if (c == '|') {
        node tmp = cal(tr, c, i);
        tr.clear();
        tr.push_back(tmp);
      }
    }
  }
  if (c == '&') {
    int sz = tr.size();
    //        if(sz<=1) while(1);
    tr[sz - 2].a = (tr[sz - 2].a * tr[sz - 1].a) % mod;
    tr[sz - 2].c = (tr[sz - 2].c * tr[sz - 1].c) % mod;
    tr[sz - 2].b = tr[sz - 2].c - tr[sz - 2].a;
    tr[sz - 2].b = ((tr[sz - 2].b % mod) + mod) % mod;
    tr.erase(tr.end() - 1);
  }
  c = '|';
  return cal(tr, c, l - 1);
}

int main() {
  //    freopen("in","r",stdin);
  while (scanf("%s", s) != EOF) {
    dep = 0;
    while (!ss.empty())
      ss.pop();
    int l = strlen(s);
    node res = dfs(0, l);
    cout << res.a << endl;
  }
  return 0;
}

// x0x1x2x3x4x5x6(x7&(x8&~x9))|(x10|x11&x12)|~x13x14x15