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

Buggy submission (status: Time Limit Exceeded):
#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

void solve(ll N, std::string S) {
  ll result = 0;
  ll cntR = 0;
  ll cntG = 0;
  ll cntB = 0;

  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'R')
      cntR++;
    if (S.at(i) == 'G')
      cntG++;
    if (S.at(i) == 'B')
      cntB++;
  }

  ll e = 0;

  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      if (S.at(i) == S.at(j))
        continue;
      for (int k = j + 1; k < N; k++) {
        if (S.at(k) == S.at(i) || S.at(k) == S.at(j))
          continue;
        if (j - i == k - j)
          e++;
      }
    }
  }

  result = cntR * cntG * cntB - e;

  cout << result << endl;
}

int main() {
  ll N;
  scanf("%lld", &N);
  std::string S;
  std::cin >> S;
  solve(N, S);
  return 0;
}


Fixed submission:
#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

void solve(ll N, std::string S) {
  ll result = 0;
  ll cntR = 0;
  ll cntG = 0;
  ll cntB = 0;

  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'R')
      cntR++;
    if (S.at(i) == 'G')
      cntG++;
    if (S.at(i) == 'B')
      cntB++;
  }

  ll e = 0;

  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      if (S.at(i) == S.at(j))
        continue;
      int k = 2 * j - i;
      if (k < N && S.at(i) != S.at(j) && S.at(j) != S.at(k) &&
          S.at(k) != S.at(i)) {
        e++;
      }
    }
  }

  result = cntR * cntG * cntB - e;

  cout << result << endl;
}

