Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

int biggerI(vector<int> &v, int val, int offset = 0) {
  int left = offset;
  if (v[left] >= val)
    return left;
  int right = v.size();
  if (v[right - 1] < val)
    return right;
  while (left + 1 < right) {
    int mid = (left + right) / 2;
    if (v[mid] < val)
      left = mid;
    else
      right = mid;
  }
  if (v[left] >= val)
    return left;
  else
    return right;
}

int main() {
  int n;
  cin >> n;
  vector<int> r, g, b;
  string s;
  cin >> s;
  for (int i = 1; i <= n; i++) {
    char c = s[i - 1];
    if (c == 'R')
      r.push_back(i);
    else if (c == 'G')
      g.push_back(i);
    else
      b.push_back(i);
  }
  sort(r.begin(), r.end());
  sort(g.begin(), g.end());
  sort(b.begin(), b.end());
  ll sum = 0;
  int sec_lst = 0;
  for (int i = 0; i < r.size(); i++) {
    int j = biggerI(g, r[i], sec_lst);
    if (j == g.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < g.size(); j++) {
      int k = biggerI(b, g[j], trd_lst);
      if (k == b.size())
        break;
      sum += b.size() - k;
      if (2 * g[j] - r[i] - 1 < n && s[2 * g[j] - r[i] - 1] == 'B')
        sum--;
      trd_lst = k;
    }
  }
  sec_lst = 0;
  for (int i = 0; i < r.size(); i++) {
    int j = biggerI(b, r[i], sec_lst);
    if (j == b.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < b.size(); j++) {
      int k = biggerI(g, b[j], trd_lst);
      if (k == g.size())
        break;
      sum += g.size() - k;
      if (2 * b[j] - r[i] - 1 < n && s[2 * b[j] - r[i] - 1] == 'G')
        sum--;
      trd_lst = k;
    }
  }
  sec_lst = 0;
  for (int i = 0; i < g.size(); i++) {
    int j = biggerI(b, g[i], sec_lst);
    if (j == b.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < b.size(); j++) {
      int k = biggerI(r, b[j], trd_lst);
      if (k == r.size())
        break;
      sum += r.size() - k;
      if (2 * b[j] - g[i] - 1 < n && s[2 * b[j] - g[i] - 1] == 'R')
        sum--;
      trd_lst = k;
    }
  }
  sec_lst = 0;
  for (int i = 0; i < g.size(); i++) {
    int j = biggerI(r, g[i], sec_lst);
    if (j == r.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < r.size(); j++) {
      int k = biggerI(b, r[j], trd_lst);
      if (k == b.size())
        break;
      sum += b.size() - k;
      if (2 * r[j] - g[i] - 1 < n && s[2 * r[j] - g[i] - 1] == 'B')
        sum--;
      trd_lst = k;
    }
  }
  sec_lst = 0;
  for (int i = 0; i < b.size(); i++) {
    int j = biggerI(r, b[i], sec_lst);
    if (j == r.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < r.size(); j++) {
      int k = biggerI(g, r[j], trd_lst);
      if (k == g.size())
        break;
      sum += g.size() - k;
      if (2 * r[j] - b[i] - 1 < n && s[2 * r[j] - b[i] - 1] == 'G')
        sum--;
      trd_lst = k;
    }
  }
  sec_lst = 0;
  for (int i = 0; i < b.size(); i++) {
    int j = biggerI(g, b[i], sec_lst);
    if (j == g.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < g.size(); j++) {
      int k = biggerI(r, g[j], trd_lst);
      if (k == r.size())
        break;
      sum += r.size() - k;
      if (2 * g[j] - b[i] - 1 < n && s[2 * g[j] - b[i] - 1] == 'R')
        sum--;
      trd_lst = k;
    }
  }

  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

int biggerI(vector<int> &v, int val, int offset = 0) {
  int left = offset;
  if (offset >= v.size())
    return v.size();
  if (v[left] >= val)
    return left;
  int right = v.size();
  if (v[right - 1] < val)
    return right;
  while (left + 1 < right) {
    int mid = (left + right) / 2;
    if (v[mid] < val)
      left = mid;
    else
      right = mid;
  }
  if (v[left] >= val)
    return left;
  else
    return right;
}

int main() {
  int n;
  cin >> n;
  vector<int> r, g, b;
  string s;
  cin >> s;
  for (int i = 1; i <= n; i++) {
    char c = s[i - 1];
    if (c == 'R')
      r.push_back(i);
    else if (c == 'G')
      g.push_back(i);
    else
      b.push_back(i);
  }
  sort(r.begin(), r.end());
  sort(g.begin(), g.end());
  sort(b.begin(), b.end());
  ll sum = 0;
  int sec_lst = 0;
  for (int i = 0; i < r.size(); i++) {
    int j = biggerI(g, r[i], sec_lst);
    if (j == g.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < g.size(); j++) {
      int k = biggerI(b, g[j], trd_lst);
      if (k == b.size())
        break;
      sum += b.size() - k;
      if (2 * g[j] - r[i] - 1 < n && s[2 * g[j] - r[i] - 1] == 'B')
        sum--;
      trd_lst = k;
    }
  }
  sec_lst = 0;
  for (int i = 0; i < r.size(); i++) {
    int j = biggerI(b, r[i], sec_lst);
    if (j == b.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < b.size(); j++) {
      int k = biggerI(g, b[j], trd_lst);
      if (k == g.size())
        break;
      sum += g.size() - k;
      if (2 * b[j] - r[i] - 1 < n && s[2 * b[j] - r[i] - 1] == 'G')
        sum--;
      trd_lst = k;
    }
  }
  sec_lst = 0;
  for (int i = 0; i < g.size(); i++) {
    int j = biggerI(b, g[i], sec_lst);
    if (j == b.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < b.size(); j++) {
      int k = biggerI(r, b[j], trd_lst);
      if (k == r.size())
        break;
      sum += r.size() - k;
      if (2 * b[j] - g[i] - 1 < n && s[2 * b[j] - g[i] - 1] == 'R')
        sum--;
      trd_lst = k;
    }
  }
  sec_lst = 0;
  for (int i = 0; i < g.size(); i++) {
    int j = biggerI(r, g[i], sec_lst);
    if (j == r.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < r.size(); j++) {
      int k = biggerI(b, r[j], trd_lst);
      if (k == b.size())
        break;
      sum += b.size() - k;
      if (2 * r[j] - g[i] - 1 < n && s[2 * r[j] - g[i] - 1] == 'B')
        sum--;
      trd_lst = k;
    }
  }
  sec_lst = 0;
  for (int i = 0; i < b.size(); i++) {
    int j = biggerI(r, b[i], sec_lst);
    if (j == r.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < r.size(); j++) {
      int k = biggerI(g, r[j], trd_lst);
      if (k == g.size())
        break;
      sum += g.size() - k;
      if (2 * r[j] - b[i] - 1 < n && s[2 * r[j] - b[i] - 1] == 'G')
        sum--;
      trd_lst = k;
    }
  }
  sec_lst = 0;
  for (int i = 0; i < b.size(); i++) {
    int j = biggerI(g, b[i], sec_lst);
    if (j == g.size())
      break;
    sec_lst = j;
    int trd_lst = 0;
    for (; j < g.size(); j++) {
      int k = biggerI(r, g[j], trd_lst);
      if (k == r.size())
        break;
      sum += r.size() - k;
      if (2 * g[j] - b[i] - 1 < n && s[2 * g[j] - b[i] - 1] == 'R')
        sum--;
      trd_lst = k;
    }
  }

  cout << sum << endl;
  return 0;
}