prithivMLmods/Coder-Stat huggingface dataset:
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007; // 998244353
using ll = long long;
using namespace std;

const int red = 1, green = 2, blue = 4;

ll sum[10][4010];

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> seq;
  for (char c : s) {
    if (c == 'R')
      seq.push_back(red);
    if (c == 'G')
      seq.push_back(green);
    if (c == 'B')
      seq.push_back(blue);
  }
  for (int i = 0; i < n; ++i) {
    for (int color = red; color <= blue; color << 1) {
      sum[color][i + 1] = sum[color][i];
    }
    sum[seq[i]][i + 1] += 1;
  }
  ll ans = 0;
  for (int left = 0; left < n - 2; ++left) {
    for (int right = left + 2; right < n; ++right) {
      if (seq[left] == seq[right])
        continue;
      int needed_c = 7 - seq[left] - seq[right];
      ans += (sum[needed_c][right] - sum[needed_c][left + 1]);
      if ((right - left) % 2 == 0 and seq[(left + right) / 2] == needed_c)
        ans -= 1;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define l_ength size
const int inf = (1 << 30);
const int mod = 1000000007; // 998244353
using ll = long long;
using namespace std;

const int red = 1, green = 2, blue = 4;

ll sum[10][4010];

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> seq;
  for (char c : s) {
    if (c == 'R')
      seq.push_back(red);
    if (c == 'G')
      seq.push_back(green);
    if (c == 'B')
      seq.push_back(blue);
  }
  for (int i = 0; i < n; ++i) {
    for (int color = red; color <= blue; color *= 2) {
      sum[color][i + 1] = sum[color][i];
    }
    sum[seq[i]][i + 1] += 1;
  }
  ll ans = 0;
  for (int left = 0; left < n - 2; ++left) {
    for (int right = left + 2; right < n; ++right) {
      if (seq[left] == seq[right])
        continue;
      int needed_c = 7 - seq[left] - seq[right];
      ans += (sum[needed_c][right] - sum[needed_c][left + 1]);
      if ((right - left) % 2 == 0 and seq[(left + right) / 2] == needed_c)
        ans -= 1;
    }
  }
  cout << ans << endl;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int ans = 0;
  for (int l = 0; l < N - 2; l++) {
    for (int i = l + 1; i < N - 1; i++) {
      for (int r = i + 1; r < N; r++) {
        if (i - l != r - i) {
          if (S[l] != S[i] && S[i] != S[r] && S[r] != S[l])
            ans += 1;
        }
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  map<char, long int> count;
  for (int i = 0; i < N; i++)
    count[S[i]] += 1;
  long int ans = count['R'] * count['G'] * count['B'];
  for (int i = 1; i < N - 1; i++) {
    for (int r = 1; r <= min(i, N - 1 - i); r++) {
      if (S[i - r] != S[i] && S[i + r] != S[i] && S[i - r] != S[i + r])
        ans -= 1;
    }
  }
  cout << ans;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
typedef long long ll;

int main() {
  ll N;
  cin >> N;
  string s;
  cin >> s;
  ll r = 0, g = 0, b = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R') {
      r++;
    } else if (s[i] == 'G') {
      g++;
    } else {
      b++;
    }
  }
  ll ans = r * g * b;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      for (int k = j + 1; k < N; k++) {
        if (j - i != k - j) {
          continue;
        }

        else if (s[i] != s[j] && s[j] != s[k] && s[k] != s[i]) {
          ans--;
        }
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
typedef long long ll;

int main() {
  ll N;
  cin >> N;
  string s;
  cin >> s;
  ll r = 0, g = 0, b = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R') {
      r++;
    } else if (s[i] == 'G') {
      g++;
    } else {
      b++;
    }
  }
  ll ans = r * g * b;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (s[i] == s[j]) {
        continue;
      }
      ll k = 2 * j - i;
      if (k >= N)
        continue;
      else if (s[j] != s[k] && s[k] != s[i]) {
        ans--;
      }
    }
  }
  cout << ans << endl;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
//       Author: Mostafa Mounir Shehab

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

#define PI 3.14159265 // PI = acos(-1)
#define EPS (1e-10)
#define endl "\n"
#define SZ(v) (int)(v.size())
typedef long long ll;
typedef long double ld;
// template <class Type>

void Fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cerr.tie(NULL);
}

inline int D() {
  int t;
  scanf("%d", &t);
  return t;
}
inline ll llD() {
  ll t;
  scanf("%lld", &t);
  return t;
}

ll MOD = 1e9 + 7;
const int N = 4e3 + 5;
char arr[N];
int OO = 1e6;

ll n, m, k, cnt, SU, idx, len, MN, MX, T;
ll l, r, mid;
ll a, b, c, d;
vector<int> vec, v;
string str = "BGR", s;

bool flage;
map<char, vector<int>> mp;

int valid(int i, int j) {
  if (arr[i] == arr[j])
    return 0;
  int len = 0;
  do {

    if (str[0] == arr[i] && str[1] == arr[j]) {
      if (!SZ(mp[str[2]]))
        return 0;
      if (mp[str[2]].back() <= j)
        continue;
      len += SZ(mp[str[2]]) -
             (lower_bound(mp[str[2]].begin(), mp[str[2]].end(), j) -
              mp[str[2]].begin());
      len -= (arr[j + j - i] == str[2]);
    }
  } while (next_permutation(str.begin(), str.end()));

  //	cerr<<i<<" "<<j<<" "<<len<<endl;

  return len;
}

int main() {

  scanf("%lld", &n);
  scanf("%s", arr);

  for (int i = 0; i < n; ++i)
    mp[arr[i]].push_back(i);

  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j < n; ++j)
      cnt += valid(i, j);

  printf("%lld\n", cnt);
}