int main() {
  ll N;
  scanf("%lld", &N);
  std::string S;
  std::cin >> S;
  solve(N, S);
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int N;
string S;
int sum[3][4000];

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

  map<char, int> mm;
  char tmpc[3] = {'R', 'G', 'B'};
  REP(i, 3) mm[tmpc[i]] = i;

  REP(i, N) {
    ++sum[mm[S[i]]][i + 1];
    REP(j, 3) sum[j][i + 1] += sum[j][i];
  }

  ll ans = 0;
  for (int i = N - 1; i >= 0; --i) {
    for (int j = i - 1; j >= 0; --j) {
      if (S[i] == S[j])
        continue;
      bool f[3]{};
      f[mm[S[i]]] = true;
      f[mm[S[j]]] = true;

      int cc;
      REP(k, 3) if (!f[k]) cc = k;
      ans += sum[cc][j];

      int k = j - (i - j);
      if (k >= 0 && S[k] != S[i] && S[k] != S[j])
        --ans;
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int N;
string S;
int sum[3][4001];

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

  map<char, int> mm;
  char tmpc[3] = {'R', 'G', 'B'};
  REP(i, 3) mm[tmpc[i]] = i;

  REP(i, N) {
    ++sum[mm[S[i]]][i + 1];
    REP(j, 3) sum[j][i + 1] += sum[j][i];
  }

  ll ans = 0;
  for (int i = N - 1; i >= 0; --i) {
    for (int j = i - 1; j >= 0; --j) {
      if (S[i] == S[j])
        continue;
      bool f[3]{};
      f[mm[S[i]]] = true;
      f[mm[S[j]]] = true;

      int cc;
      REP(k, 3) if (!f[k]) cc = k;
      ans += sum[cc][j];

      int k = j - (i - j);
      if (k >= 0 && S[k] != S[i] && S[k] != S[j])
        --ans;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <tuple>

#define INF INT_MAX >> 1
#define SIZE 100010
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, from, to) for (int i = (from); i < (int)(to); i++)
#define all(obj) (obj).begin(), (obj).end()
#define lpair pair<ll, ll>
#define vint vector<ll>
#define vinput(name)                                                           \
  for (int i = 0; i < (int)(name.size()); i++)                                 \
    cin >> name[i];
#define voutput(name)                                                          \
  for (int i = 0; i < (int)(name.size()); i++)                                 \
    cout << name[i] << endl;
typedef long long ll;

using namespace std;

ll ans = 0;

int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vint R, G, B;
  rep(i, n) {
    if (s[i] == 'R')
      R.push_back(i + 1);
    if (s[i] == 'G')
      G.push_back(i + 1);
    if (s[i] == 'B')
      B.push_back(i + 1);
  }

  rep(r, R.size()) {
    rep(g, G.size()) {
      rep(b, B.size()) {
        vint v;
        v.push_back(R[r]);
        v.push_back(G[g]);
        v.push_back(B[b]);
        sort(all(v));
        if (v[1] - v[0] != v[2] - v[1])
          ans++;
      }
    }
  }

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

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <tuple>

#define INF INT_MAX >> 1
#define SIZE 100010
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, from, to) for (int i = (from); i < (int)(to); i++)
#define all(obj) (obj).begin(), (obj).end()
#define lpair pair<ll, ll>
#define vint vector<ll>
#define vinput(name)                                                           \
  for (int i = 0; i < (int)(name.size()); i++)                                 \
    cin >> name[i];
#define voutput(name)                                                          \
  for (int i = 0; i < (int)(name.size()); i++)                                 \
    cout << name[i] << endl;
typedef long long ll;

using namespace std;

ll ans = 0;

int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vint R, G, B;
  rep(i, n) {
    if (s[i] == 'R')
      R.push_back(i + 1);
    if (s[i] == 'G')
      G.push_back(i + 1);
    if (s[i] == 'B')
      B.push_back(i + 1);
  }

  ll ans = R.size() * G.size() * B.size();
  rep(i, n) {
    for (int j = 1; i - j >= 0 && i + j < n; j++) {
      if (s[i - j] != s[i] && s[i + j] != s[i] && s[i - j] != s[i + j]) {
        ans--;
      }
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

ll count(vector<int> &R, vector<int> &G, vector<int> &B) {
  ll sum = 0;
  for (int i = 0; i < R.size(); i++) {
    for (int j = 0; j < G.size(); j++) {
      if (R[i] > G[j]) {
        continue;
      }
      int a = G[j] - R[i];
      for (int k = 0; k < B.size(); k++) {
        if (G[j] > B[k]) {
          continue;
        }
        if (a == B[k] - G[j]) {
          continue;
        }
        sum++;
      }
    }
  }
  return sum;
}

int main() {
  int N;
  char S;
  vector<int> R, G, B;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S;
    if (S == 'R') {
      R.push_back(i);
    } else if (S == 'G') {
      G.push_back(i);
    } else if (S == 'B') {
      B.push_back(i);
    }
  }
  ll sum = 0;
  sum += count(R, B, G);
  sum += count(R, G, B);
  sum += count(B, R, G);
  sum += count(B, G, R);
  sum += count(G, R, B);
  sum += count(G, B, R);
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

ll count(vector<int> &R, vector<int> &G, vector<int> &B) {
  ll sum = 0;
  for (int i = 0; i < R.size(); i++) {
    for (auto j = std::lower_bound(G.begin(), G.end(), R[i]); j < G.end();
         j++) {
      int a = *j - R[i];
      for (auto k = std::lower_bound(B.begin(), B.end(), *j); k < B.end();
           k++) {
        if (a == *k - *j) {
          continue;
        }
        sum++;
      }
    }
  }
  return sum;
}

int main() {
  int N;
  char S;
  vector<int> R, G, B;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S;
    if (S == 'R') {
      R.push_back(i);
    } else if (S == 'G') {
      G.push_back(i);
    } else if (S == 'B') {
      B.push_back(i);
    }
  }
  ll sum = 0;
  sum += count(R, B, G);
  sum += count(R, G, B);
  sum += count(B, R, G);
  sum += count(B, G, R);
  sum += count(G, R, B);
  sum += count(G, B, R);
  cout << sum << endl;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int N;
  char S[4000];
  long long int count = 0;
  long long int r, g, b;
  r = 0;
  g = 0;
  b = 0;
  long long int set, ans;

  cin >> N;
  cin >> S;

  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      r++;
    }
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == 'G') {
      g++;
    }
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == 'B') {
      b++;
    }
  }

  set = r * g * b;

  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      long long int k = j * 2 - i;
      if ((S[i] != S[j]) && (S[i] != S[k]) && (S[j] != S[k]) && k < N) {
        count++;
      }
    }
  }

  ans = set - count;

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int N;
  char S[4000];
  long long int count = 0;
  long long int r, g, b;
  r = 0;
  g = 0;
  b = 0;
  long long int set, ans;

  cin >> N;
  cin >> S;

  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      r++;
    }
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == 'G') {
      g++;
    }
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == 'B') {
      b++;
    }
  }

  set = r * g * b;

  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      long long int k = j * 2 - i;
      if ((S[i] != S[j]) && (S[i] != S[k]) && (S[j] != S[k]) && k < N) {
        count++;
      }
    }
  }

  ans = set - count;

  cout << ans << endl;

  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  string S;
  cin >> N;
  cin >> S;
  int sum = 0;
  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      for (int k = j + 1; k < N; k++) {
        if (S.at(j) != S.at(k) && S.at(i) != S.at(k) && S.at(j) != S.at(i)) {
          if ((j - i) != (k - j)) {
            sum++;
          }
        }
      }
    }
  }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  string s;
  cin >> N >> s;
  long long int r, g, b;
  r = 0, g = 0, b = 0;
  for (char ch : s) {
    if (ch == 'R')
      r++;
    else if (ch == 'G')
      g++;
    else if (ch == 'B')
      b++;
  }
  long long int sum = r * g * b;
  for (int i = 0; i < N; i++) {
    for (int k = i + 2; k < N; k += 2) {
      int j = (i + k) / 2;
      if (s.at(i) + s.at(j) + s.at(k) == 'R' + 'G' + 'B') {
        sum--;
      }
    }
  }
  cout << sum << endl;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  ll N, i, j, k, ans;
  string S;
  cin >> N >> S;
  ans = 0;

  for (i = 0; i < N - 2; i++) {
    for (j = i + 1; j < N - 1; j++) {
      for (k = j + 1; k < N; k++) {
        if (S[i] != S[j] && S[j] != S[k] && S[k] != S[i] && j - i != k - j)
          ans++;
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  ll N, i, j, k, ans;
  string S;
  cin >> N >> S;
  ans = 0;

  for (i = 0; i < N - 2; i++) {
    for (j = i + 1; j < N - 1; j++) {
      for (k = j + 1; k < N; k++) {
        if (S[i] != S[j] && S[j] != S[k] && S[k] != S[i] && j - i != k - j)
          ans++;
      }
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
unordered_map<char, int> mp;
const int N = 40010;
int a[4][N];
int main() {
  int n;
  string s;
  cin >> n >> s;
  mp['R'] = 1, mp['G'] = 2, mp['B'] = 3;
  s = " " + s;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 3; j++)
      a[j][i] = a[j][i - 1];
    a[mp[s[i]]][i]++;
  }
  long long cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (s[i] == s[j])
        continue;
      int x = 6 - mp[s[i]] - mp[s[j]];
      cnt += a[x][n] - a[x][j] - (mp[s[2 * j - i]] == x);
    }
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
unordered_map<char, int> mp;
const int N = 40010;
int a[4][N];
int main() {
  int n;
  string s;
  cin >> n >> s;
  mp['R'] = 1, mp['G'] = 2, mp['B'] = 3;
  s = " " + s;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 3; j++)
      a[j][i] = a[j][i - 1];
    a[mp[s[i]]][i]++;
  }
  long long cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (s[i] == s[j])
        continue;
      int x = 6 - mp[s[i]] - mp[s[j]];

      cnt += a[x][n] - a[x][j];
      if (2 * j - i <= n)
        cnt -= (mp[s[2 * j - i]] == x);
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02714 (C++)

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

int main() {
  int n;
  string s;

  cin >> n;
  cin >> s;

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

  cout << t << endl;
  return 0;
}


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

int main() {
  int n;
  string s;

  cin >> n;
  cin >> s;

  long long rt = 0;
  long long gt = 0;
  long long bt = 0;

  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      rt += 1;
    }
    if (s[i] == 'G') {
      gt += 1;
    }
    if (s[i] == 'B') {
      bt += 1;
    }
  }

  long long t = rt * gt * bt;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      int k = 2 * j - i;
      if (k < n && s[i] != s[j] && s[i] != s[k] && s[j] != s[k]) {
        t--;
      }
    }
  }

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

