Problem p02667 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (300006)
string A;
ll n, ans;
int main() {
  FAST cin >> A;
  n = A.size();
  ll zero = count(all(A), '0');
  vector<char> v(n, '*');
  ll pos = 0;
  FOR(i, 0, n - 1) {
    if (A[i] == '0') {
      v[pos++] = A[i];
    } else {
      if (pos > 0 && v[pos - 1] == A[i]) {
        --pos;
        ans += zero;
      } else {
        v[pos++] = A[i];
      }
    }
  }
  ll sofar = 0;
  FOR(i, 0, pos - 1) if (v[i] == '1') {
    auto getOdd = [&](ll l, ll r) {
      if (l % 2 == 0)
        ++l;
      if (r % 2 == 0)
        --r;
      return l > r ? 0 : (r - l + 2) / 2;
    };
    ++sofar;
    ans += zero - getOdd(sofar, i);
  }
  ll one = count(all(A), '1');
  FOR(i, 1, one) { ans += (i + 1) / 2; }
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? llabs(b) : gcd(b % a, a);
}

using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;

#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (300006)
ll ans;
string A;
struct node {
  int s, e, m;
  pi v;
  ll add;
  bool flip;
  node *l, *r;
  node(int S, int E) {
    s = S, e = E, m = (s + e) >> 1;
    v.f = -LLINF, add = 0, v.s = e;
    flip = 0;
    if (s ^ e)
      l = new node(s, m), r = new node(m + 1, e);
  }
  void update(int x, int y, ll nval) {
    value();
    if (s == x && e == y) {
      add += nval;
      return;
    }
    if (x > m)
      r->update(x, y, nval);
    else if (y <= m)
      l->update(x, y, nval);
    else
      l->update(x, m, nval), r->update(m + 1, y, nval);
    v = max(l->value(), r->value());
  }
  pi value() {
    if (v.f == -LLINF) {
      add = flip = 0;
      return v;
    }
    // flip b4 u add
    if (flip)
      v.f *= -1;
    v.f += add;
    if (s ^ e) {
      l->flip ^= flip, l->add *= flip ? -1 : 1;
      r->flip ^= flip, r->add *= flip ? -1 : 1;
      l->add += add, r->add += add;
    }
    add = 0;
    flip = 0;
    return v;
  }
  void set(int x, ll nval) {
    value();
    if (s == e) {
      v.f = nval;
      return;
    }
    if (x > m)
      r->set(x, nval);
    else
      l->set(x, nval);
    v = max(l->value(), r->value());
  }
  void change(int x, int y) {
    value();
    if (s == x && e == y) {
      flip ^= 1;
      return;
    }
    if (x > m)
      r->change(x, y);
    else if (y <= m)
      l->change(x, y);
    else
      l->change(x, m), r->change(m + 1, y);
    v = max(l->value(), r->value());
  }
  pi rmq(int x, int y) {
    value();
    if (s == x && e == y) {
      return v;
    }
    if (x > m)
      return r->rmq(x, y);
    else if (y <= m)
      return l->rmq(x, y);
    else
      return max(l->rmq(x, m), r->rmq(m + 1, y));
  }
} *seg;
int main() {
  FAST cin >> A;
  if (count(all(A), '1') == 0) {
    assert(0);
    cout << 0 << '\\n';
    return 0;
  }
  seg = new node(0, siz(A) - 1);
  ll ones = 0, ofs = 0, lo = -1;
  DEC(i, siz(A) - 1, 0) if (A[i] == '0') {
    if (ones == 0)
      seg->set(i, 0);
    else {
      ofs += i % 2 == 0;
      seg->set(i, ofs);
    }
  }
  else ++ones, lo = max(lo, i);
  FOR(i, 0, siz(A) - 1) {
    ll ofs = 0, zeros = 0;
    FOR(i, 0, siz(A) - 1)
    if (A[i] == '0' && seg->rmq(i, i).f != -LLINF) ofs += (i - zeros) % 2 == 0;
    else if (A[i] == '0')++ zeros;
    ans += (siz(A) - i + 1) / 2 - ofs;
    pi place = seg->value();
    if (place.f == -LLINF) {
      assert(ofs == 0);
      continue;
    } else {
      if (place.s)
        seg->update(0, place.s - 1, -place.f);
      if (place.s != siz(A) - 1)
        seg->change(place.s + 1, siz(A) - 1);
      seg->set(place.s, -LLINF);
    }
  }
  cout << ans << '\\n';
}