Fixed submission:
//       Author: Mostafa Mounir Shehab

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

#define PI 3.14159265 // PI = acos(-1)
#define EPS (1e-10)
#define endl "\n"
#define SZ(v) (int)(v.size())
typedef long long ll;
typedef long double ld;
// template <class Type>

void Fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cerr.tie(NULL);
}

inline int D() {
  int t;
  scanf("%d", &t);
  return t;
}
inline ll llD() {
  ll t;
  scanf("%lld", &t);
  return t;
}

ll MOD = 1e9 + 7;
const int N = 1e5 + 5;
char arr[N];
int OO = 1e6;

ll n, m, k, cnt, SU, idx, len, MN, MX, T;
ll l, r, mid;
ll a, b, c, d;
vector<int> vec, v;
string str = "BGR", s;

bool flage;
map<char, vector<int>> mp;

int valid(int i, int j) {
  if (arr[i] == arr[j])
    return 0;
  int len = 0;
  do {

    if (str[0] == arr[i] && str[1] == arr[j]) {
      if (!SZ(mp[str[2]]))
        return 0;
      if (mp[str[2]].back() <= j)
        continue;
      len += SZ(mp[str[2]]) -
             (lower_bound(mp[str[2]].begin(), mp[str[2]].end(), j) -
              mp[str[2]].begin());
      len -= (arr[j + j - i] == str[2]);
    }
  } while (next_permutation(str.begin(), str.end()));

  //	cerr<<i<<" "<<j<<" "<<len<<endl;

  return len;
}

int main() {

  scanf("%lld", &n);
  scanf("%s", arr);

  for (int i = 0; i < n; ++i)
    mp[arr[i]].push_back(i);

  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j < n; ++j)
      cnt += valid(i, j);

  printf("%lld\n", cnt);
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ll n;
  cin >> n;

  string s;
  cin >> s;

  map<char, vector<ll>> ind;
  for (ll i = 0; i < n; i++) {
    ind[s[i]].push_back(i);
  }

  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      if (s[i] == s[j])
        continue;
      set<char> cols = {'R', 'G', 'B'};
      cols.erase(s[i]);
      cols.erase(s[j]);
      char c = *cols.begin();

      auto begin = lower_bound(ind[c].begin(), ind[c].end(), j + 1);
      auto it = lower_bound(begin, ind[c].end(), 2 * j - i);
      if (*it == 2 * j - i)
        ans += ind[c].end() - begin - 1;
      else
        ans += ind[c].end() - begin;
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ll n;
  cin >> n;

  string s;
  cin >> s;

  map<char, vector<ll>> ind;
  for (ll i = 0; i < n; i++) {
    ind[s[i]].push_back(i);
  }

  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      if (s[i] == s[j])
        continue;
      set<char> cols = {'R', 'G', 'B'};
      cols.erase(s[i]);
      cols.erase(s[j]);
      char c = *cols.begin();

      auto begin = lower_bound(ind[c].begin(), ind[c].end(), j + 1);
      auto exist = binary_search(begin, ind[c].end(), 2 * j - i);
      if (exist)
        ans += ind[c].end() - begin - 1;
      else
        ans += ind[c].end() - begin;
    }
  }

  cout << ans << endl;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <unordered_set>

using namespace std;

int main() {

  int n;
  string s;
  cin >> n >> s;
  long long res = 0;
  unordered_set<int> r, g, b;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'R')
      r.insert(i);
    else if (s[i] == 'G')
      g.insert(i);
    else
      b.insert(i);
  }
  for (auto i : r) {
    for (auto j : g) {
      for (auto k : b) {
        int _max = max(max(i, j), k);
        int _min = min(min(i, j), k);
        if ((_max + _min) * 3 != (i + j + k) * 2)
          res++;
      }
    }
  }
  cout << res;
}

Fixed submission:
#include <iostream>
#include <unordered_set>

using namespace std;

int main() {

  int n;
  string s;
  cin >> n >> s;
  long long res = 0;
  unordered_set<int> r, g, b;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'R')
      r.insert(i);
    else if (s[i] == 'G')
      g.insert(i);
    else
      b.insert(i);
  }
  for (auto i : r) {
    for (auto j : g) {
      res += b.size();
      if (b.count(2 * i - j))
        res--;
      if (b.count(2 * j - i))
        res--;
      if ((i + j) % 2 == 0 && b.count((i + j) / 2))
        res--;
    }
  }
  cout << res;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