Buggy submission (status: Runtime Error):
// agrawal117
// chahatagrawal117
#include <bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 100005
vector<ll> v[4];
int idx(char a, char b) {
  if (b == 'R')
    swap(a, b);
  if (a == 'R' && b == 'G')
    return 3;
  if (a == 'R' && b == 'B')
    return 2;
  else
    return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'R')
      v[1].push_back(i);
    else if (s[i] == 'G')
      v[2].push_back(i);
    else
      v[3].push_back(i);
  }
  ll ans = 0;
  for (int j = 1; j < s.size() - 1; j++) {
    for (int i = 0; i < j; i++) {
      if (s[j] != s[i]) {
        int kk = idx(s[i], s[j]);
        int k = lower_bound(v[kk].begin(), v[kk].end(), j) - v[kk].begin();
        ans += (v[kk].size() - k);
        k = lower_bound(v[kk].begin(), v[kk].end(), 2 * j - i) - v[kk].begin();
        if (v[kk][k] == (2 * j) - i)
          ans--;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
// agrawal117
// chahatagrawal117
#include <bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 100005
vector<ll> v[4];
int idx(char a, char b) {
  if (b == 'R')
    swap(a, b);
  if (a == 'R' && b == 'G')
    return 3;
  if (a == 'R' && b == 'B')
    return 2;
  else
    return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'R')
      v[1].push_back(i);
    else if (s[i] == 'G')
      v[2].push_back(i);
    else
      v[3].push_back(i);
  }
  ll ans = 0;
  for (int j = 1; j < s.size() - 1; j++) {
    for (int i = 0; i < j; i++) {
      if (s[j] != s[i]) {
        int kk = idx(s[i], s[j]);
        int k = lower_bound(v[kk].begin(), v[kk].end(), j) - v[kk].begin();
        ans += (v[kk].size() - k);
        k = lower_bound(v[kk].begin(), v[kk].end(), 2 * j - i) - v[kk].begin();
        if (k != v[kk].size() && v[kk][k] == (2 * j) - i)
          ans--;
      }
    }
  }
  cout << ans << endl;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<ll, ll>
#define nyan "(=^･ω･^=)"
#define read_input freopen("in.txt", "r", stdin)
#define print_output freopen("out.txt", "w", stdout)
typedef long long ll;
typedef long double ld;
using namespace std;

const ll maxn = 4e3 + 10;
ll pre[4][maxn], a[maxn];

ll other(ll a, ll b) {
  if (a > b)
    swap(a, b);
  if (a == 1 && b == 2)
    return 3;
  if (a == 1 && b == 3)
    return 2;
  if (a == 2 && b == 3)
    return 1;
}

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

  for (ll i = 1; i <= n; i++) {
    if (s[i - 1] == 'R')
      pre[1][i]++, a[i] = 1;
    if (s[i - 1] == 'G')
      pre[2][i]++, a[i] = 2;
    if (s[i - 1] == 'B')
      pre[3][i]++, a[i] = 3;
  }

  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= 3; j++)
      pre[j][i] += pre[j][i - 1];
  }

  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = i + 1; j <= n; j++) {
      if (a[i] == a[j])
        continue;

      ll c = other(a[i], a[j]);

      ans += pre[c][i - 1];
      ans -= a[2 * i - j] == c;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<ll, ll>
#define nyan "(=^･ω･^=)"
#define read_input freopen("in.txt", "r", stdin)
#define print_output freopen("out.txt", "w", stdout)
typedef long long ll;
typedef long double ld;
using namespace std;

const ll maxn = 4e3 + 10;
ll pre[4][maxn], a[maxn];

ll other(ll a, ll b) {
  if (a > b)
    swap(a, b);
  if (a == 1 && b == 2)
    return 3;
  if (a == 1 && b == 3)
    return 2;
  if (a == 2 && b == 3)
    return 1;
}

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

  for (ll i = 1; i <= n; i++) {
    if (s[i - 1] == 'R')
      pre[1][i]++, a[i] = 1;
    if (s[i - 1] == 'G')
      pre[2][i]++, a[i] = 2;
    if (s[i - 1] == 'B')
      pre[3][i]++, a[i] = 3;
  }

  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= 3; j++)
      pre[j][i] += pre[j][i - 1];
  }

  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = i + 1; j <= n; j++) {
      if (a[i] == a[j])
        continue;

      ll c = other(a[i], a[j]);

      ans += pre[c][i - 1];
      if (2 * i - j >= 1 && 2 * i - j <= n)
        ans -= a[2 * i - j] == c;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02714 (C++)

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

int N;
string A;
int B[10005];
int pre[10005][5];
long long ans;

int main() {
  cin >> N >> A;
  for (int i = 0; i < N; i++) {
    B[i + 1] = (A[i] == 'R' ? 1 : A[i] == 'G' ? 2 : 3);
    pre[i + 1][B[i + 1]] = 1;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= 3; j++) {
      pre[i][j] += pre[i - 1][j];
    }
  }
  int cari;
  for (int i = 1; i = N - 2; i++) {
    for (int j = i + 1; j <= N - 1; j++) {
      if (B[i] == B[j])
        continue;
      cari = B[i] ^ B[j];
      ans += pre[N][cari] - pre[j][cari] - (B[j + j - i] == cari ? 1 : 0);
    }
  }
  cout << ans << "\n";
}

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

