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

#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif

using namespace std;

#define TRACE(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cerr << endl;                                                              \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[" << *it << " = " << a << "]";
  err(++it, args...);
}

#define MAXN ((int)2e5 + 5)
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cerr << x << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\\n"
#define pii pair<int, int>
#define mid ((l + r) / 2)

int n;
string s;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;

  int lw = 0;
  while (lw < n && s[lw] != 'W')
    lw++;

  int rr = n - 1;
  while (rr > 0 && s[rr] != 'R')
    rr--;

  if (lw == n or rr == 0) {
    cout << 0 << endl;
    return 0;
  }

  int rct = 0;
  for (int i = lw; i <= rr; ++i)
    rct += (s[i] == 'R');

  int ans = 0;
  for (int i = 0; i < rct; ++i)
    ans += s[lw + i] == 'W';

  cout << ans << endl;
}

/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 */

Error: 0

Fixed submission:

#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif

using namespace std;

#define TRACE(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cerr << endl;                                                              \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[" << *it << " = " << a << "]";
  err(++it, args...);
}

#define MAXN ((int)2e5 + 5)
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cerr << x << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\\n"
#define pii pair<int, int>
#define mid ((l + r) / 2)

int n;
string s;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;

  int lw = 0;
  while (s[lw] != 'W')
    lw++;

  int rr = n - 1;
  while (s[rr] != 'R')
    rr--;

  int rct = 0;
  for (int i = lw; i <= rr; ++i)
    rct += (s[i] == 'R');

  int ans = 0;
  for (int i = 0; i < rct; ++i)
    ans += s[lw + i] == 'W';

  cout << ans << endl;
}

/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 */