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

// by yjz
#include <bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#ifndef LOCAL
#define cerr                                                                   \\
  if (0)                                                                       \\
  cout
#endif
typedef long long ll;
// My i/o stream
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len)
      return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\\n')
      c = get();
    if (it > 0)
      it--;
    return c != EOF;
  }
} _buff;
#define geti(x) x = getnum()
#define getii(x, y) geti(x), geti(y)
#define getiii(x, y, z) getii(x, y), geti(z)
#define puti(x) putnum(x), putchar(' ')
#define putii(x, y) puti(x), puti(y)
#define putiii(x, y, z) putii(x, y), puti(z)
#define putsi(x) putnum(x), putchar('\\n')
#define putsii(x, y) puti(x), putsi(y)
#define putsiii(x, y, z) putii(x, y), putsi(z)
inline ll getnum() {
  ll r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9'))
    c = _buff.get();
  if (c == '-')
    ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9')
    r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T> inline void putnum(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x)
    a[sz++] = x % 10, x /= 10;
  if (sz == 0)
    putchar('0');
  for (int i = sz - 1; i >= 0; i--)
    putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32)
    c = _buff.get();
  return c;
}

const int mod = 1e9 + 7;
ll qpow(ll x, ll k) {
  return k == 0 ? 1
                : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;
}

const int maxn = 4000111;
struct node {
  ll mn, mx, lz;
} a[maxn];
#define ls p << 1
#define rs p << 1 | 1
void update(int p) {
  a[p].mx = max(a[ls].mx, a[rs].mx);
  a[p].mn = min(a[ls].mn, a[rs].mn);
}
void pushdown(int p) {
  if (a[p].lz) {
    a[ls].mx += a[p].lz;
    a[ls].mn += a[p].lz;
    a[ls].lz += a[p].lz;
    a[rs].mx += a[p].lz;
    a[rs].mn += a[p].lz;
    a[rs].lz += a[p].lz;
    a[p].lz = 0;
  }
}
void add(int x, int y, ll v, int l, int r, int p) {
  if (x <= l && r <= y) {
    a[p].mx += v;
    a[p].mn += v;
    a[p].lz += v;
    return;
  }
  pushdown(p);
  int m = l + r >> 1;
  if (x <= m)
    add(x, y, v, l, m, ls);
  if (m < y)
    add(x, y, v, m + 1, r, rs);
  update(p);
}
int query(int x, int y, int l, int r, int p) {
  if (x <= l && r <= y)
    return a[p].mx;
  pushdown(p);
  int m = l + r >> 1;
  if (x <= m && m < y)
    return max(query(x, y, l, m, ls), query(x, y, m + 1, r, rs));
  else if (x <= m)
    return query(x, y, l, m, ls);
  else
    return query(x, y, m + 1, r, rs);
}
char s[1000111];
int n;
set<int> st, st2;
const ll B = 1e9;
int ans = 1e9;
void upd() {
  ans = min(ans, (int)(a[1].mx / B - a[1].mn / B));
  //	printf("%d %s\\n", (int)(a[1].mx/B-a[1].mn/B), s+1);
}
int get() { return min(ans, (int)(a[1].mx / B - a[1].mn / B)); }
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int c = n;
  add(0, 0, n * B, 0, n, 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      c++;
    else
      c--;
    if (s[i] == '?')
      st.insert(i);
    add(i, i, c * B + i, 0, n, 1);
  }
  upd();
  int MX = a[1].mx / B;
  vector<int> v;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '?') {
      add(i, n, 2 * B, 0, n, 1);
      if (a[1].mx / B > MX) {
        add(i, n, -2 * B, 0, n, 1);
      } else
        v.PB(i);
    }
  }
  upd();
  for (auto x : v)
    add(x, n, -2 * B, 0, n, 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '?') {
      add(i, n, 2 * B, 0, n, 1);
      if (a[1].mx / B > MX + 1) {
        add(i, n, -2 * B, 0, n, 1);
      }
    }
  }
  upd();
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

// by yjz
#include <bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#ifndef LOCAL
#define cerr                                                                   \\
  if (0)                                                                       \\
  cout