int N;
string A;
int B[10005];
int pre[10005][5];
long long ans;

int main() {
  cin >> N >> A;
  for (int i = 0; i < N; i++) {
    B[i + 1] = (A[i] == 'R' ? 1 : A[i] == 'G' ? 2 : 3);
    pre[i + 1][B[i + 1]] = 1;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= 3; j++) {
      pre[i][j] += pre[i - 1][j];
    }
  }
  int cari;
  for (int i = 1; i <= N - 2; i++) {
    for (int j = i + 1; j <= N - 1; j++) {
      if (B[i] == B[j])
        continue;
      cari = B[i] ^ B[j];
      ans += pre[N][cari] - pre[j][cari] - (B[j + j - i] == cari ? 1 : 0);
    }
  }
  cout << ans << "\n";
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f first
#define s second
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fast
#define f first
#define s second
#define endl "\n"
#define pie 3.1415926535
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

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

  long long sum = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      if (s[i] == s[j])
        continue;
      for (int k = j + 1; k < n; k++) {
        if ((j - i) == (k - j) || s[j] == s[k] || s[i] == s[k])
          continue;
        else
          sum++;
      }
    }
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f first
#define s second
#define pb push_back
#define mod 1000000007
#define mp make_pair
#define fast
#define f first
#define s second
#define endl "\n"
#define pie 3.1415926535
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

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

  long long sum = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      if (s[i] == s[j])
        continue;
      for (int k = j + 1; k < n; k++) {
        if ((j - i) == (k - j) || s[j] == s[k] || s[i] == s[k])
          continue;
        else
          sum++;
      }
    }
  }
  cout << sum << endl;
  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;
  std::map<int, int> r;
  std::map<int, int> g;
  std::map<int, int> b;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      r.emplace(i + 1, 1);
    } else if (s[i] == 'G') {
      g.emplace(i + 1, 1);
    } else {
      b.emplace(i + 1, 1);
    }
  }
  long long int sum = r.size() * g.size() * b.size();
  long long int count = 0;
  for (auto itr = r.begin(); itr != r.end(); itr++) {
    for (int i = 1; i < n; i++) {
      if (g[itr->first + i] == 1 && b[itr->first + i + i] == 1) {
        count++;
        // cout<<itr->first<<" "<<itr->first+i<<" "<<itr->first+i+i<<endl;
      }
      if (g[itr->first + i] == 1 && b[itr->first - i] == 1) {
        count++;
      }
      if (g[itr->first - i] == 1 && b[itr->first + i] == 1) {
        count++;
      }
      if (g[itr->first - i] == 1 && b[itr->first - i - i] == 1) {
        count++;
        // cout<<itr->first<<" "<<itr->first-i<<" "<<itr->first-i-i<<endl;
      }
      if (g[itr->first - i - i] == 1 && b[itr->first - i] == 1) {
        count++;
      }
      if (g[itr->first + i + i] == 1 && b[itr->first + i] == 1) {
        count++;
      }
    }
  }
  cout << sum - count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  std::map<int, int> r;
  std::map<int, int> g;
  std::map<int, int> b;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      r.emplace(i + 1, 1);
    } else if (s[i] == 'G') {
      g.emplace(i + 1, 1);
    } else {
      b.emplace(i + 1, 1);
    }
  }
  long long int sum = r.size() * g.size() * b.size();
  long long int count = 0;
  for (auto itr = r.begin(); itr != r.end(); itr++) {
    for (int i = 1; abs(itr->first - i - i) <= n; i++) {
      if (g[itr->first + i] == 1 && b[itr->first + i + i] == 1) {
        count++;
        // cout<<itr->first<<" "<<itr->first+i<<" "<<itr->first+i+i<<endl;
      }
      if (g[itr->first + i] == 1 && b[itr->first - i] == 1) {
        count++;
      }
      if (g[itr->first - i] == 1 && b[itr->first + i] == 1) {
        count++;
      }
      if (g[itr->first - i] == 1 && b[itr->first - i - i] == 1) {
        count++;
        // cout<<itr->first<<" "<<itr->first-i<<" "<<itr->first-i-i<<endl;
      }
      if (g[itr->first - i - i] == 1 && b[itr->first - i] == 1) {
        count++;
      }
      if (g[itr->first + i + i] == 1 && b[itr->first + i] == 1) {
        count++;
      }
    }
  }
  cout << sum - count << endl;
}
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> v[3];

  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R')
      v[0].push_back(i);
    if (s[i] == 'G')
      v[1].push_back(i);
    if (s[i] == 'B')
      v[2].push_back(i);
  }

  auto check = [&](vector<int> a, vector<int> b, vector<int> c) {
    long long aa = 0;
    for (int l : a) {
      for (int m : b) {
        if (m < l)
          continue;
        int d = m - l;
        int pos = upper_bound(c.begin(), c.end(), m) - c.begin();
        int cur = lower_bound(c.begin(), c.end(), m + d) - c.begin();
        aa += c.size() - pos;
        if (c[cur] == m + d)
          aa--;
      }
    }
    return aa;
  };

  long long ans = 0;
  vector<int> bb = {0, 1, 2};
  do {
    ans += check(v[bb[0]], v[bb[1]], v[bb[2]]);
  } while (next_permutation(bb.begin(), bb.end()));

  cout << ans << "\n";

  return 0;
}


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

