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

#include <cassert>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  string s;
  cin >> s;
  int cnt_ones = 0;
  for (auto c : s) {
    cnt_ones += (c == '1');
  }
  int cnt_zeroes = s.size() - cnt_ones;
  long long ans = 0;
  while (cnt_ones > 0) {
    ans += (cnt_ones + 1) / 2;
    cnt_ones--;
  }
  vector<pair<char, int>> comp;
  for (auto c : s) {
    if (comp.empty() || c != comp.back().first) {
      comp.emplace_back(c, 1);
    } else {
      comp.back().second++;
    }
  }
  for (auto seg : comp) {
    if (seg.first == '1') {
      int whole = (seg.second / 2);
      ans += 1LL * whole * cnt_zeroes;
    }
  }
  vector<pair<char, int>> new_comp;
  for (auto seg : comp) {
    if (seg.first == '1') {
      seg.second %= 2;
    }
    if (seg.second > 0) {
      if (new_comp.empty() || new_comp.back().first != seg.first) {
        new_comp.push_back(seg);
      } else {
        new_comp.back().second += seg.second;
      }
    }
  }
  if (new_comp.empty()) {
    cout << ans << endl;
    return 0;
  }
  if (new_comp[0].first == '1') {
    new_comp.insert(new_comp.begin(), make_pair('0', 0));
  }
  if (new_comp.back().first == '1') {
    new_comp.emplace_back('0', 0);
  }
  int n = new_comp.size() / 2;
  vector<int> zs;
  for (int i = 0; i < new_comp.size(); i += 2) {
    zs.push_back(new_comp[i].second);
  }
  assert(zs.size() == n + 1);
  vector<int> pref(n + 1);
  pref[0] = zs[0];
  for (int i = 1; i < n + 1; i++) {
    pref[i] = pref[i - 1] + zs[i];
  }
  vector<int> suf(n + 1);
  suf[n] = zs[n];
  for (int i = n - 1; i >= 0; i--) {
    suf[i] = suf[i + 1] + zs[i];
  }
  for (int i = 0; i < n; i++) {
    ans += suf[i + 1];
    int pos = pref[i] + i;
    ans += (pref[i] + !(pos & 1)) / 2;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <cassert>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  string s;
  cin >> s;
  int cnt_ones = 0;
  for (auto c : s) {
    cnt_ones += (c == '1');
  }
  int cnt_zeroes = s.size() - cnt_ones;
  long long ans = 0;
  while (cnt_ones > 0) {
    ans += (cnt_ones + 1) / 2;
    cnt_ones--;
  }
  vector<pair<char, int>> comp;
  for (auto c : s) {
    if (comp.empty() || c != comp.back().first) {
      comp.emplace_back(c, 1);
    } else {
      comp.back().second++;
    }
  }
  for (auto seg : comp) {
    if (seg.first == '1') {
      int whole = (seg.second / 2);
      ans += 1LL * whole * cnt_zeroes;
    }
  }
  vector<pair<char, int>> new_comp;
  for (auto seg : comp) {
    if (seg.first == '1') {
      seg.second %= 2;
    }
    if (seg.second > 0) {
      if (new_comp.empty() || new_comp.back().first != seg.first) {
        new_comp.push_back(seg);
      } else {
        new_comp.back().second += seg.second;
      }
    }
  }
  if (new_comp[0].first == '1') {
    new_comp.insert(new_comp.begin(), make_pair('0', 0));
  }
  if (new_comp.back().first == '1') {
    new_comp.emplace_back('0', 0);
  }
  int n = new_comp.size() / 2;
  vector<int> zs;
  for (int i = 0; i < new_comp.size(); i += 2) {
    zs.push_back(new_comp[i].second);
  }
  assert(zs.size() == n + 1);
  vector<int> pref(n + 1);
  pref[0] = zs[0];
  for (int i = 1; i < n + 1; i++) {
    pref[i] = pref[i - 1] + zs[i];
  }
  vector<int> suf(n + 1);
  suf[n] = zs[n];
  for (int i = n - 1; i >= 0; i--) {
    suf[i] = suf[i + 1] + zs[i];
  }
  for (int i = 0; i < n; i++) {
    ans += suf[i + 1];
    ans += pref[i] / 2;
  }
  cout << ans << endl;
}