Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<pii> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vector<pair<ll, ll>>> vvpll;
typedef vector<bool> vb;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<ll, ll> mll;

#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define all(x) x.begin(), x.end()
#define pb push_back
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define F first
#define S second
#define left (node << 1)
#define right (node << 1) + 1
// #define mod 998244353
#define mod 1000000007

#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl
#define trace4(x, y, z, t)                                                     \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << " | " << #t << ": " << t << endl
#define trace5(x, y, z, t, u)                                                  \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << " | " << #t << ": " << t << " | " << #u << ": " << u << endl
#define trace6(x, y, z, t, u, v)                                               \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << " | " << #t << ": " << t << " | " << #u << ": " << u << " | "   \
       << #v << ": " << v << endl

#define repa(i, sta, end, inc) for (int i = sta; i <= end; i += inc)
#define repb(i, sta, end, inc) for (int i = sta; i >= end; i -= inc)

ll power(ll x, ll y, ll z) {
  ll res = 1;
  x %= z;
  while (y) {
    if (y & 1) {
      res = res * x % z;
    }
    x = x * x % z;
    y /= 2;
  }
  return res % z;
}
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }

const int N = 2 * 100000 + 1;
const int inf = (1 << 30);

void solve() {
  int n;
  string s;
  cin >> n >> s;
  vi r, g, b;
  repa(i, 0, n - 1, 1) {
    if (s[i] == 'R') {
      r.pb(i);
    } else if (s[i] == 'G') {
      g.pb(i);
    } else {
      b.pb(i);
    }
  }
  ll ans = 0;
  repa(i, 0, r.size() - 1, 1) {
    int ri = r[i];
    int id = ub(all(g), ri) - g.begin();
    if (id != g.size()) {
      repa(j, id, g.size() - 1, 1) {
        int gj = g[j];
        int id_ = ub(all(b), gj) - b.begin();
        if (id_ != b.size()) {
          ans += b.size() - id_;
          if (bs(all(b), 2 * gj - ri))
            ans--;
        }
      }
    }
  }
  repa(i, 0, r.size() - 1, 1) {
    int ri = r[i];
    int id = ub(all(b), ri) - b.begin();
    if (id != b.size()) {
      repa(j, id, b.size() - 1, 1) {
        int bj = b[j];
        int id_ = ub(all(g), bj) - g.begin();
        if (id_ != g.size()) {
          ans += g.size() - id_;
          if (bs(all(g), 2 * bj - ri))
            ans--;
        }
      }
    }
  }
  repa(i, 0, g.size() - 1, 1) {
    int gi = g[i];
    int id = ub(all(r), gi) - r.begin();
    if (id != r.size()) {
      repa(j, id, r.size() - 1, 1) {
        int rj = r[j];
        int id_ = ub(all(b), rj) - b.begin();
        if (id_ != b.size()) {
          ans += b.size() - id_;
          if (bs(all(b), 2 * rj - gi))
            ans--;
        }
      }
    }
  }
  repa(i, 0, g.size() - 1, 1) {
    int gi = g[i];
    int id = ub(all(b), gi) - b.begin();
    if (id != b.size()) {
      repa(j, id, b.size() - 1, 1) {
        int bj = b[j];
        int id_ = ub(all(r), bj) - r.begin();
        if (id_ != r.size()) {
          ans += r.size() - id_;
          if (bs(all(r), 2 * bj - gi))
            ans--;
        }
      }
    }
  }
  repa(i, 0, b.size() - 1, 1) {
    int bi = b[i];
    int id = ub(all(g), bi) - g.begin();
    if (id != g.size()) {
      repa(j, id, g.size() - 1, 1) {
        int gj = g[j];
        int id_ = ub(all(r), gj) - r.begin();
        if (id_ != r.size()) {
          ans += r.size() - id_;
          if (bs(all(r), 2 * gj - bi))
            ans--;
        }
      }
    }
  }
  repa(i, 0, b.size() - 1, 1) {
    int bi = b[i];
    int id = ub(all(r), bi) - r.begin();
    if (id != r.size()) {
      repa(j, id, r.size() - 1, 1) {
        int rj = r[j];
        int id_ = ub(all(g), rj) - g.begin();
        if (id_ != g.size()) {
          ans += g.size() - id_;
          if (bs(all(g), 2 * rj - bi))
            ans--;
        }
      }
    }
  }
  cout << ans << endl;
}

