Problem p02667 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof(x))
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define outval(x) cerr << #x " = " << x << endl
#define outv(x) cerr << #x " = " << x << "  "
#define outtag(x) cerr << "--------------" #x "---------------" << endl
#define outarr(a, L, R)                                                        \
  cerr << #a "[" << L << ".." << R << "] = ";                                  \
  For(_x, L, R) cerr << a[_x] << " ";                                          \
  cerr << endl;
#define User_Time ((double)clock() / CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector<int> vi;
typedef pair<int, int> pii;
LL read() {
  LL x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f = ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
const int N = 200005;
int n;
char s[N];
int a[N];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int cnt1 = 0;
  For(i, 1, n) {
    a[i] = s[i] == '1';
    cnt1 += a[i];
  }
  if (cnt1 == 0)
    return puts("0"), 0;
  LL ans = (LL)cnt1 / 2 * (cnt1 / 2 + 1);
  if (cnt1 & 1)
    ans += (cnt1 + 1) / 2;
  int cnt0 = n - cnt1;
  //	outval(ans);outval(cnt0);
  vi v;
  int c0 = 0, c1 = 0;
  int p0 = 0, p1 = 0;
  For(i, 1, n) if (a[i] == 0) {
    if (c1) {
      v.pb(c0), c1 = c0 = 0;
      if ((i - 1) & 1)
        p1++;
      else
        p0++;
    }
    c0++;
  }
  else {
    if (c1)
      ans += cnt0;
    c1 ^= 1;
  }
  v.pb(c0);
  if (c1) {
    v.pb(0);
    if (n & 1)
      p1++;
    else
      p0++;
  }
  if (v.size() == 1)
    return printf("%lld\n", ans), 0;
  //	outval(ans);
  //	outval(p0),outval(p1);
  //	outarr(v,0,(int)v.size()-1);
  while (v[0] > 0) {
    ans += p1;
    swap(p0, p1);
    v[0]--;
  }
  //	outval(ans);
  For(i, 1, (int)v.size() - 1) {
    p1--;
    while (v[i] > 1) {
      ans += i;
      ans += p1;
      swap(p0, p1);
      v[i]--;
    }
  }
  //	outtag();
  //	outval(p0),outval(p1);
  //	outval(ans);
  //	outarr(v,0,(int)v.size()-1);
  int flag = 0;
  if (v.back() == 0) {
    if ((int)v.size() == 2)
      return printf("%lld\n", ans);
    ans += (int)v.size() - 1;
    v.pop_back();
    flag = 1;
  }
  int vsz = (int)v.size() - 1;
  For(i, 0, vsz - 1 - flag) ans += (vsz + (vsz - i) + 1) / 2;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof(x))
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define outval(x) cerr << #x " = " << x << endl
#define outv(x) cerr << #x " = " << x << "  "
#define outtag(x) cerr << "--------------" #x "---------------" << endl
#define outarr(a, L, R)                                                        \
  cerr << #a "[" << L << ".." << R << "] = ";                                  \
  For(_x, L, R) cerr << a[_x] << " ";                                          \
  cerr << endl;
#define User_Time ((double)clock() / CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector<int> vi;
typedef pair<int, int> pii;
LL read() {
  LL x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f = ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
const int N = 200005;
int n;
char s[N];
int a[N];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int cnt1 = 0;
  For(i, 1, n) {
    a[i] = s[i] == '1';
    cnt1 += a[i];
  }
  if (cnt1 == 0)
    return puts("0"), 0;
  LL ans = (LL)cnt1 / 2 * (cnt1 / 2 + 1);
  if (cnt1 & 1)
    ans += (cnt1 + 1) / 2;
  int cnt0 = n - cnt1;
  //	outval(ans);outval(cnt0);
  vi v;
  int c0 = 0, c1 = 0;
  int p0 = 0, p1 = 0;
  For(i, 1, n) if (a[i] == 0) {
    if (c1) {
      v.pb(c0), c1 = c0 = 0;
      if ((i - 1) & 1)
        p1++;
      else
        p0++;
    }
    c0++;
  }
  else {
    if (c1)
      ans += cnt0;
    c1 ^= 1;
  }
  v.pb(c0);
  if (c1) {
    v.pb(0);
    if (n & 1)
      p1++;
    else
      p0++;
  }
  if (v.size() == 1)
    return printf("%lld\n", ans), 0;
  //	outval(ans);
  //	outval(p0),outval(p1);
  //	outarr(v,0,(int)v.size()-1);
  while (v[0] > 0) {
    ans += p1;
    swap(p0, p1);
    v[0]--;
  }
  //	outval(ans);
  For(i, 1, (int)v.size() - 1) {
    p1--;
    while (v[i] > 1) {
      ans += i;
      ans += p1;
      swap(p0, p1);
      v[i]--;
    }
  }
  //	outtag();
  //	outval(p0),outval(p1);
  //	outval(ans);
  //	outarr(v,0,(int)v.size()-1);
  int flag = 0;
  if (v.back() == 0) {
    if ((int)v.size() == 2)
      return printf("%lld\n", ans), 0;
    ans += (int)v.size() - 1;
    v.pop_back();
    flag = 1;
  }
  int vsz = (int)v.size() - 1;
  For(i, 0, vsz - 1 - flag) ans += (vsz + (vsz - i) + 1) / 2;
  cout << ans << endl;
  return 0;
}