Problem p02954 (C++)

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

#define range 524289
#define mod 1000000007
#define eps 1e-9
#define PI 3.14159265358979323846
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define ALL(V) V.begin(), V.end()
#define _ << " " <<

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<int, pair<int, int>> iii;
typedef vector<ii> vii;
typedef vector<iii> viii;

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, l;
  string a;
  cin >> a;
  n = a.size();
  int rl = 0;

  int rc = 1;
  int lc = 0;
  int turns = 0;
  char curr = 'R';
  int count = 0;
  vi res(n, 0);
  int i = 1;
  int rstart = 0;
  int lstart = 0;
  while (i < n) {
    if (a[i] == curr) {
      if (curr == 'R') {
        rc++;
      } else {
        lc++;
      }
    } else {
      if (a[i] == 'L') {
        lstart = i;
        curr = 'L';
        lc++;
      } else {
        int lolr = rc + rstart - 1;
        int loll = lstart;
        int trns = max(lc, rc) - 1;
        turns = max(trns, turns);
        int sum = lc + rc;
        if (sum % 2) {
          if (lc > rc) {
            res[loll] = sum / 2 + sum % 2;
            res[lolr] = sum / 2;
          } else {
            res[lolr] = sum / 2 + sum % 2;
            res[loll] = sum / 2;
          }
        } else {
          res[loll] = sum / 2;
          res[lolr] = sum / 2;
        }

        if (trns == 0 || trns % 2 == 1) {
          swap(res[loll], res[lolr]);
        }
        rstart = i;
        lc = 0;
        rc = 1;
        curr = 'R';
      }
    }
    i++;
  }

  int lollr = rc + rstart - 1;
  int lolll = lstart;
  int trnss = max(lc, rc) - 1;
  turns = max(trnss, turns);
  int sums = lc + rc;
  if (sums % 2) {
    if (lc > rc) {
      res[lolll] = sums / 2 + sums % 2;
      res[lollr] = sums / 2;
    } else {
      res[lollr] = sums / 2 + sums % 2;
      res[lolll] = sums / 2;
    }
  } else {
    res[lolll] = sums / 2;
    res[lollr] = sums / 2;
  }
  if (trnss == 0 || trnss % 2 == 1) {
    swap(res[lolll], res[lollr]);
  }

  for (i = 0; i < n; ++i) {
    cout << res[i] << " ";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define range 524289
#define mod 1000000007
#define eps 1e-9
#define PI 3.14159265358979323846
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define ALL(V) V.begin(), V.end()
#define _ << " " <<

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<int, pair<int, int>> iii;
typedef vector<ii> vii;
typedef vector<iii> viii;

int main(int argc, char const *argv[]) {
  // #ifndef ONLINE_JUDGE
  //    	freopen("input.txt", "r", stdin);
  //    	freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, l;
  string a;
  cin >> a;
  n = a.size();
  int rl = 0;

  int rc = 1;
  int lc = 0;
  int turns = 0;
  char curr = 'R';
  int count = 0;
  vi res(n, 0);
  int i = 1;
  int rstart = 0;
  int lstart = 0;
  while (i < n) {
    if (a[i] == curr) {
      if (curr == 'R') {
        rc++;
      } else {
        lc++;
      }
    } else {
      if (a[i] == 'L') {
        lstart = i;
        curr = 'L';
        lc++;
      } else {
        int lolr = rc + rstart - 1;
        int loll = lstart;
        int trns = max(lc, rc) - 1;
        turns = max(trns, turns);
        int sum = lc + rc;
        if (sum % 2) {
          if (lc > rc) {
            res[loll] = sum / 2 + sum % 2;
            res[lolr] = sum / 2;
          } else {
            res[lolr] = sum / 2 + sum % 2;
            res[loll] = sum / 2;
          }
        } else {
          res[loll] = sum / 2;
          res[lolr] = sum / 2;
        }

        if (trns == 0 || trns % 2 == 1) {
          swap(res[loll], res[lolr]);
        }
        rstart = i;
        lc = 0;
        rc = 1;
        curr = 'R';
      }
    }
    i++;
  }

  int lollr = rc + rstart - 1;
  int lolll = lstart;
  int trnss = max(lc, rc) - 1;
  turns = max(trnss, turns);
  int sums = lc + rc;
  if (sums % 2) {
    if (lc > rc) {
      res[lolll] = sums / 2 + sums % 2;
      res[lollr] = sums / 2;
    } else {
      res[lollr] = sums / 2 + sums % 2;
      res[lolll] = sums / 2;
    }
  } else {
    res[lolll] = sums / 2;
    res[lollr] = sums / 2;
  }
  if (trnss == 0 || trnss % 2 == 1) {
    swap(res[lolll], res[lollr]);
  }

  for (i = 0; i < n; ++i) {
    cout << res[i] << " ";
  }
  return 0;
}