using namespace std;

int main() {

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

  vector<int> v[3];

  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R')
      v[0].push_back(i);
    if (s[i] == 'G')
      v[1].push_back(i);
    if (s[i] == 'B')
      v[2].push_back(i);
  }

  auto check = [&](vector<int> a, vector<int> b, vector<int> c) {
    long long aa = 0;
    for (int l : a) {
      for (int m : b) {
        if (m < l)
          continue;
        int d = m - l;
        int pos = upper_bound(c.begin(), c.end(), m) - c.begin();
        int cur = lower_bound(c.begin(), c.end(), m + d) - c.begin();
        aa += c.size() - pos;
        if (cur < (int)c.size() and c[cur] == m + d)
          aa--;
      }
    }
    return aa;
  };

  long long ans = 0;
  vector<int> bb = {0, 1, 2};
  do {
    ans += check(v[bb[0]], v[bb[1]], v[bb[2]]);
  } while (next_permutation(bb.begin(), bb.end()));

  cout << ans << "\n";

  return 0;
}
Problem p02714 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
S = input()
exc = 0
ans = S.count("R") * S.count("G") * S.count("B")

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j + (j - i)
        if N - 1 < k:
            continue
        if S[k] != S[i] and S[k] != S[j] and S[i] != S[j]:
            ans -= 1

print(ans)


Fixed submission:
N = int(input())
S = [s for s in input()]
ans = S.count("R") * S.count("G") * S.count("B")

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j + (j - i)
        if N - 1 < k:
            continue
        if S[k] != S[i] and S[k] != S[j] and S[i] != S[j]:
            ans -= 1