int main() {
  using namespace std;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, sum;
  long long count = 0;
  string s;
  vector<int> r, g, b;
  cin >> n >> s;
  for (int i = 0; i <= n; i++) {
    switch (s[i]) {
    case 'R':
      r.push_back(i + 1);
      break;
    case 'G':
      g.push_back(i + 1);
      break;
    case 'B':
      b.push_back(i + 1);
      break;
    default:
      break;
    }
  }
  for (int i = 0; i < (int)r.size(); i++) {
    for (int j = 0; j < (int)g.size(); j++) {
      for (int k = 0; k < (int)b.size(); k++) {
        sum = r[i] + g[j] + b[k];
        if (3 * r[i] == sum) {
          count--;
          continue;
        } else if (3 * g[j] == sum) {
          count--;
          continue;
        } else if (3 * b[k] == sum) {
          count--;
          continue;
        }
      }
    }
  }
  count += r.size() * g.size() * b.size();
  cout << count << endl;
  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

int main() {
  using namespace std;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, sum;
  long long count = 0;
  string s;
  vector<int> r, g, b;
  cin >> n >> s;
  for (int i = 0; i <= n; i++) {
    switch (s[i]) {
    case 'R':
      r.push_back(i + 1);
      break;
    case 'G':
      g.push_back(i + 1);
      break;
    case 'B':
      b.push_back(i + 1);
      break;
    default:
      break;
    }
  }
  for (int i = 0; i < (int)r.size(); i++) {
    for (int j = 0; j < (int)g.size(); j++) {
      for (int k = 0; k < (int)b.size(); k++) {
        sum = r[i] + g[j] + b[k];
        if (3 * r[i] == sum || 3 * g[j] == sum || 3 * b[k] == sum) {
          count--;
          continue;
        }
      }
    }
  }
  count += r.size() * g.size() * b.size();
  cout << count << endl;
  return 0;
}
Problem p02714 (C++)

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

int64_t pairs(string s, int n) {
  int64_t sum = 0;

  int64_t r = 0;
  int64_t g = 0;
  int64_t b = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == 'R') {
      r++;
    } else if (s.at(i) == 'G') {
      g++;
    } else if (s.at(i) == 'B') {
      b++;
    }
  }

  int64_t no = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int d = 1;; d++) {
      if (2 * d >= n) {
        break;
      }
      if (s.at(i) != s.at(i + d) && s.at(i + d) != s.at(i + 2 * d) &&
          s.at(i + 2 * d) != s.at(i)) {
        no++;
      }
    }
  }
  sum = r * g * b - no;

  return sum;
}

int main() {
  int n;
  string s;
  cin >> n >> s;

  cout << pairs(s, n) << endl;
}

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

int64_t pairs(string s, int n) {
  int64_t sum = 0;

  int64_t r = 0;
  int64_t g = 0;
  int64_t b = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == 'R') {
      r++;
    } else if (s.at(i) == 'G') {
      g++;
    } else if (s.at(i) == 'B') {
      b++;
    }
  }

  int64_t no = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int d = 1;; d++) {
      if (i + 2 * d >= n) {
        break;
      }
      if (s.at(i) != s.at(i + d) && s.at(i + d) != s.at(i + 2 * d) &&
          s.at(i + 2 * d) != s.at(i)) {
        no++;
      }
    }
  }
  sum = r * g * b - no;

  return sum;
}

int main() {
  int n;
  string s;
  cin >> n >> s;

  cout << pairs(s, n) << endl;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i < (n); i++)
