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

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

#define ll long long
#define ld long double
#define ar array

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()

#define F_OR(i, a, b, s)                                                       \\
  for (int i = (a); (s) > 0 ? i < (b) : i > (b); i += (s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto &x : a)

template <class T> bool umin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool umax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

ll FIRSTTRUE(function<bool(ll)> f, ll lb, ll rb) {
  while (lb < rb) {
    ll mb = (lb + rb) / 2;
    f(mb) ? rb = mb : lb = mb + 1;
  }
  return lb;
}
ll LASTTRUE(function<bool(ll)> f, ll lb, ll rb) {
  while (lb < rb) {
    ll mb = (lb + rb + 1) / 2;
    f(mb) ? lb = mb : rb = mb - 1;
  }
  return lb;
}

template <class A> void read(vt<A> &v);
template <class A, size_t S> void read(ar<A, S> &a);
template <class T> void read(T &x) { cin >> x; }
void read(double &d) {
  string t;
  read(t);
  d = stod(t);
}
void read(long double &d) {
  string t;
  read(t);
  d = stold(t);
}
template <class H, class... T> void read(H &h, T &...t) {
  read(h);
  read(t...);
}
template <class A> void read(vt<A> &x) {
  EACH(a, x)
  read(a);
}
template <class A, size_t S> void read(array<A, S> &x) {
  EACH(a, x)
  read(a);
}

string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char *s) { return string(s); }
string to_string(string s) { return s; }
string to_string(vt<bool> v) {
  string res;
  FOR(sz(v))
  res += char('0' + v[i]);
  return res;
}

template <size_t S> string to_string(bitset<S> b) {
  string res;
  FOR(S)
  res += char('0' + b[i]);
  return res;
}
template <class T> string to_string(T v) {
  bool f = 1;
  string res;
  EACH(x, v) {
    if (!f)
      res += ' ';
    f = 0;
    res += to_string(x);
  }
  return res;
}

template <class A> void write(A x) { cout << to_string(x); }
template <class H, class... T> void write(const H &h, const T &...t) {
  write(h);
  write(t...);
}
void print() { write("\\n"); }
template <class H, class... T> void print(const H &h, const T &...t) {
  write(h);
  if (sizeof...(t))
    write(' ');
  print(t...);
}

void DBG() { cerr << "]" << endl; }
template <class H, class... T> void DBG(H h, T... t) {
  cerr << to_string(h);
  if (sizeof...(t))
    cerr << ", ";
  DBG(t...);
}
#ifdef _DEBUG
#define dbg(...)                                                               \\
  cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [",           \\
      DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

template <class T> void offset(ll o, T &x) { x += o; }
template <class T> void offset(ll o, vt<T> &x) {
  EACH(a, x)
  offset(o, a);
}
template <class T, size_t S> void offset(ll o, ar<T, S> &x) {
  EACH(a, x)
  offset(o, a);
}

mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b) { return uniform_int_distribution<ll>(a, b)(mt_rng); }

template <class T, class U> void vti(vt<T> &v, U x, size_t n) {
  v = vt<T>(n, x);
}
template <class T, class U> void vti(vt<T> &v, U x, size_t n, size_t m...) {
  v = vt<T>(n);
  EACH(a, v)
  vti(a, x, m);
}

const int d4i[4] = {-1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1};
const int d8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
          d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};

void solve() {
  int n;
  string s;
  read(n, s);
  int l = 0, r = n - 1, ans = 0;
  while (1) {
    while (l < r && s[l] == 'R')
      ++l;
    while (l < r && s[r] == 'W')
      --r;
    if (l == r)
      break;
    swap(s[l], s[r]);
    ++ans;
  }
  print(ans);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int t = 1;
  // read(t);
  FOR(t) {
    // write("Case #", i+1, ": ");
    solve();
  }
}


Error: -6
/usr/include/c++/12/bits/basic_string.h:1221: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; reference = char&; size_type = long unsigned int]: Assertion '__pos <= size()' failed.


Fixed submission:

#pragma GCC Optimize("Ofast")
#pragma GCC Optimize "trapv"
#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace boost::multiprecision;

#define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TC                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define li long int
#define ll long long
#define ull unsigned long long
#define Vec(x) vector<x>
#define Pai(x, y) pair<x, y>
#define Map(x, y) map<x, y>
#define UMap(x, y) unordered_map<x, y>
#define Set(x) set<x>
#define Sta(x) stack<x>
#define Qu(x) queue<x>
#define DQ(x) deque(x)
#define PriorQ(x) priority_queue<x>
#define bg begin()
#define ed end()
#define sz size()
#define ct(x) count(x)
#define len length()
#define F first
#define S second
#define Makep make_pair
#define ins(x) insert(x)
#define ers(x) erase(x)
#define endl "\\n";
#define MOD 1000000007
#define PI 2 * acos(0.0)
#define clr() clear()
#define fori0(n) for (int i = 0; i < n; i++)
#define foriab(a, b) for (int i = a; i < b; i++)
#define mem0(x) memset(x, 0, sizeof(x))
#define mem1(x) memset(x, 1, sizeof(x))
#define autoit(x) for (auto it = x.beg; it != x.ed; it++)
#define count0(x) __builtin_popcount(x)
#define ret return
mt19937_64
    rang(chrono::high_resolution_clock::now().time_since_epoch().count());

const ll Inf = 1e9 + 5;

ll a[100005], freq[1000006], mx = INT_MIN;

namespace funset {
bool checkvow(char x) { return ((0x208222) >> (x & (0x1f))) & 1; }
ll highpow2(ll x) { return (x & ~(x - 1)); }
bool chk2(ll x) { return (x & (x - 1)); }
} // namespace funset

using namespace funset;

ll n, w;
ll wei[101], val[101], dp[1001][10001];

string s;

void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cw = 0, wr = 0, i;
  for (i = 0; s[i]; i++) {
    if (s[i] == 'R')
      break;
    cw++;
  }
  for (ll j = i + 1; s[j]; j++) {
    if (s[j] == 'R')
      wr++;
  }
  if (!wr || !cw) {
    cout << 0 << endl;
    return;
  }
  cout << (wr + cw) - min(wr, cw) << endl;
}

int main() {
  auto beg = clock();
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FAST srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  solve();
  auto ted = clock();
#ifndef ONLINE_JUDGE
  cout << (double)(ted - beg) / CLOCKS_PER_SEC << endl;
#endif
  ret 0;
}