print(ans)
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long N;
string s;
int main() {
  cin >> N;
  cin >> s;
  long long total = 0;
  for (int i = 0; i <= N - 3; i++) {
    for (int j = i + 1; j <= N - 2; j++) {
      if (s[i] == s[j]) {
        continue;
      }
      for (int k = j + 1; k <= N - 1; k++) {
        if (s[k] != s[j] && s[k] != s[i]) {
          if (k - j != j - i) {
            total++;
          }
        }
      }
    }
  }
  cout << total << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long N;
string s;
int main() {
  cin >> N;
  cin >> s;
  long long 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++;
    }
  }
  long long total = r * g * b;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      long long X = 2 * j - i;
      if (X < N) {
        if (s[i] != s[j] && s[j] != s[X] && s[i] != s[X]) {
          total--;
        }
      }
    }
  }
  cout << total << '\n';
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)                                                              \
  ;                                                                            \
  for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  long long cnt = 0;

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

  cout << cnt << endl;
}

Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)                                                              \
  ;                                                                            \
  for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  string st;
  int s[n];
  cin >> st;
  rep(i, n) {
    if (st[i] == 'R')
      s[i] = 0;
    else if (st[i] == 'G')
      s[i] = 1;
    else
      s[i] = 2;
  }
  long long cnt = 0;

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

  cout << cnt << endl;
}
Problem p02714 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
s = input()
rr = 0
gg = 0
bb = 0
for i in range(n):
    if s[i] == "R":
        rr += 1
    if s[i] == "G":
        gg += 1
    if s[i] == "B":
        bb += 1

ans = rr * gg * bb

for i in range(n):
    for j in range(n):
        if j - i >= 0 and j + i < n:
            if s[j - i] != s[j] and s[j] != s[j + i] and s[j + i] != s[j - i]:
                ans -= 1
print(ans)


Fixed submission:
n = int(input())
s = input()
rr = 0
gg = 0
bb = 0
for i in range(n):
    if s[i] == "R":
        rr += 1
    if s[i] == "G":
        gg += 1
    if s[i] == "B":
        bb += 1

ans = rr * gg * bb

for j in range(n):
    for i in range(min(j + 1, n - j)):
        if s[j - i] != s[j] and s[j] != s[j + i] and s[j + i] != s[j - i]:
            ans -= 1
print(ans)
Problem p02714 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
S = input()

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'

n_R = S.count("R")
n_G = S.count("G")
n_B = S.count("B")
ans = n_R * n_G * n_B