typedef long long ll;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> r(0), g(0), b(0);
  rep(i, n) {
    if (s[i] == 'R')
      r.push_back(i);
    if (s[i] == 'G')
      g.push_back(i);
    if (s[i] == 'B')
      b.push_back(i);
  }
  ll ans = 0;
  for (int i : r) {
    for (int j : g) {
      ll IJ = abs(i - j);
      for (int k : b) {
        ll JK = abs(j - k);
        ll KI = abs(k - i);
        if (IJ == JK || JK == KI || KI == IJ)
          continue;
        else
          ans++;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i < (n); i++)
typedef long long ll;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> r(0), g(0), b(0);
  rep(i, n) {
    if (s[i] == 'R')
      r.push_back(i);
    if (s[i] == 'G')
      g.push_back(i);
    if (s[i] == 'B')
      b.push_back(i);
  }
  ll ans = r.size() * g.size() * b.size();
  for (auto i : r) {
    for (auto j : g) {
      if (i % 2 == j % 2) {
        if (s[(i + j) / 2] == 'B')
          ans--;
      }
      if (0 <= 2 * i - j && 2 * i - j < n) {
        if (s[2 * i - j] == 'B')
          ans--;
      }
      if (0 <= 2 * j - i && 2 * j - i < n) {
        if (s[2 * j - i] == 'B')
          ans--;
      }
    }
  }
  cout << ans << endl;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve(long long N, std::string S) {
  ll R = 0, G = 0, B = 0;
  for (auto ch : S) {
    if (ch == 'R') {
      R++;
    }
    if (ch == 'G') {
      G++;
    }
    if (ch == 'B') {
      B++;
    }
  }
  ll Rl = 0, Gl = 0, Bl = 0;
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    auto ch = S[i];
    if (ch == 'R') {
      ans += Gl * B + Bl * G;
      Rl++;
      R--;
    }
    if (ch == 'G') {
      ans += Rl * B + Bl * R;
      Gl++;
      G--;
    }
    if (ch == 'B') {
      ans += Rl * G + Gl * R;
      Bl++;
      B--;
    }
    for (ll j = 1; 0 <= i - j && i + j < N; j++) {
      if (ch != S[i - j] && ch != S[i + j] && S[i - j] != S[i + j]) {
        ans--;
      }
    }
  }
  cout << ans << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
// clang-format off
#pragma GCC diagnostic ignored "-Wunused-result"
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long N;
    scanf("%lld",&N);
    std::string S;
    std::cin >> S;
    solve(N, S);
    return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve(long long N, std::string S) {
  ll R = 0, G = 0, B = 0;
  for (auto ch : S) {
    if (ch == 'R') {
      R++;
    }
    if (ch == 'G') {
      G++;
    }
    if (ch == 'B') {
      B++;
    }
  }
  ll Rl = 0, Gl = 0, Bl = 0;
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    auto ch = S[i];
    if (ch == 'R') {
      ans += Gl * B + Bl * G;
      Rl++;
      R--;
    }
    if (ch == 'G') {
      ans += Rl * B + Bl * R;
      Gl++;
      G--;
    }
    if (ch == 'B') {
      ans += Rl * G + Gl * R;
      Bl++;
      B--;
    }
    for (ll j = 1; 0 <= i - j && i + j < N; j++) {
      if (ch != S[i - j] && ch != S[i + j] && S[i - j] != S[i + j]) {
        ans--;
      }
    }
  }
  cout << ans << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
// clang-format off
#pragma GCC diagnostic ignored "-Wunused-result"
int main(){
    long long N;
    scanf("%lld",&N);
    std::string S;
    std::cin >> S;
    solve(N, S);
    return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main(void) {
  int N;
  cin >> N;
  string s;
  cin >> s;

  vector<int> r;
  vector<int> g;
  vector<int> b;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R')
      r.push_back(i);
    if (s[i] == 'G')
      g.push_back(i);
    if (s[i] == 'B')
      b.push_back(i);
  }

  if (r.size() < 1 || g.size() < 1 || b.size() < 1) {
    cout << 0 << endl;
    return 0;
  }

  int count = 0;
  for (int i = 0; i < r.size(); i++) {
    for (int j = 0; j < g.size(); j++) {
      for (int k = 0; k < b.size(); k++) {
        int maxnum = max(max(r[i], g[j]), b[k]);
        int minnum = min(min(r[i], g[j]), b[k]);
        int middle = r[i] + g[j] + b[k] - maxnum - minnum;
        if (maxnum - middle != middle - minnum)
          count++;
      }
    }
  }

  cout << count << endl;
}

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

int main(void) {
  int N;
  cin >> N;
  string s;
  cin >> s;

  vector<int> r;
  vector<int> g;
  vector<int> b;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R')
      r.push_back(i);
    if (s[i] == 'G')
      g.push_back(i);
    if (s[i] == 'B')
      b.push_back(i);
  }

  if (r.size() < 1 || g.size() < 1 || b.size() < 1) {
    cout << 0 << endl;
    return 0;
  }

  long long count = r.size() * g.size() * b.size();
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      int k = 2 * j - i;
      if (k >= N)
        continue;
      if (s[i] != s[j] && s[j] != s[k] && s[i] != s[k])
        count--;
    }
  }

  cout << count << endl;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