#endif
typedef long long ll;
// My i/o stream
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len)
      return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\\n')
      c = get();
    if (it > 0)
      it--;
    return c != EOF;
  }
} _buff;
#define geti(x) x = getnum()
#define getii(x, y) geti(x), geti(y)
#define getiii(x, y, z) getii(x, y), geti(z)
#define puti(x) putnum(x), putchar(' ')
#define putii(x, y) puti(x), puti(y)
#define putiii(x, y, z) putii(x, y), puti(z)
#define putsi(x) putnum(x), putchar('\\n')
#define putsii(x, y) puti(x), putsi(y)
#define putsiii(x, y, z) putii(x, y), putsi(z)
inline ll getnum() {
  ll r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9'))
    c = _buff.get();
  if (c == '-')
    ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9')
    r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T> inline void putnum(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x)
    a[sz++] = x % 10, x /= 10;
  if (sz == 0)
    putchar('0');
  for (int i = sz - 1; i >= 0; i--)
    putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32)
    c = _buff.get();
  return c;
}

const int mod = 1e9 + 7;
ll qpow(ll x, ll k) {
  return k == 0 ? 1
                : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;
}

const int maxn = 4000111;
struct node {
  ll mn, mx, lz;
} a[maxn];
#define ls p << 1
#define rs p << 1 | 1
void update(int p) {
  a[p].mx = max(a[ls].mx, a[rs].mx);
  a[p].mn = min(a[ls].mn, a[rs].mn);
}
void pushdown(int p) {
  if (a[p].lz) {
    a[ls].mx += a[p].lz;
    a[ls].mn += a[p].lz;
    a[ls].lz += a[p].lz;
    a[rs].mx += a[p].lz;
    a[rs].mn += a[p].lz;
    a[rs].lz += a[p].lz;
    a[p].lz = 0;
  }
}
void add(int x, int y, ll v, int l, int r, int p) {
  if (x <= l && r <= y) {
    a[p].mx += v;
    a[p].mn += v;
    a[p].lz += v;
    return;
  }
  pushdown(p);
  int m = l + r >> 1;
  if (x <= m)
    add(x, y, v, l, m, ls);
  if (m < y)
    add(x, y, v, m + 1, r, rs);
  update(p);
}
int query(int x, int y, int l, int r, int p) {
  if (x <= l && r <= y)
    return a[p].mx;
  pushdown(p);
  int m = l + r >> 1;
  if (x <= m && m < y)
    return max(query(x, y, l, m, ls), query(x, y, m + 1, r, rs));
  else if (x <= m)
    return query(x, y, l, m, ls);
  else
    return query(x, y, m + 1, r, rs);
}
char s[1000111];
int n;
set<int> st, st2;
const ll B = 1e9;
int ans = 1e9;
void upd() {
  ans = min(ans, (int)(a[1].mx / B - a[1].mn / B));
  //	printf("%d %s\\n", (int)(a[1].mx/B-a[1].mn/B), s+1);
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int c = n;
  add(0, 0, n * B, 0, n, 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      c++;
    else
      c--;
    if (s[i] == '?')
      st.insert(i);
    add(i, i, c * B + i, 0, n, 1);
    s[i] = '0';
  }
  upd();
  while (true) {
    ll t = a[1].mn;
    int x = t % B;
    //		cerr<<"x="<<x<<endl;
    auto it = st.lower_bound(x + 1);
    if (it == st.begin())
      break;
    it--;
    int p = *it;
    add(p, n, 2 * B, 0, n, 1);
    s[p] = '1';
    st2.insert(p);
    st.erase(p);

    auto it2 = st2.lower_bound(max(x, p) + 1);
    //		cerr<<"p="<<p<<endl;
    if (it2 != st2.end()) {
      int q = *it2;
      add(q, n, -2 * B, 0, n, 1);
      st2.erase(q);
      st.insert(q);
      s[q] = '0';
      //			cerr<<"q="<<q<<endl;
    }
    upd();
  }
  cout << ans << endl;
  return 0;
}