for a in range(N - 2):
    # a = 2
    for b in range(1, N // 2 + 1):
        # b = 3
        i = a
        j = a + b
        k = a + 2 * b

        if k > N - 1:
            continue

        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1
print(ans)


Fixed submission:
N = int(input())
S = input()

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'

n_R = S.count("R")
n_G = S.count("G")
n_B = S.count("B")
ans = n_R * n_G * n_B

for b in range(1, N // 2 + 1):
    # b = 2
    for a in range(N - 2 * b):
        # a = 1
        i = a
        j = a + b
        k = a + 2 * b

        if k > N - 1:
            continue

        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1
print(ans)
Problem p02714 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter
from copy import copy

n = int(input())
s = input()

total_cnt = dict(Counter(s))
cnt = [total_cnt]
for i, c in enumerate(s):
    curr_cnt = copy(cnt[-1])
    curr_cnt[c] -= 1
    cnt.append(curr_cnt)

res = 0
for i, c1 in enumerate(s):
    for j in range(i + 1, n - 1):
        c2 = s[j]
        if c2 == c1:
            continue
        for c3 in "RGB":
            if c3 == c1 or c3 == c2:
                continue
            res += cnt[j + 1][c3]
            k = 2 * j - i
            if k >= n:
                continue
            if s[k] == c3:
                res -= 1
print(res)


Fixed submission:
from collections import Counter
from copy import copy

n = int(input())
s = input()

total_cnt = dict(Counter(s))
if len(total_cnt) < 3:
    print(0)
    exit()

cnt = [total_cnt]
for i, c in enumerate(s):
    curr_cnt = copy(cnt[-1])
    curr_cnt[c] -= 1
    cnt.append(curr_cnt)

res = 0
for i, c1 in enumerate(s):
    for j in range(i + 1, n - 1):
        c2 = s[j]
        if c2 == c1:
            continue
        for c3 in "RGB":
            if c3 == c1 or c3 == c2:
                continue
            res += cnt[j + 1][c3]
            k = 2 * j - i
            if k >= n:
                continue
            if s[k] == c3:
                res -= 1
print(res)
Problem p02714 (Python)

Buggy submission (status: Runtime Error):
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10**9 + 7
INF = float("inf")


def main():
    N = int(input())
    S = input()
    C = Counter(S)
    r = C.get("R")
    g = C.get("G")
    b = C.get("B")
    answer = r * g * b
    for left in range(N):
        for mid in range(left + 1, N):
            right = mid * 2 - left
            if right < N:
                if S[right] != S[mid] and S[mid] != S[left] and S[right] != S[left]:
                    answer -= 1
    print(answer)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10**9 + 7
INF = float("inf")


def main():
    N = int(input())
    S = input()
    C = Counter(S)
    r = C.get("R", 0)
    g = C.get("G", 0)
    b = C.get("B", 0)
    answer = r * g * b
    for left in range(N):
        for mid in range(left + 1, N):
            right = mid * 2 - left
            if right < N:
                if S[right] != S[mid] and S[mid] != S[left] and S[right] != S[left]:
                    answer -= 1
    print(answer)


if __name__ == "__main__":
    main()
Problem p02714 (C++)

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

int main() {
  int64_t N;
  string S;
  cin >> N;
  cin >> S;
  vector<int64_t> R, G, B;
  for (int64_t i = 0LL; i < N; i++) {
    if (S.at(i) == 'R') {
      R.push_back(i);
    }
    if (S.at(i) == 'G') {
      G.push_back(i);
    }
    if (S.at(i) == 'B') {
      B.push_back(i);
    }
  }
  int64_t r = R.size();
  int64_t g = G.size();
  int64_t b = B.size();
  int64_t Ans = r * g * b;
  int64_t Max, Middle, Min;
  for (int64_t i = 0LL; i < r; i++) {
    for (int64_t j = 0LL; j < g; j++) {
      for (int64_t k = 0LL; k < b; k++) {
        Max = max(R.at(i), max(G.at(j), B.at(k)));
        Min = min(R.at(i), min(G.at(j), B.at(k)));
        Middle = R.at(i) + G.at(j) + B.at(k) - Max - Min;
        if (Max - Middle == Middle - Min) {
          Ans--;
        }
      }
    }
  }
  cout << Ans << endl;
}

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

int main() {
  int64_t N;
  string S;
  cin >> N;
  cin >> S;
  vector<int64_t> R, G, B;
  for (int64_t i = 0LL; i < N; i++) {
    if (S.at(i) == 'R') {
      R.push_back(i);
    }
    if (S.at(i) == 'G') {
      G.push_back(i);
    }
    if (S.at(i) == 'B') {
      B.push_back(i);
    }
  }
  int64_t r = R.size();
  int64_t g = G.size();
  int64_t b = B.size();
  int64_t Ans = r * g * b;
  int64_t Max, Middle, Min;
  for (int64_t i = 0LL; i < r; i++) {
    for (int64_t j = 1LL; j <= (N - R.at(i) - 1) / 2; j++) {
      if (S.at(R.at(i) + j) == 'G' && S.at(R.at(i) + 2 * j) == 'B') {
        Ans--;
      }
      if (S.at(R.at(i) + j) == 'B' && S.at(R.at(i) + 2 * j) == 'G') {
        Ans--;
      }
    }
  }
  for (int64_t i = 0LL; i < g; i++) {
    for (int64_t j = 1LL; j <= (N - G.at(i) - 1) / 2; j++) {
      if (S.at(G.at(i) + j) == 'B' && S.at(G.at(i) + 2 * j) == 'R') {
        Ans--;
      }
      if (S.at(G.at(i) + j) == 'R' && S.at(G.at(i) + 2 * j) == 'B') {
        Ans--;
      }
    }
  }
  for (int64_t i = 0LL; i < b; i++) {
    for (int64_t j = 1LL; j <= (N - B.at(i) - 1) / 2; j++) {
      if (S.at(B.at(i) + j) == 'R' && S.at(B.at(i) + 2 * j) == 'G') {
        Ans--;
      }
      if (S.at(B.at(i) + j) == 'G' && S.at(B.at(i) + 2 * j) == 'R') {
        Ans--;
      }
    }
  }
  cout << Ans << endl;
}
Problem p02714 (C++)

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

using namespace std;

using ll = long long;

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll r = 0, g = 0, b = 0, ans = 0;
  for (ll i = 0; i < n - 2; ++i) {
    for (ll j = i + 1; j < n - 1; ++j) {
      for (ll k = j + 1; k < n; ++k) {
        if ((s[i] != s[j] && s[i] != s[k] && s[j] != s[k]) &&
            (j - i != k - j)) {
          ans++;
        }
      }
    }
  }
  cout << ans;
  return 0;
}


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

using namespace std;

using ll = long long;

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

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#include <bits/stdc++.h>

int main() {
  int n;
  string s;
  cin >> n >> s;
  long res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (s[i] == s[j])
        continue;
      for (int k = 0; k < j; k++)
        if (j - k != i - j && s[k] != s[i] && s[k] != s[j])
          res++;
    }
  }
  cout << res;
  return 0;
}

Fixed submission:
#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#include <bits/stdc++.h>

int main() {
  int n;
  string s;
  cin >> n >> s;
  long res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (s[i] == s[j])
        continue;
      for (int k = 0; k < j; k++)
        if (j - k != i - j && s[k] != s[i] && s[k] != s[j])
          res++;
    }
  }
  cout << res;
  return 0;
}
Problem p02714 (C++)

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

int main() {
  int N;
  string S;
  cin >> N >> S;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (S.at(i) != S.at(j)) {
        int x = j - i;
        for (int k = j + 1; k < N; k++) {
          if (S.at(i) != S.at(k) && S.at(j) != S.at(k) && x != (k - j)) {
            ans++;
          }
        }
      }
    }
  }
  cout << ans << endl;
}

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
typedef long long ll;

