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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
char s[maxn];
int n;
int L[maxn], R[maxn], par[maxn];
bool check(int mid) {
  for (int i = 0; i <= n; ++i)
    L[i] = R[i] = par[i] = 0;
  L[n] = 0;
  R[n] = mid;
  for (int i = n - 1; ~i; --i) {
    if (s[i + 1] == '0') {
      L[i] = L[i + 1] + 1;
      R[i] = min(mid, R[i + 1] + 1);
      if (par[i + 1] && R[i + 1] == mid)
        R[i] = min(R[i], mid - 1);
    } else if (s[i + 1] == '1') {
      L[i] = max(0, L[i + 1] - 1);
      R[i] = R[i + 1] - 1;
      if (par[i + 1] && L[i + 1] == 0)
        L[i] = max(L[i], 1);
    } else {
      L[i] = max(0, L[i + 1] - 1);
      R[i] = min(mid, R[i + 1] + 1);
      if (par[i + 1]) {
        if (L[i + 1] == 0)
          L[i] = max(L[i], 1);
        if (R[i + 1] == mid)
          R[i] = min(R[i], mid - 1);
      }
    }
    if (L[i] > R[i])
      return 0;
    if (L[i] == R[i] || par[i + 1])
      par[i] = 1;
  }
  return 1;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int l = 1, r = n;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\\n", l);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ls (rt << 1)
#define rs (rt << 1 | 1)
const int maxn = 1e6 + 10;
char s[maxn];
int n, mid, Min[maxn << 2], Max[maxn << 2], Add[maxn << 2], sum[maxn];
void pushup(int rt) {
  Min[rt] = min(Min[ls], Min[rs]);
  Max[rt] = max(Max[ls], Max[rs]);
}
void lazy(int rt, int x) {
  Min[rt] += x;
  Max[rt] += x;
  Add[rt] += x;
}
void pushdown(int rt) {
  if (Add[rt]) {
    lazy(ls, Add[rt]);
    lazy(rs, Add[rt]);
    Add[rt] = 0;
  }
}
void build(int rt, int l, int r) {
  Add[rt] = 0;
  if (l == r) {
    Min[rt] = Max[rt] = 2 * sum[l] - l;
    return;
  }
  int mid = l + r >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
  pushup(rt);
}
void update(int rt, int l, int r, int a, int b) {
  if (a <= l && r <= b) {
    lazy(rt, 2);
    return;
  }
  pushdown(rt);
  int mid = l + r >> 1;
  if (a <= mid)
    update(ls, l, mid, a, b);
  if (b > mid)
    update(rs, mid + 1, r, a, b);
  pushup(rt);
}
int queryMin(int rt, int l, int r, int a, int b) {
  if (a <= l && r <= b)
    return Min[rt];
  pushdown(rt);
  int mid = l + r >> 1;
  if (b <= mid)
    return queryMin(ls, l, mid, a, b);
  else if (a > mid)
    return queryMin(rs, mid + 1, r, a, b);
  else
    return min(queryMin(ls, l, mid, a, b), queryMin(rs, mid + 1, r, a, b));
}
int queryMax(int rt, int l, int r, int a, int b) {
  if (a <= l && r <= b)
    return Max[rt];
  pushdown(rt);
  int mid = l + r >> 1;
  if (b <= mid)
    return queryMax(ls, l, mid, a, b);
  else if (a > mid)
    return queryMax(rs, mid + 1, r, a, b);
  else
    return max(queryMax(ls, l, mid, a, b), queryMax(rs, mid + 1, r, a, b));
}
bool check() {
  build(1, 0, n);
  stack<int> sta;
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '?')
      sta.push(i);
    if (queryMax(1, 0, n, 0, i - 1) - mid > queryMax(1, 0, n, i, i)) {
      if (sta.empty())
        return 0;
      int pos = sta.top();
      sta.pop();
      update(1, 0, n, pos, n);
    }
    if (queryMin(1, 0, n, 0, i) + mid < queryMax(1, 0, n, 0, i))
      return 0;
  }
  return 1;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; ++i)
    sum[i] = sum[i - 1] + (s[i] == '1');
  int l = 1, r = n;
  while (l <= r) {
    mid = l + r >> 1;
    if (check())
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\\n", l);
  return 0;
}