Problem p02954 (C++)

Buggy submission (status: Runtime Error):
/*==============================================*\
ID:          shahidul_brur

Name:     Md. Shahidul Islam
Study:      CSE, BRUR
Address:  Rangpur, Bangladesh

 mail: shahidul.cse.brur@gmail.com
 FB  : fb.com/shahidul.brur
 Blog: shahidul-brur.blogspot.com(in Bengali),
       shahidul-brur-en.blogspot.com(in English)
\*===============================================*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including
// tree_order_statistics_node_update using namespace __gnu_pbds;
using namespace std;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
typedef pair<int, ll> il;
typedef vector<ii> vii;
typedef vector<il> vil;
typedef vector<li> vli;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz size()
#define all(a) a.begin(), a.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define f0(i, b) for (int i = 0; i < (b); i++)
#define f1(i, b) for (int i = 1; i <= (b); i++)
#define f2(i, a, b) for (int i = (a); i <= (b); i++)
#define fr(i, b, a) for (int i = (b); i >= (a); i--)
#define rep(i, a, b, c) for (int i = (a); i != (b); i += (c))

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};

const double pi = acos(-1.0);
const double eps = 1e-6;
const int mod = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
const int logn = 20;
int c[maxn], n, last[maxn];
string s;

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin >> s;
  n = s.size();
  f0(i, n) {
    if (s[i] == 'R') {
      int j = i + 1;
      while (s[j] != 'L')
        ++j;
      f2(k, i, j - 1) last[k] = j;
      i = j;
    }
  }
  fr(i, n - 1, 0) {
    if (s[i] == 'L') {
      int j = i - 1;
      while (s[j] != 'R')
        --j;
      f2(k, j + 1, i) last[k] = j;
      i = j;
    }
  }
  f0(i, n) {
    if (abs(i - last[i]) % 2 == 0)
      c[last[i]]++;
    else {
      if (s[i] == 'L')
        c[last[i] + 1]++;
      else
        c[last[i] - 1]++;
    }
  }
  f0(i, n) cout << c[i] << " ";
  return 0;
}


Fixed submission:
/*==============================================*\
ID:          shahidul_brur

Name:     Md. Shahidul Islam
Study:      CSE, BRUR
Address:  Rangpur, Bangladesh

 mail: shahidul.cse.brur@gmail.com
 FB  : fb.com/shahidul.brur
 Blog: shahidul-brur.blogspot.com(in Bengali),
       shahidul-brur-en.blogspot.com(in English)
\*===============================================*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including
// tree_order_statistics_node_update using namespace __gnu_pbds;
using namespace std;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;

// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,fma")

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
typedef pair<int, ll> il;
typedef vector<ii> vii;
typedef vector<il> vil;
typedef vector<li> vli;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz size()
#define all(a) a.begin(), a.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define f0(i, b) for (int i = 0; i < (b); i++)
#define f1(i, b) for (int i = 1; i <= (b); i++)
#define f2(i, a, b) for (int i = (a); i <= (b); i++)
#define fr(i, b, a) for (int i = (b); i >= (a); i--)
#define rep(i, a, b, c) for (int i = (a); i != (b); i += (c))

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};

const double pi = acos(-1.0);
const double eps = 1e-6;
const int mod = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
const int logn = 20;
int c[maxn], n, last[maxn];
string s;

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin >> s;
  n = s.size();
  f0(i, n) {
    if (s[i] == 'R') {
      int j = i + 1;
      while (s[j] != 'L')
        ++j;
      f2(k, i, j - 1) last[k] = j;
      i = j;
    }
  }
  fr(i, n - 1, 0) {
    if (s[i] == 'L') {
      int j = i - 1;
      while (s[j] != 'R')
        --j;
      f2(k, j + 1, i) last[k] = j;
      i = j;
    }
  }
  f0(i, n) {
    if (abs(i - last[i]) % 2 == 0)
      c[last[i]]++;
    else {
      if (s[i] == 'L')
        c[last[i] + 1]++;
      else
        c[last[i] - 1]++;
    }
  }
  f0(i, n) cout << c[i] << " ";
  return 0;
}