Problem p02954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> II;
typedef vector<II> VII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long int LL;
typedef unsigned long long int ULL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))
#define LET(x, a) __typeof(a) x(a)

#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
// Works for forward as well as backward iteration

#define gu getchar
#define pu putchar
#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)

#define DRT()                                                                  \
  int t;                                                                       \
  si(t);                                                                       \
  while (t--)

#define PlUSWRAP(index, n)                                                     \
  index = (index + 1) % n // index++; if(index>=n) index=0
#define MINUSWRAP(index, n)                                                    \
  index = (index + n - 1) % n // index--; if(index<0) index=n-1
#define ROUNDOFFINT(d)                                                         \
  d = (int)((double)d + 0.5) // Round off d to nearest integer

#define FLUSHN while (gu() != '\n')
#define FLUSHS while (gu() != ' ')

#define TRACE

#ifdef TRACE
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define off                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  off;
  int n;
  string s;
  vector<II> seg;
  cin >> s;
  n = s.length();
  char finding = 'R';
  char previous = 'R';
  int segments = 0;
  vector<int> rlboundary;
  int countR = 1;
  int countL = 0;
  rep(i, 1, n) {
    // R.R..LL...L
    if (previous == 'R') {
      if (s[i] == 'L') {
        rlboundary.PB(i - 1);
        previous = 'L';
        countL = 1;
      } else {
        countR++;
      }
    } else if (previous == 'L') {
      if (s[i] == 'R') {
        segments++;
        seg.PB(MP(countR, countL));
        countR = 1;
        countL = 0;
        previous = 'R';
      } else {
        countL++;
      }
    }
  }
  seg.PB(MP(countR, countL));
  segments++;
  vector<int> ans(n, 0);
  // cout<<"segments "<<segments<<"\n";
  rep(i, 0, segments) {
    int r = seg[i].F;
    int l = seg[i].S;
    int su = r + l;
    if (su & 1) {
      int decide = max(r, l) - 1;
      int small = su / 2;
      int big = su - small;
      if (r > l) {
        if (decide & 1) {
          ans[rlboundary[i]] = small;
          ans[rlboundary[i] + 1] = big;
        } else {
          ans[rlboundary[i]] = big;
          ans[rlboundary[i] + 1] = small;
        }
      } else {
        if (decide % 2 == 0) {
          ans[rlboundary[i]] = small;
          ans[rlboundary[i] + 1] = big;
        } else {
          ans[rlboundary[i]] = big;
          ans[rlboundary[i] + 1] = small;
        }
      }

    } else {
      ans[rlboundary[i]] = su / 2;
      ans[rlboundary[i] + 1] = su / 2;
    }
  }
  rep(i, 0, n) cout << ans[i] << " ";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> II;
typedef vector<II> VII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long int LL;
typedef unsigned long long int ULL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))
#define LET(x, a) __typeof(a) x(a)

#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
// Works for forward as well as backward iteration

#define gu getchar
#define pu putchar
#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)

#define DRT()                                                                  \
  int t;                                                                       \
  si(t);                                                                       \
  while (t--)

#define PlUSWRAP(index, n)                                                     \
  index = (index + 1) % n // index++; if(index>=n) index=0
#define MINUSWRAP(index, n)                                                    \
  index = (index + n - 1) % n // index--; if(index<0) index=n-1
#define ROUNDOFFINT(d)                                                         \
  d = (int)((double)d + 0.5) // Round off d to nearest integer

#define FLUSHN while (gu() != '\n')
#define FLUSHS while (gu() != ' ')

#define TRACE

#ifdef TRACE
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define off                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

int main() {
  // #ifndef ONLINE_JUDGE
  //    freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  // #endif
  off;
  int n;
  string s;
  vector<II> seg;
  cin >> s;
  n = s.length();
  char finding = 'R';
  char previous = 'R';
  int segments = 0;
  vector<int> rlboundary;
  int countR = 1;
  int countL = 0;
  rep(i, 1, n) {
    // R.R..LL...L
    if (previous == 'R') {
      if (s[i] == 'L') {
        rlboundary.PB(i - 1);
        previous = 'L';
        countL = 1;
      } else {
        countR++;
      }
    } else if (previous == 'L') {
      if (s[i] == 'R') {
        segments++;
        seg.PB(MP(countR, countL));
        countR = 1;
        countL = 0;
        previous = 'R';
      } else {
        countL++;
      }
    }
  }
  seg.PB(MP(countR, countL));
  segments++;
  vector<int> ans(n, 0);
  // cout<<"segments "<<segments<<"\n";
  rep(i, 0, segments) {
    int r = seg[i].F;
    int l = seg[i].S;
    int su = r + l;
    if (su & 1) {
      int decide = max(r, l) - 1;
      int small = su / 2;
      int big = su - small;
      if (r > l) {
        if (decide & 1) {
          ans[rlboundary[i]] = small;
          ans[rlboundary[i] + 1] = big;
        } else {
          ans[rlboundary[i]] = big;
          ans[rlboundary[i] + 1] = small;
        }
      } else {
        if (decide % 2 == 0) {
          ans[rlboundary[i]] = small;
          ans[rlboundary[i] + 1] = big;
        } else {
          ans[rlboundary[i]] = big;
          ans[rlboundary[i] + 1] = small;
        }
      }

    } else {
      ans[rlboundary[i]] = su / 2;
      ans[rlboundary[i] + 1] = su / 2;
    }
  }
  rep(i, 0, n) cout << ans[i] << " ";
  return 0;
}