Problem p02954 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

/*
||||||||||||||||||||||||||||||||||||||||||||||||
   |      |||         |      ||||    ||||  |   |
  | |     |   |      | |     |   |  |      |   |
 |   |    |    |    |   |    |   |  |      |   |
|||||||   |    |   |||||||   |||     |||   |||||
|     |   |   |    |     |   | |        |  |   |
|     |   |||      |     |   |  ||  ||||   |   |
||||||||||||||||||||||||||||||||||||||||||||||||

*/
using namespace std;
// #include "testlib.h"
#define ff first
#define ss second
#define mp make_pair
#define all(v) v.begin(), v.end()
#define int long long
#define ll long long
#define M 1000000007
#define inputarr(a, n)                                                         \
  for (int i = 0; i < n; ++i)                                                  \
  cin >> a[i]
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll int, string>
#define rep(a, b) for (ll i = a; i < b; i++)
#define rep0(n) for (ll i = 0; i < n; i++)
#define repi(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#define vi vector<ll>
#define mp make_pair
#define vs vector<string>
#define ppb pop_back
#define endl '\n'
#define asdf                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define r0 return 0;
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define FORE(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define inputoutput                                                            \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define input freopen("input.txt", "r", stdin);
#define Set(a, s) 4(a, s, sizeof(a))
#define FOR repi
#define pii pair<int, int>
// #define float long double
ll max(ll a, ll b) { return (a > b) ? a : b; }
int min(int a, int b) { return (a < b) ? a : b; }

int solve() {
  string s;
  cin >> s;
  int n = s.size();
  vi r, l;
  rep0(n) if (s[i] == 'L') l.pb(i);
  else r.pb(-i);
  // FORE(it,l) cout<<*it<<" ";cout<<endl;
  // FORE(it,r) cout<<*it<<" ";
  sort(all(r));
  vi ans(n + 1, 0);
  rep0(n) {
    if (s[i] == 'R') {
      int k = upper_bound(all(l), i) - l.begin();
      // cout<<k<<" ";
      if ((l[k] - i) % 2)
        ans[l[k] - 1]++;
      else
        ans[l[k]]++;
    } else {
      int k = upper_bound(all(r), -i) - r.begin();
      if ((-r[k] - i) % 2)
        ans[-r[k] + 1]++;
      else
        ans[-r[k]]++;
    }
  }
  rep0(n) cout << ans[i] << " ";

  r0
}
signed main() {
  inputoutput asdf int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

/*
||||||||||||||||||||||||||||||||||||||||||||||||
   |      |||         |      ||||    ||||  |   |
  | |     |   |      | |     |   |  |      |   |
 |   |    |    |    |   |    |   |  |      |   |
|||||||   |    |   |||||||   |||     |||   |||||
|     |   |   |    |     |   | |        |  |   |
|     |   |||      |     |   |  ||  ||||   |   |
||||||||||||||||||||||||||||||||||||||||||||||||

*/
using namespace std;
// #include "testlib.h"
#define ff first
#define ss second
#define mp make_pair
#define all(v) v.begin(), v.end()
#define int long long
#define ll long long
#define M 1000000007
#define inputarr(a, n)                                                         \
  for (int i = 0; i < n; ++i)                                                  \
  cin >> a[i]
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll int, string>
#define rep(a, b) for (ll i = a; i < b; i++)
#define rep0(n) for (ll i = 0; i < n; i++)
#define repi(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#define vi vector<ll>
#define mp make_pair
#define vs vector<string>
#define ppb pop_back
#define endl '\n'
#define asdf                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define r0 return 0;
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define FORE(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define inputoutput                                                            \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define input freopen("input.txt", "r", stdin);
#define Set(a, s) 4(a, s, sizeof(a))
#define FOR repi
#define pii pair<int, int>
// #define float long double
ll max(ll a, ll b) { return (a > b) ? a : b; }
int min(int a, int b) { return (a < b) ? a : b; }

int solve() {
  string s;
  cin >> s;
  int n = s.size();
  vi r, l;
  rep0(n) if (s[i] == 'L') l.pb(i);
  else r.pb(-i);
  // FORE(it,l) cout<<*it<<" ";cout<<endl;
  // FORE(it,r) cout<<*it<<" ";
  sort(all(r));
  vi ans(n + 1, 0);
  rep0(n) {
    if (s[i] == 'R') {
      int k = upper_bound(all(l), i) - l.begin();
      // cout<<k<<" ";
      if ((l[k] - i) % 2)
        ans[l[k] - 1]++;
      else
        ans[l[k]]++;
    } else {
      int k = upper_bound(all(r), -i) - r.begin();
      if ((-r[k] - i) % 2)
        ans[-r[k] + 1]++;
      else
        ans[-r[k]]++;
    }
  }
  rep0(n) cout << ans[i] << " ";

  r0
}
signed main() { // inputoutput
  asdf int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}