bool c[4002];
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;

  ll R = 0, B = 0, G = 0;
  vector<ll> a, b;
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'B') {
      c[i] = true;
    } else if (s[i] == 'R') {
      a.push_back(i);
    } else {
      b.push_back(i);
    }
  }
  ll ans = a.size() * b.size() * (n - a.size() - b.size());
  //	cout <<ans;
  for (ll i = 0; i < b.size(); i++) {
    ll now = b[i];
    //	cout << now<<endl;
    for (ll j = 0; j < a.size(); j++) {
      ll next = a[j];
      //	cout << next;
      ll dex = abs(now - next);
      if (c[next + dex] && next + dex < n) {
        ans--;
      }
      if (c[next - dex] && next - dex >= 0) {
        ans--;
      }
      if (c[now + dex] && now + dex < n) {
        ans--;
      }
      if (c[now - dex] && now - dex >= 0) {
        ans--;
      }
      if (dex % 2 == 0) {
        if (c[(now + next) / 2]) {
          ans--;
        }
      }
    }
    // cout <<ans;
  }
  cout << ans;
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
bool c[20002];
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;

  ll R = 0, B = 0, G = 0;
  vector<ll> a, b;
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'B') {
      c[i] = true;
    } else if (s[i] == 'R') {
      a.push_back(i);
    } else {
      b.push_back(i);
    }
  }
  ll ans = a.size() * b.size() * (n - a.size() - b.size());
  //	cout <<ans;
  for (ll i = 0; i < b.size(); i++) {
    ll now = b[i];
    //	cout << now<<endl;
    for (ll j = 0; j < a.size(); j++) {
      ll next = a[j];
      //	cout << next;
      ll dex = abs(now - next);
      if (c[next + dex] && next + dex < n) {
        ans--;
      }
      if (c[next - dex] && next - dex >= 0) {
        ans--;
      }
      if (c[now + dex] && now + dex < n) {
        ans--;
      }
      if (c[now - dex] && now - dex >= 0) {
        ans--;
      }
      if (dex % 2 == 0) {
        if (c[(now + next) / 2]) {
          ans--;
        }
      }
    }
    // cout <<ans;
  }
  cout << ans;
  // your code goes here
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define pb push_back
#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
signed main() {
  SPEED;
  int n, i, j, k;
  cin >> n;
  string s;
  cin >> s;
  int ans = 0;
  for (j = 1; j < n - 1; ++j) {
    char ch[1000] = {'0'};
    int cr = 0, cb = 0, cg = 0;
    for (i = 0; i < j; ++i) {
      if (s[j] != s[i]) {
        if (s[i] == 'R')
          cr++;
        else if (s[i] == 'B')
          cb++;
        else
          cg++;
        ch[j - i] = s[i];
      }
    }
    for (k = j + 1; k < n; ++k) {
      if (s[j] != s[k]) {
        if (s[k] == 'B') {
          if (ch[k - j] != 'R' && ch[k - j] != 'G')
            ans += cr + cg;
          else
            ans += cr + cg - 1;
        } else if (s[k] == 'R') {
          if (ch[k - j] != 'B' && ch[k - j] != 'G')
            ans += cb + cg;
          else
            ans += cb + cg - 1;
        } else if (s[k] == 'G') {
          if (ch[k - j] != 'B' && ch[k - j] != 'R')
            ans += cr + cb;
          else
            ans += cr + cb - 1;
        }
      }
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define pb push_back
#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
signed main() {
  SPEED;
  int n, i, j, k;
  cin >> n;
  string s;
  cin >> s;
  int ans = 0;
  for (j = 1; j < n - 1; ++j) {
    char ch[4000] = {'0'};
    int cr = 0, cb = 0, cg = 0;
    for (i = 0; i < j; ++i) {
      if (s[j] != s[i]) {
        if (s[i] == 'R')
          cr++;
        else if (s[i] == 'B')
          cb++;
        else
          cg++;
        ch[j - i] = s[i];
      }
    }
    for (k = j + 1; k < n; ++k) {
      if (s[j] != s[k]) {
        if (s[k] == 'B') {
          if (ch[k - j] != 'R' && ch[k - j] != 'G')
            ans += cr + cg;
          else
            ans += cr + cg - 1;
        } else if (s[k] == 'R') {
          if (ch[k - j] != 'B' && ch[k - j] != 'G')
            ans += cb + cg;
          else
            ans += cb + cg - 1;
        } else if (s[k] == 'G') {
          if (ch[k - j] != 'B' && ch[k - j] != 'R')
            ans += cr + cb;
          else
            ans += cr + cb - 1;
        }
      }
    }
  }
  cout << ans;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll n;
  string s;
  cin >> n >> s;

  vvll m(3);
  rep(i, n) {
    if (s[i] == 'R') {
      m[0].push_back(i);
    } else if (s[i] == 'G') {
      m[1].push_back(i);
    } else {
      m[2].push_back(i);
    }
  }

  ll ans = m[0].size() * m[1].size() * m[2].size();

  FORm(r, m[0]) {
    FORm(g, m[1]) {
      ll v = *r - *g;
      ll cand1 = *r + v;
      ll cand2 = *g - v;
      ll cand_max = max(cand1, cand2);
      bool is_even = (v % 2 == 0);
      ll cand3 = (*r + *g) / 2;
      FORm(b, m[2]) {
        if (*b > cand_max) {
          break;
        }
        if (*b == cand1) {
          ans--;
        } else if (*b == cand2) {
          ans--;
        } else if (is_even && *b == cand3) {
          ans--;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll n;
  string s;
  cin >> n >> s;

  vvll m(3);
  rep(i, n) {
    if (s[i] == 'R') {
      m[0].push_back(i);
    } else if (s[i] == 'G') {
      m[1].push_back(i);
    } else {
      m[2].push_back(i);
    }
  }

  ll ans = m[0].size() * m[1].size() * m[2].size();

  FORm(r, m[0]) {
    FORm(g, m[1]) {
      ll v = *r - *g;
      if (binary_search(all(m[2]), *r + v)) {
        ans--;
      }
      if (binary_search(all(m[2]), *g - v)) {
        ans--;
      }

      if (v % 2 == 0) {
        if (binary_search(all(m[2]), (*r + *g) / 2)) {
          ans--;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main(void) {
  IOS int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> r, g, b;
  REP(i, n) {
    if (s[i] == 'R')
      r.push_back(i);
    else if (s[i] == 'G')
      g.push_back(i);
    else
      b.push_back(i);
  }
  ll ans = 0;
  if (g.size() == 0) {
    OUT(0)
    return 0;
  }
  for (auto i : r) {
    for (auto j : g) {
      // if (i > j) swap(i,j);
      int d = j - i;
      int bs = b.size();
      auto it1 = lower_bound(ALL(b), i - d);
      if (*it1 == i - d)
        bs--;
      auto it2 = lower_bound(ALL(b), j + d);
      if (*it2 == j + d)
        bs--;
      if (d % 2 == 0) {
        auto it3 = lower_bound(ALL(b), i + d / 2);
        if (*it3 == i + d / 2)
          bs--;
      }
      ans += bs;
    }
  }

  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main(void) {
  IOS int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> r, g, b;
  REP(i, n) {
    if (s[i] == 'R')
      r.push_back(i);
    else if (s[i] == 'G')
      g.push_back(i);
    else
      b.push_back(i);
  }
  ll ans = 0;
  if (b.size() == 0) {
    OUT(0)
    return 0;
  }
  for (auto i : r) {
    for (auto j : g) {
      // if (i > j) swap(i,j);
      int d = j - i;
      int bs = b.size();
      auto it1 = lower_bound(ALL(b), i - d);
      if (*it1 == i - d)
        bs--;
      auto it2 = lower_bound(ALL(b), j + d);
      if (*it2 == j + d)
        bs--;
      if (d % 2 == 0) {
        auto it3 = lower_bound(ALL(b), i + d / 2);
        if (*it3 == i + d / 2)
          bs--;
      }
      ans += bs;
    }
  }

  cout << ans << '\n';

  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <iostream>
#include <math.h>
#include <sstream>
#include <string>
#include <string>
#include <vector>
using ll = long long;
using namespace std;
int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  vector<int> r, g, b;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R')
      r.push_back(i);
    else if (s[i] == 'G')
      g.push_back(i);
    else
      b.push_back(i);
  }
  long long ans = r.size() * g.size() * b.size();
  int count = 0;
  for (int i : r) {
    for (int j : b) {
      for (int k : g) {
        vector<int> temp = {i, j, k};
        sort(temp.begin(), temp.end());

        if (temp[2] - temp[1] == temp[1] - temp[0]) {
          count++;
        }
      }
    }
  }
  cout << ans - count << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <iostream>
#include <math.h>
#include <sstream>
#include <string>
#include <string>
#include <vector>
using ll = long long;
using namespace std;
int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  vector<int> r, g, b;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R')
      r.push_back(i);
    else if (s[i] == 'G')
      g.push_back(i);
    else
      b.push_back(i);
  }
  long long ans = r.size() * g.size() * b.size();
  int count = 0;
  for (int i : r) {
    for (int j : b) {
      int minn = min(i, j);
      int maxx = max(i, j);
      int d = maxx - minn;
      if (minn - d >= 0 && s[minn - d] == 'G')
        count++;
      if (maxx + d < N && s[maxx + d] == 'G')
        count++;
      if ((i + j) % 2 == 0 && s[(i + j) / 2] == 'G')
        count++;
    }
  }
  cout << ans - count << endl;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
// INCLUDE
//------------------------------------------
#include <bits/stdc++.h>
// DEFINE
//------------------------------------------
#define ll long long
#define ld long double
#define ALLv(a) (a).begin(), (a).end()
#define ALL(a, n) (a), (a) + n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
// CONST
//------------------------------------------
#define INF 1010000000000000017LL
#define MOD 1000000007LL
#define EPS 1e-12
#define PI 3.14159265358979323846
// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++)
#define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--)
#define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--)
#define WREP(i, in, j, jn) REP(i, in) REP(j, jn)
//-----------------------------------------
#define dcml(n) fixed << setprecision(n)
using namespace std;
int main(void) {
  ll N;
  string S;
  cin >> N >> S;
  ll cnt = 0;
  REP(i, N - 2) {
    FOR(j, i + 1, N - 1) {
      if (S[i] == S[j])
        continue;
      FOR(k, j + 1, N) {
        if (S[i] == S[k])
          continue;
        if (S[j] == S[k])
          continue;
        if (j - i == k - j)
          continue;
        cnt++;
      }
    }
  }
  cout << cnt << "\n";
}

Fixed submission:
// INCLUDE
//------------------------------------------
#include <bits/stdc++.h>
// DEFINE
//------------------------------------------
#define ll long long
#define ld long double
#define ALLv(a) (a).begin(), (a).end()
#define ALL(a, n) (a), (a) + n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
// CONST
//------------------------------------------
#define INF 1010000000000000017LL
#define MOD 1000000007LL
#define EPS 1e-12
#define PI 3.14159265358979323846
// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++)
#define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--)
#define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--)
#define WREP(i, in, j, jn) REP(i, in) REP(j, jn)
//-----------------------------------------
#define dcml(n) fixed << setprecision(n)
using namespace std;
int main(void) {
  ll N;
  string S;
  cin >> N >> S;
  ll cnt = 0;
  ll n1 = 0, n2 = 0, n3 = 0;
  REP(i, N) {
    if (S[i] == 'R')
      n1++;
    else if (S[i] == 'G')
      n2++;
    else
      n3++;
  }
  cnt = n1 * n2 * n3;
  REP(i, N) {
    for (ll k = 1; i + 2 * k < N; k++) {
      if (S[i] != S[i + k] && S[i + k] != S[i + 2 * k] && S[i + 2 * k] != S[i])
        cnt--;
    }
  }
  cout << cnt << "\n";
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;

  map<char, int> mp;
  mp['R'] = 0;
  mp['G'] = 1;
  mp['B'] = 2;

  vector<vector<int>> colors(3, vector<int>());
  for (int i = 0; i < n; i++) {
    colors[mp[s[i]]].push_back(i);
  }

  long long sum = 0;
  for (int i = 0; i < colors[0].size(); i++) {
    for (int j = 0; j < colors[1].size(); j++) {
      for (int k = 0; k < colors[2].size(); k++) {
        int ri = colors[0][i];
        int rj = colors[1][j];
        int rk = colors[2][k];
        if (ri > rj) {
          swap(ri, rj);
        }
        if (rj > rk) {
          swap(rj, rk);
        }
        if (ri > rj) {
          swap(ri, rj);
        }
        if (rj - ri != rk - rj) {
          sum++;
        }
      }
    }
  }

  printf("%lld\n", sum);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;

  map<char, int> mp;
  mp['R'] = 0;
  mp['G'] = 1;
  mp['B'] = 2;

  vector<vector<int>> colors(3, vector<int>());
  for (int i = 0; i < n; i++) {
    colors[mp[s[i]]].push_back(i);
  }

  long long sum = colors[0].size() * colors[1].size() * colors[2].size();

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n && (j - i) + j < n; j++) {
      int k = (j - i) + j;
      if (k < j) {
        continue;
      }
      if (s[i] != s[j] && s[i] != s[k] && s[k] != s[j]) {
        sum--;
      }
    }
  }

  printf("%lld\n", sum);
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define inf LLONG_MAX
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vll r, g, b;
  rep(i, s.size()) {
    if (s[i] == 'R')
      r.push_back(i);
    if (s[i] == 'G')
      g.push_back(i);
    if (s[i] == 'B')
      b.push_back(i);
  }
  // for (auto elem : r) cout << elem << " "; cout << endl;
  // for (auto elem : g) cout << elem << " "; cout << endl;
  // for (auto elem : b) cout << elem << " "; cout << endl;

  ll sum = 0;
  rep(i, r.size()) {
    rep(j, g.size()) {
      rep(k, b.size()) {
        vll _l;
        _l.push_back(r[i]);
        _l.push_back(g[j]);
        _l.push_back(b[k]);
        sort(all(_l));
        if (_l[2] - _l[1] != _l[1] - _l[0])
          sum++;
      }
    }
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define inf LLONG_MAX
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vll r, g, b;
  rep(i, s.size()) {
    if (s[i] == 'R')
      r.push_back(i);
    if (s[i] == 'G')
      g.push_back(i);
    if (s[i] == 'B')
      b.push_back(i);
  }
  // for (auto elem : r) cout << elem << " "; cout << endl;
  // for (auto elem : g) cout << elem << " "; cout << endl;
  // for (auto elem : b) cout << elem << " "; cout << endl;

  ll sum = 0;
  rep(i, r.size()) {
    rep(j, g.size()) {
      sum += b.size();
      if (r[i] < g[j]) {
        ll diff = g[j] - r[i];
        ll low = r[i] - (diff);
        if (0 <= low && s[low] == 'B')
          sum--;

        ll high = g[j] + diff;
        if (high < n && s[high] == 'B')
          sum--;

        ll middle = (r[i] + diff / 2);
        if (diff % 2 == 0 && s[middle] == 'B')
          sum--;
      } else {
        ll diff = r[i] - g[j];
        ll low = g[j] - diff;
        if (0 <= low && s[low] == 'B')
          sum--;

        ll high = r[i] + diff;
        if (high < n && s[high] == 'B')
          sum--;

        ll middle = (g[j] + diff / 2);
        if (diff % 2 == 0 && s[middle] == 'B')
          sum--;
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  vector<int> r;
  vector<int> g;
  vector<int> b;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R')
      r.push_back(i);
    else if (s[i] == 'G')
      g.push_back(i);
    else
      b.push_back(i);
  }

  int ans = 0;
  for (int i = 0; i < r.size(); ++i) {
    for (int j = 0; j < g.size(); ++j) {
      for (int k = 0; k < b.size(); k++) {
        vector<int> l;
        l.push_back(r[i]);
        l.push_back(g[j]);
        l.push_back(b[k]);
        sort(l.begin(), l.end());

        if (l[2] - l[1] != l[1] - l[0])
          ++ans;
      }
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  vector<int> r;
  vector<int> g;
  vector<int> b;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R')
      r.push_back(i);
    else if (s[i] == 'G')
      g.push_back(i);
    else
      b.push_back(i);
  }

  long long ans = r.size() * b.size() * g.size();
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i; j < n; ++j) {
      int k = j + j - i;
      if (k >= n)
        continue;
      if (s[i] != s[j] && s[j] != s[k] && s[i] != s[k])
        --ans;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
// garnab27
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define ll int64_t
#define F first
#define S second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
ll modex(ll a, ll b, ll p) {
  ll res = 1;
  a %= p;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b >>= 1;
  }
  return res;
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int r[4004], g[4004], b[4004];
int rs[4004], gs[4004], bs[4004];

ll count(int a[], int b[], int c[], int d[]) {
  ll ans = 0;

  for (int i = 0; i < 4001; i++) {
    for (int j = i + 1; j < 4001; j++) {
      if (a[i] && b[j]) {
        int nk = 2 * j - i;
        // cout<<i.F<<" "<<j.F<<" "<<nk<<"\n";
        ans += (d[j + 1] - c[nk]);
      }
    }
  }

  return ans;
}

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

  int n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r[i] = 1;
    else if (s[i] == 'G')
      g[i] = 1;
    else
      b[i] = 1;
  }

  for (int i = n - 1; i >= 0; i--) {
    rs[i] = r[i] + rs[i + 1];
    gs[i] = g[i] + gs[i + 1];
    bs[i] = b[i] + bs[i + 1];
  }

  ll ans = 0;

  // RGB
  ans += count(r, g, b, bs);
  // RBG
  ans += count(r, b, g, gs);
  // GBR
  ans += count(g, b, r, rs);
  // GRB
  ans += count(g, r, b, bs);
  // BRG
  ans += count(b, r, g, gs);
  // BGR
  ans += count(b, g, r, rs);

  cout << ans;

  return 0;
}

Fixed submission:
// garnab27
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define ll int64_t
#define F first
#define S second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
ll modex(ll a, ll b, ll p) {
  ll res = 1;
  a %= p;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b >>= 1;
  }
  return res;
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int r[4004], g[4004], b[4004];
int rs[4004], gs[4004], bs[4004];

ll count(int a[], int b[], int c[], int d[]) {
  ll ans = 0;

  for (int i = 0; i < 4001; i++) {
    for (int j = i + 1; j < 4001; j++) {
      if (a[i] && b[j]) {
        int nk = 2 * j - i;
        // cout<<i.F<<" "<<j.F<<" "<<nk<<"\n";
        ans += d[j + 1];
        if (nk < 4001)
          ans -= c[nk];
      }
    }
  }

  return ans;
}

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

  int n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r[i] = 1;
    else if (s[i] == 'G')
      g[i] = 1;
    else
      b[i] = 1;
  }

  for (int i = n - 1; i >= 0; i--) {
    rs[i] = r[i] + rs[i + 1];
    gs[i] = g[i] + gs[i + 1];
    bs[i] = b[i] + bs[i + 1];
  }

  ll ans = 0;

  // RGB
  ans += count(r, g, b, bs);
  // RBG
  ans += count(r, b, g, gs);
  // GBR
  ans += count(g, b, r, rs);
  // GRB
  ans += count(g, r, b, bs);
  // BRG
  ans += count(b, r, g, gs);
  // BGR
  ans += count(b, g, r, rs);

  cout << ans;

  return 0;
}
Problem p02714 (C++)

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

int main() {
  int N;
  string S;
  cin >> N >> S;

  vector<int> r;
  vector<int> g;
  vector<int> b;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R')
      r.push_back(i);
    else if (S[i] == 'G')
      g.push_back(i);
    else if (S[i] == 'B')
      b.push_back(i);
  }
  long long ans1 = 0;
  auto f = [&ans1](vector<int> &r, vector<int> &g, vector<int> &b) {
    for (int i = 0; i < r.size(); i++) {
      int j = upper_bound(g.begin(), g.end(), r[i]) - g.begin();
      for (; j < g.size(); j++) {
        auto it = lower_bound(b.begin(), b.end(), g[j] * 2 - r[i]);
        if (g[j] - r[i] == *it - g[j])
          ans1++;
      }
    }
  };
  f(r, g, b);
  f(r, b, g);
  f(g, b, r);
  f(g, r, b);
  f(b, r, g);
  f(b, g, r);
  long long ans = r.size() * g.size() * b.size() - ans1;

  cout << ans << endl;
}


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

int main() {
  int N;
  string S;
  cin >> N >> S;

  vector<int> r;
  vector<int> g;
  vector<int> b;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R')
      r.push_back(i);
    else if (S[i] == 'G')
      g.push_back(i);
    else if (S[i] == 'B')
      b.push_back(i);
  }
  long long ans1 = 0;
  auto f = [&ans1](vector<int> &r, vector<int> &g, vector<int> &b) {
    for (int i = 0; i < r.size(); i++) {
      int j = upper_bound(g.begin(), g.end(), r[i]) - g.begin();
      for (; j < g.size(); j++) {
        auto it = lower_bound(b.begin(), b.end(), g[j] * 2 - r[i]);
        if (it != b.end() && g[j] - r[i] == *it - g[j])
          ans1++;
      }
    }
  };
  f(r, g, b);
  f(r, b, g);
  f(g, b, r);
  f(g, r, b);
  f(b, r, g);
  f(b, g, r);
  long long ans = r.size() * g.size() * b.size() - ans1;

  cout << ans << endl;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  long long r = 0, g = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == 'R')
      r++;
    if (s.at(i) == 'G')
      g++;
    if (s.at(i) == 'B')
      b++;
  }

  int exc = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      for (int k = j + 1; k <= n; k++) {
        if (j - i == k - j && s.at(i - 1) != s.at(j - 1) &&
            s.at(j - 1) != s.at(k - 1) && s.at(k - 1) != s.at(i - 1))
          exc++;
      }
    }
  }

  cout << r * g * b - exc << endl;

  return 0;
}

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  long long r = 0, g = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == 'R')
      r++;
    if (s.at(i) == 'G')
      g++;
    if (s.at(i) == 'B')
      b++;
  }

  int exc = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if ((2 * j - i) < n) {
        if (s.at(i) != s.at(j) && s.at(j) != s.at(2 * j - i) &&
            s.at(2 * j - i) != s.at(i))
          exc++;
      }
    }
  }

  cout << r * g * b - exc << endl;

  return 0;
}