int main() {
  FIO;
  int T = 1;
  // cin >> T;
  repa(i, 1, T, 1) { solve(); }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<pii> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vector<pair<ll, ll>>> vvpll;
typedef vector<bool> vb;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<ll, ll> mll;

#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define all(x) x.begin(), x.end()
#define pb push_back
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define F first
#define S second
#define left (node << 1)
#define right (node << 1) + 1
// #define mod 998244353
#define mod 1000000007

#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl
#define trace4(x, y, z, t)                                                     \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << " | " << #t << ": " << t << endl
#define trace5(x, y, z, t, u)                                                  \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << " | " << #t << ": " << t << " | " << #u << ": " << u << endl
#define trace6(x, y, z, t, u, v)                                               \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << " | " << #t << ": " << t << " | " << #u << ": " << u << " | "   \
       << #v << ": " << v << endl

#define repa(i, sta, end, inc) for (int i = sta; i <= end; i += inc)
#define repb(i, sta, end, inc) for (int i = sta; i >= end; i -= inc)

ll power(ll x, ll y, ll z) {
  ll res = 1;
  x %= z;
  while (y) {
    if (y & 1) {
      res = res * x % z;
    }
    x = x * x % z;
    y /= 2;
  }
  return res % z;
}
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }

const int N = 2 * 100000 + 1;
const int inf = (1 << 30);

void solve() {
  int n;
  string s;
  cin >> n >> s;
  vi r, g, b;
  repa(i, 0, n - 1, 1) {
    if (s[i] == 'R') {
      r.pb(i);
    } else if (s[i] == 'G') {
      g.pb(i);
    } else {
      b.pb(i);
    }
  }
  ll ans = 0;
  if (r.size())
    repa(i, 0, r.size() - 1, 1) {
      int ri = r[i];
      if (g.size()) {
        int id = ub(all(g), ri) - g.begin();
        if (id != g.size()) {
          repa(j, id, g.size() - 1, 1) {
            int gj = g[j];
            if (b.size()) {
              int id_ = ub(all(b), gj) - b.begin();
              if (id_ != b.size()) {
                ans += b.size() - id_;
                if (bs(all(b), 2 * gj - ri))
                  ans--;
              }
            }
          }
        }
      }
    }
  if (r.size())
    repa(i, 0, r.size() - 1, 1) {
      int ri = r[i];
      if (b.size()) {
        int id = ub(all(b), ri) - b.begin();
        if (id != b.size()) {
          repa(j, id, b.size() - 1, 1) {
            int bj = b[j];
            if (g.size()) {
              int id_ = ub(all(g), bj) - g.begin();
              if (id_ != g.size()) {
                ans += g.size() - id_;
                if (bs(all(g), 2 * bj - ri))
                  ans--;
              }
            }
          }
        }
      }
    }
  if (g.size())
    repa(i, 0, g.size() - 1, 1) {
      int gi = g[i];
      if (r.size()) {
        int id = ub(all(r), gi) - r.begin();
        if (id != r.size()) {
          repa(j, id, r.size() - 1, 1) {
            int rj = r[j];
            if (b.size()) {
              int id_ = ub(all(b), rj) - b.begin();
              if (id_ != b.size()) {
                ans += b.size() - id_;
                if (bs(all(b), 2 * rj - gi))
                  ans--;
              }
            }
          }
        }
      }
    }
  if (g.size())
    repa(i, 0, g.size() - 1, 1) {
      int gi = g[i];
      if (b.size()) {
        int id = ub(all(b), gi) - b.begin();
        if (id != b.size()) {
          repa(j, id, b.size() - 1, 1) {
            int bj = b[j];
            if (r.size()) {
              int id_ = ub(all(r), bj) - r.begin();
              if (id_ != r.size()) {
                ans += r.size() - id_;
                if (bs(all(r), 2 * bj - gi))
                  ans--;
              }
            }
          }
        }
      }
    }
  if (b.size())
    repa(i, 0, b.size() - 1, 1) {
      int bi = b[i];
      if (g.size()) {
        int id = ub(all(g), bi) - g.begin();
        if (id != g.size()) {
          repa(j, id, g.size() - 1, 1) {
            int gj = g[j];
            if (r.size()) {
              int id_ = ub(all(r), gj) - r.begin();
              if (id_ != r.size()) {
                ans += r.size() - id_;
                if (bs(all(r), 2 * gj - bi))
                  ans--;
              }
            }
          }
        }
      }
    }
  if (b.size())
    repa(i, 0, b.size() - 1, 1) {
      int bi = b[i];
      if (r.size()) {
        int id = ub(all(r), bi) - r.begin();
        if (id != r.size()) {
          repa(j, id, r.size() - 1, 1) {
            int rj = r[j];
            if (g.size()) {
              int id_ = ub(all(g), rj) - g.begin();
              if (id_ != g.size()) {
                ans += g.size() - id_;
                if (bs(all(g), 2 * rj - bi))
                  ans--;
              }
            }
          }
        }
      }
    }
  cout << ans << endl;
}

int main() {
  FIO;
  int T = 1;
  // cin >> T;
  repa(i, 1, T, 1) { solve(); }
  return 0;
}