Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define ull unsigned long long

#define max 998244353 // 1000000007

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  while (ll r = a % b)
    a = b, b = r;
  return b;
}

class mod {
public:
  ll val;

  mod(ll v) { val = v % max; }

  mod() { val = 0; }

  void operator-=(const mod &v) {
    val -= v.val;

    if (val < 0)
      val += max;
  }

  void operator+=(const mod &v) {
    if (val + v.val >= max)
      val -= max;
    val += v.val;
  }

  void operator+=(ll &v) {
    if (val + v >= max)
      val -= max;
    val += v;
  }

  void operator*=(const mod &v) {
    val *= v.val;
    val %= max;
  }

  void operator*=(ll &v) {
    val *= v;
    val %= max;
  }

  ll modinv(ll a) {
    ll b = max, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    u %= max;
    if (u < 0)
      u += max;
    return u;
  }

  void operator/=(const mod &v) {
    val *= modinv(v.val);
    val %= max;
  }

  void operator/=(ll &v) {
    val *= modinv(v);
    val %= max;
  }
};

int func(int a, int b) {
  if (a < b)
    return func(b, a);
  int r;
  while (r = a % b) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int n;
  cin >> n;
  vector<int> s(n), r(n), g(n), b(n);
  int nr = 0, ng = 0, nb = 0;

  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    if (c == 'R')
      s[i] = 1, r[nr++] = i;
    else if (c == 'G')
      s[i] = 2, g[ng++] = i;
    else
      s[i] = 4, b[nb++] = i;
  }

  ll re = 0;

  for (int i = 0; i < n - 2; i++)
    for (int j = i + 1; j < n - 1; j++)
      if (s[i] == s[j])
        continue;
      else
        for (int k = j + 1; k < n; k++)
          if (j - i != k - j && s[i] + s[j] + s[k] == 7)
            re++;
  printf("%lld", re);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define ull unsigned long long

#define max 998244353 // 1000000007

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  while (ll r = a % b)
    a = b, b = r;
  return b;
}

class mod {
public:
  ll val;

  mod(ll v) { val = v % max; }

  mod() { val = 0; }

  void operator-=(const mod &v) {
    val -= v.val;

    if (val < 0)
      val += max;
  }

  void operator+=(const mod &v) {
    if (val + v.val >= max)
      val -= max;
    val += v.val;
  }

  void operator+=(ll &v) {
    if (val + v >= max)
      val -= max;
    val += v;
  }

  void operator*=(const mod &v) {
    val *= v.val;
    val %= max;
  }

  void operator*=(ll &v) {
    val *= v;
    val %= max;
  }

  ll modinv(ll a) {
    ll b = max, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    u %= max;
    if (u < 0)
      u += max;
    return u;
  }

  void operator/=(const mod &v) {
    val *= modinv(v.val);
    val %= max;
  }

  void operator/=(ll &v) {
    val *= modinv(v);
    val %= max;
  }
};

int func(int a, int b) {
  if (a < b)
    return func(b, a);
  int r;
  while (r = a % b) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int n;
  cin >> n;
  vector<int> s(n), r(n), g(n), b(n);
  int nr = 0, ng = 0, nb = 0;

  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    if (c == 'R')
      s[i] = 1, r[nr++] = i;
    else if (c == 'G')
      s[i] = 2, g[ng++] = i;
    else
      s[i] = 4, b[nb++] = i;
  }

  ll re = 0;

  for (int i = 0; i < n - 2; i++)
    for (int j = i + 1; j < n - 1; j++)
      if (s[i] == s[j])
        continue;
      //	j以上の数から探索
      else {
        int k;
        int target = 2 * j - i;
        bool had = false;
        if (s[i] + s[j] == 3) {
          //	b
          k = lower_bound(b.begin(), b.begin() + nb, j + 1) - b.begin();
          re += (nb - k);
          if (binary_search(b.begin() + k, b.begin() + nb, target))
            re--;
        } else if (s[i] + s[j] == 5) {
          //	g
          k = lower_bound(g.begin(), g.begin() + ng, j + 1) - g.begin();
          re += (ng - k);
          if (binary_search(g.begin() + k, g.begin() + ng, target))
            re--;
        } else {
          //	r
          k = lower_bound(r.begin(), r.begin() + nr, j + 1) - r.begin();
          re += (nr - k);
          if (binary_search(r.begin() + k, r.begin() + nr, target))
            re--;
        }
      }

  printf("%lld", re);

  return 0;
}