ll n;
char s[4444];
ll SR[4444], SB[4444], SG[4444];
ll Out;

int main() {
  scanf("%lld", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    SR[i] = SR[i - 1];
    SB[i] = SB[i - 1];
    SG[i] = SG[i - 1];
    if (s[i] == 'R')
      SR[i]++;
    else if (s[i] == 'B')
      SB[i]++;
    else
      SG[i]++;
  }
  for (int i = 1; i < n - 1; i++) {
    for (int j = i + 1; j <= n - 1; j++) {
      int Dis = j - i;
      if ((s[i] == 'R' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'R')) {
        Out += (SG[n] - SG[j]);
        if (s[j + Dis] == 'G')
          Out--;
      } else if ((s[i] == 'R' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'R')) {
        Out += (SB[n] - SB[j]);
        if (s[j + Dis] == 'B')
          Out--;
      } else if ((s[i] == 'G' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'G')) {
        Out += (SR[n] - SR[j]);
        if (s[j + Dis] == 'R')
          Out--;
      }
    }
  }
  printf("%lld\n", Out);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
typedef long long ll;

ll n;
char s[444444];
ll SR[444444], SB[444444], SG[444444];
ll Out;

int main() {
  scanf("%lld", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    SR[i] = SR[i - 1];
    SB[i] = SB[i - 1];
    SG[i] = SG[i - 1];
    if (s[i] == 'R')
      SR[i]++;
    else if (s[i] == 'B')
      SB[i]++;
    else
      SG[i]++;
  }
  for (int i = 1; i < n - 1; i++) {
    for (int j = i + 1; j <= n - 1; j++) {
      int Dis = j - i;
      if ((s[i] == 'R' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'R')) {
        Out += (SG[n] - SG[j]);
        if (s[j + Dis] == 'G')
          Out--;
      } else if ((s[i] == 'R' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'R')) {
        Out += (SB[n] - SB[j]);
        if (s[j + Dis] == 'B')
          Out--;
      } else if ((s[i] == 'G' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'G')) {
        Out += (SR[n] - SR[j]);
        if (s[j + Dis] == 'R')
          Out--;
      }
    }
  }
  printf("%lld\n", Out);
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  char c;
  vector<int> R, G, B;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> c;
    switch (c) {
    case 'R':
      R.push_back(i);
      break;
    case 'G':
      G.push_back(i);
      break;
    case 'B':
      B.push_back(i);
      break;
    }
  }
  long long ans = 0;
  int m, r, g, b, big, small;
  int target[3];
  int from, to;
  int flag = 0;
  for (int i = 0; i < R.size(); i++) {
    r = R.at(i);
    for (int j = 0; j < G.size(); j++) {
      g = G.at(j);
      if (r > g) {
        big = r;
        small = g;
      } else {
        big = g;
        small = r;
      }
      if ((r + g) % 2) {
        target[0] = -1;
      } else {
        target[0] = (r + g) / 2;
      }
      target[1] = 2 * big - small;
      target[2] = 2 * small - big;
      flag = 0;
      for (int k = 0; k < 3; k++) {
        // if(target[k]<0) continue;
        from = 0;
        to = B.size() - 1;
        while (1) {
          if (B.at((to + from) / 2) == target[k]) {
            flag++;
            break;
          } else if (to - from < 1) {
            break;
          } else if (B.at((to + from) / 2) > target[k]) {
            to = (to + from) / 2;
          } else {
            from = (to + from + 1) / 2;
          }
        }
      }
      ans += B.size() - flag;
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  char c;
  vector<int> R, G, B;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> c;
    switch (c) {
    case 'R':
      R.push_back(i);
      break;
    case 'G':
      G.push_back(i);
      break;
    case 'B':
      B.push_back(i);
      break;
    }
  }
  long long ans = 0;
  int m, r, g, b, big, small;
  int target[3];
  int from, to;
  int flag = 0;
  for (int i = 0; i < R.size(); i++) {
    r = R.at(i);
    for (int j = 0; j < G.size(); j++) {
      g = G.at(j);
      if (r > g) {
        big = r;
        small = g;
      } else {
        big = g;
        small = r;
      }
      if ((r + g) % 2) {
        target[0] = -1;
      } else {
        target[0] = (r + g) / 2;
      }
      target[1] = 2 * big - small;
      target[2] = 2 * small - big;
      flag = 0;
      for (int k = 0; k < 3; k++) {
        // if(target[k]<0) continue;
        from = 0;
        to = B.size() - 1;
        if (to < 0)
          break;
        while (1) {
          if (B.at((to + from) / 2) == target[k]) {
            flag++;
            break;
          } else if (to - from < 1) {
            break;
          } else if (B.at((to + from) / 2) > target[k]) {
            to = (to + from) / 2;
          } else {
            from = (to + from + 1) / 2;
          }
        }
      }
      ans += B.size() - flag;
    }
  }
  cout << ans;
  return 0;
}