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

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

const int MOD = 1000000007;
const int INF = 1000000000;

// long long
using ll = long long;

// 出力系
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

// begin() end()
#define all(x) (x).begin(), (x).end()

// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

// 最大公約数
unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

// 最小公倍数
unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; }

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = 0;
  REP(i, n) {
    ll cnt = 0;
    for (int j = i + 1; j < n; j++) {
      if (s[i] != s[j]) {
        int d = j - i;
        for (int k = j + 1; k < n; k++) {
          if (j + d < n)
            if (k == j + d)
              continue;
          if (s[i] != s[k] && s[j] != s[k])
            ans++;
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}


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

const int MOD = 1000000007;
const int INF = 1000000000;

// long long
using ll = long long;

// 出力系
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

// begin() end()
#define all(x) (x).begin(), (x).end()

// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

// 最大公約数
unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

// 最小公倍数
unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; }

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = 0;
  ll cnt1 = 0, cnt2 = 0, cnt3 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      cnt1++;
    if (s[i] == 'G')
      cnt2++;
    if (s[i] == 'B')
      cnt3++;
  }

  ans = cnt1 * cnt2 * cnt3;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      if (j - i < 0 || j + i >= n)
        continue;
      if (s[j] != s[j - i] && s[j] != s[j + i] && s[j + i] != s[j - i])
        ans--;
    }
  }
  cout << ans << "\n";
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repX(i, n, x) for (int i = x; i < (int)(n); i++)
#define repM(i, n) for (int i = n; i > 0; i--)

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<vector<int>> RGB(3);
  rep(i, N) {
    if (S[i] == 'R') {
      RGB[0].push_back(i);
    }
    if (S[i] == 'G') {
      RGB[1].push_back(i);
    }
    if (S[i] == 'B') {
      RGB[2].push_back(i);
    }
  }
  int ans = 0;
  rep(a, 3) {
    rep(i, RGB[a].size()) {
      rep(b, 3) {
        if (a == b)
          continue;
        rep(j, RGB[b].size()) {
          if (RGB[a][i] >= RGB[b][j])
            continue;
          rep(c, 3) {
            if (a == c || b == c)
              continue;
            if (RGB[b][i] <= RGB[c][RGB[c].size() - 1])
              break;
            rep(k, RGB[c].size()) {
              if (RGB[b][j] >= RGB[c][k])
                continue;
              if (RGB[b][j] - RGB[a][i] == RGB[c][k] - RGB[b][j])
                continue;
              ans++;
            }
          }
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repX(i, n, x) for (int i = x; i < (int)(n); i++)
#define repM(i, n) for (int i = n; i > 0; i--)

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<vector<int>> RGB(3);
  rep(i, N) {
    if (S[i] == 'R') {
      RGB[0].push_back(i);
    }
    if (S[i] == 'G') {
      RGB[1].push_back(i);
    }
    if (S[i] == 'B') {
      RGB[2].push_back(i);
    }
  }
  ll ans = 0;
  ans = RGB[0].size() * RGB[1].size() * RGB[2].size();
  rep(i, N) {
    repX(j, N, i + 1) {
      if (S[i] == S[j])
        continue;
      int k = j * 2 - i;
      if (k >= N || S[k] == S[i] || S[k] == S[j])
        continue;
      ans--;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define endl '\n'
#define lli long long int
#define rep(i, s, e) for (int i = s; i < e; i++)

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<int> R, G, B;
  rep(i, 0, N) {
    switch (S[i]) {
    case 'R':
      R.push_back(i);
      break;
    case 'G':
      G.push_back(i);
      break;
    case 'B':
      B.push_back(i);
      break;
    default:
      break;
    }
  }
  const int res = R.size() * G.size() * B.size();
  unsigned int even = 0;
  for (int r : R) {
    const int rr = 2 * r;
    for (int g : G) {
      const int gg = 2 * g;
      int rg = r + g;
      for (int b : B) {
        const int bb = 2 * b;
        const int br = b + r;
        const int bg = b + g;
        // r<g<b
        if (gg == br)
          even++;
        // r<b<g
        else if (bb == rg)
          even++;
        // b<r<g
        else if (rr == bg)
          even++;
      }
    }
  }
  cout << res - even << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define endl '\n'
#define lli long long int
#define rep(i, s, e) for (int i = s; i < e; i++)

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<int> R, G, B;
  rep(i, 0, N) {
    switch (S[i]) {
    case 'R':
      R.push_back(i);
      break;
    case 'G':
      G.push_back(i);
      break;
    case 'B':
      B.push_back(i);
      break;
    default:
      break;
    }
  }
  const int res = R.size() * G.size() * B.size();
  unsigned int even = 0;
  for (int r : R) {
    const int rr = 2 * r;
    for (int g : G) {
      const int gg = 2 * g;
      int rg = r + g;
      for (int b : B) {
        const int bb = 2 * b;
        const int br = b + r;
        const int bg = b + g;
        // r<g<b
        if (gg == br || bb == rg || rr == bg)
          even++;
      }
    }
  }
  cout << res - even << endl;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pq priority_queue
using namespace std;
const ll INF = (ll)1e9;
const ll MOD = (ll)1e9 + 7;
const ll MAX = 510000;
vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll N, ans = 0;
  string S;
  cin >> N >> S;
  vector<ll> R, G, B;
  set<ll> r, g, b;
  for (ll i = 0; i < N; i++) {
    if (S[i] == 'R') {
      R.push_back(i);
      r.insert(i);
    } else if (S[i] == 'B') {
      B.push_back(i);
      b.insert(i);
    } else if (S[i] == 'G') {
      G.push_back(i);
      g.insert(i);
    }
  }
  ans = R.size() * B.size() * G.size();
  for (ll i = 0; i < R.size(); i++) {
    for (ll j = 0; j < B.size(); j++) {
      for (ll k = 0; k < G.size(); k++) {
        ll d1, d2, d3;
        d1 = abs(R[i] - B[j]);
        d2 = abs(R[i] - G[k]);
        d3 = abs(B[j] - G[k]);
        if ((d1 == d2 || d1 == d3) || d2 == d3)
          ans--;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pq priority_queue
using namespace std;
const ll INF = (ll)1e9;
const ll MOD = (ll)1e9 + 7;
const ll MAX = 510000;
vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll N, ans = 0;
  string S;
  cin >> N >> S;
  vector<ll> R, G, B;
  set<ll> r, g, b;
  for (ll i = 0; i < N; i++) {
    if (S[i] == 'R') {
      R.push_back(i);
      r.insert(i);
    } else if (S[i] == 'B') {
      B.push_back(i);
      b.insert(i);
    } else if (S[i] == 'G') {
      G.push_back(i);
      g.insert(i);
    }
  }
  ans = R.size() * B.size() * G.size();
  for (ll i = 0; i < R.size(); i++) {
    for (ll j = 0; j < B.size(); j++) {
      ll d, m;
      d = abs(R[i] - B[j]);
      if (R[i] > B[j])
        m = R[i] - d / 2;
      else
        m = R[i] + d / 2;
      if (g.count(R[i] - d))
        ans--;
      if (g.count(R[i] + d))
        ans--;
      if (g.count(B[j] + d))
        ans--;
      if (g.count(B[j] - d))
        ans--;
      if ((R[i] + B[j]) % 2 == 0 && g.count(m))
        ans--;
    }
  }
  cout << ans << endl;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define mm(a, x) memset(a, x, sizeof(a))
#define sync                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0)
#define ll long long
using namespace std;
const int MAXN = 4010;
vector<int> rp, gp, bp;
int vis1[MAXN], vis2[MAXN], vis3[MAXN];
int main(void) {
  sync;
  int n;
  string s;
  cin >> n >> s;
  for (int i = 0; i < n; ++i)
    if (s[i] == 'R') {
      rp.push_back(i);
      vis1[i] = 1;
    } else if (s[i] == 'G') {
      gp.push_back(i);
      vis2[i] = 1;
    } else {
      bp.push_back(i);
      vis3[i] = 1;
    }
  ll res = rp.size() * gp.size() * bp.size();
  for (auto i : rp)
    for (auto j : gp)
      if (2 * j - i >= 0 && vis3[2 * j - i])
        --res;
  for (auto i : gp)
    for (auto j : rp)
      if (2 * j - i >= 0 && vis3[2 * j - i])
        --res;
  for (auto i : rp)
    for (auto j : bp)
      if (2 * j - i >= 0 && vis2[2 * j - i])
        --res;
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define mm(a, x) memset(a, x, sizeof(a))
#define sync                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0)
#define ll long long
using namespace std;
const int MAXN = 40100;
vector<int> rp, gp, bp;
int vis1[MAXN], vis2[MAXN], vis3[MAXN];
int main(void) {
  sync;
  int n;
  string s;
  cin >> n >> s;
  for (int i = 0; i < n; ++i)
    if (s[i] == 'R') {
      rp.push_back(i);
      vis1[i] = 1;
    } else if (s[i] == 'G') {
      gp.push_back(i);
      vis2[i] = 1;
    } else {
      bp.push_back(i);
      vis3[i] = 1;
    }
  ll res = rp.size() * gp.size() * bp.size();
  for (auto i : rp)
    for (auto j : gp)
      if (2 * j - i >= 0 && vis3[2 * j - i])
        --res;
  for (auto i : gp)
    for (auto j : rp)
      if (2 * j - i >= 0 && vis3[2 * j - i])
        --res;
  for (auto i : rp)
    for (auto j : bp)
      if (2 * j - i >= 0 && vis2[2 * j - i])
        --res;
  cout << res << endl;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M = 1e9 + 7;
const int iinf = 1 << 28;
const long long llinf = 1ll << 60;
const double PI = 3.14159265;

inline int getIdx(char c) {
  if (c == 'R')
    return 0;
  if (c == 'G')
    return 1;
  return 2;
}

struct S {
  vector<int> a;
  unordered_map<int, int> m;
};

void work() {
  int n;
  string s;
  cin >> n >> s;
  vector<vector<S>> v(n, vector<S>(3));
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (s[i] != s[j]) {
        int idx = getIdx(s[j]);
        v[i][idx].a.push_back(j);
        v[i][idx].m[j - i]++;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int k = 0; k < 3; ++k) {
      if (getIdx(s[i]) == k)
        continue;
      for (int j : v[i][k].a) {
        for (int l = 0; l < 3; ++l) {
          if (l == getIdx(s[i]) || l == k)
            continue;
          int d = j - i;
          ans += (int)(v[j][l].a.size()) - v[j][l].m[d];
        }
      }
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // int nt; cin>>nt; while (nt--) work();
  work();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M = 1e9 + 7;
const int iinf = 1 << 28;
const long long llinf = 1ll << 60;
const double PI = 3.14159265;

inline int getIdx(char c) {
  if (c == 'R')
    return 0;
  if (c == 'G')
    return 1;
  return 2;
}

struct S {
  vector<int> a;
  int m[4001];
};

void work() {
  int n;
  string s;
  cin >> n >> s;
  vector<vector<S>> v(n, vector<S>(3));
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (s[i] != s[j]) {
        int idx = getIdx(s[j]);
        v[i][idx].a.push_back(j);
        v[i][idx].m[j - i]++;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int k = 0; k < 3; ++k) {
      if (getIdx(s[i]) == k)
        continue;
      for (int j : v[i][k].a) {
        for (int l = 0; l < 3; ++l) {
          if (l == getIdx(s[i]) || l == k)
            continue;
          int d = j - i;
          ans += (int)(v[j][l].a.size()) - v[j][l].m[d];
        }
      }
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // int nt; cin>>nt; while (nt--) work();
  work();
  return 0;
}
Problem p02714 (C++)

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

using ll = long long;

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

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

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

  ll ans = 0;
  for (int i = 0; i < r.size(); i++) {
    for (int j = 0; j < g.size(); j++) {
      int left = r.at(i), right = g.at(j);
      if (left > right)
        swap(left, right);
      int keep = b.size();
      if ((2 * right - left < n) &&
          *lower_bound(b.begin(), b.end(), 2 * right - left) ==
              2 * right - left &&
          lower_bound(b.begin(), b.end(), 2 * right - left) != b.end()) {
        keep--;
      }
      if ((2 * left - right >= 0) &&
          *lower_bound(b.begin(), b.end(), 2 * left - right) ==
              2 * left - right &&
          lower_bound(b.begin(), b.end(), 2 * left - right) != b.end()) {
        keep--;
      }
      if (((left + right) % 2 == 0) &&
          *lower_bound(b.begin(), b.end(), (left + right) / 2) ==
              (left + right) / 2)
        keep--;
      ans += keep;
    }
  }

  cout << ans << '\n';
}

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

using ll = long long;

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

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

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

  if (r.size() == 0 || g.size() == 0 || b.size() == 0) {
    cout << "0\n";
    return 0;
  }

  ll ans = 0;
  for (int i = 0; i < r.size(); i++) {
    for (int j = 0; j < g.size(); j++) {
      int left = r.at(i), right = g.at(j);
      if (left > right)
        swap(left, right);
      int keep = b.size();
      if ((2 * right - left < n) &&
          *lower_bound(b.begin(), b.end(), 2 * right - left) ==
              2 * right - left &&
          lower_bound(b.begin(), b.end(), 2 * right - left) != b.end()) {
        keep--;
      }
      if ((2 * left - right >= 0) &&
          *lower_bound(b.begin(), b.end(), 2 * left - right) ==
              2 * left - right &&
          lower_bound(b.begin(), b.end(), 2 * left - right) != b.end()) {
        keep--;
      }
      if (((left + right) % 2 == 0) &&
          *lower_bound(b.begin(), b.end(), (left + right) / 2) ==
              (left + right) / 2)
        keep--;
      ans += keep;
    }
  }

  cout << ans << '\n';
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
int n;
char s[2005];
int num[2005];
ll a, b, c, ans;
int main() {
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 1; i <= n; i++) {
    if (s[i - 1] == 'R') {
      num[i] = 0;
      a++;
    } else if (s[i - 1] == 'G') {
      num[i] = 1;
      b++;
    } else if (s[i - 1] == 'B') {
      num[i] = 2;
      c++;
    }
  }
  int l, r;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i - j < 1 || i + j > n)
        break;
      if (num[i] != num[i - j] && num[i] != num[i + j] &&
          num[i - j] != num[i + j])
        ans++;
    }
  }
  printf("%lld", a * b * c - ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
int n;
char s[4005];
int num[4005];
ll a, b, c, ans;
int main() {
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 1; i <= n; i++) {
    if (s[i - 1] == 'R') {
      num[i] = 0;
      a++;
    } else if (s[i - 1] == 'G') {
      num[i] = 1;
      b++;
    } else if (s[i - 1] == 'B') {
      num[i] = 2;
      c++;
    }
  }
  int l, r;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i - j < 1 || i + j > n)
        break;
      if (num[i] != num[i - j] && num[i] != num[i + j] &&
          num[i - j] != num[i + j])
        ans++;
    }
  }
  printf("%lld", a * b * c - ans);

  return 0;
}
Problem p02714 (C++)

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

int main() {
  int N;
  string S;
  cin >> N >> S;
  ll ans = 0;
  FOR(i, 0, N - 2) FOR(j, i + 1, N - 1) FOR(k, j + 1, N) {
    if (j - i != k - j && S[i] != S[j] && S[i] != S[k] && S[j] != S[k])
      ans += 1;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int N;
  string S;
  cin >> N >> S;
  vector<ll> cnt(3);
  rep(i, N) {
    if (S[i] == 'R')
      cnt[0] += 1;
    if (S[i] == 'G')
      cnt[1] += 1;
    if (S[i] == 'B')
      cnt[2] += 1;
  }
  ll ans = cnt[0] * cnt[1] * cnt[2];
  rep(j, N) rep(i, j) {
    int k = 2 * j - i;
    if (k < N) {
      if (S[i] != S[j] && S[j] != S[k] && S[k] != S[i])
        ans -= 1;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define INF (1 << 30) - 1
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define INF (1 << 30) - 1
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vi txt(n);
  rep(i, n) {
    if (s[i] == 'R')
      txt[i] = 0;
    if (s[i] == 'G')
      txt[i] = 1;
    if (s[i] == 'B')
      txt[i] = 2;
  }
  vi rgb(3);
  rep(i, n) rgb[txt[i]]++;
  ll ans = 1;
  rep(i, 3) ans *= rgb[i];
  rep(j, n) {
    rep(i, j) {
      if (2 * j - i < n && txt[i] != txt[j] && txt[j] != txt[2 * j - i] &&
          txt[2 * j - i] != txt[i])
        ans--;
    }
  }
  cout << ans;
  cout << "\n";
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repe(i, n) for (int i = 0; i <= (n); ++i)
#define repe1(i, n) for (int i = 0; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define eb(k, v) emplace_back(k, v)
#define ct(res) cout << res << "\n";
#define vi vector<int>
#define vl vector<ll>
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const ll MOD = 1000000007;
using Graph = vector<vector<int>>;
ll gcd(ll a, ll b) { return (a % b) ? gcd(b, a % b) : b; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      for (int k = j + 1; k < n; ++k) {
        if (s[i] != s[j] && s[k] != s[i] && s[k] != s[j] && j - i != k - j) {
          ans++;
        }
      }
    }
  }
  ct(ans);
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repe(i, n) for (int i = 0; i <= (n); ++i)
#define repe1(i, n) for (int i = 0; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define eb(k, v) emplace_back(k, v)
#define ct(res) cout << res << "\n";
#define vi vector<int>
#define vl vector<ll>
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const ll MOD = 1000000007;
using Graph = vector<vector<int>>;
ll gcd(ll a, ll b) { return (a % b) ? gcd(b, a % b) : b; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = 0;
  map<char, vector<ll>> mp;
  for (ll i = 0; i < n; ++i) {
    mp[s[i]].pb(i + 1);
  }
  ll r = mp['R'].size();
  ll g = mp['G'].size();
  ll b = mp['B'].size();
  ans = r * g * b;
  for (ll i = 0; i < n; ++i) {
    for (ll j = i + 1; j < n; ++j) {
      ll k = j + j - i;
      if (k < n && s[i] != s[j] && s[j] != s[k] && s[i] != s[k]) {
        ans--;
      }
    }
  }
  ct(ans);
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
// g++ .cpp && ./a.out
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

typedef pair<int, int> p;
typedef long long ll;

const int mod = 1000000007;
const int inf = 1000000007;

int main() {
  int n;
  string s;
  cin >> n >> s;
  int cnt = 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 (j - i != k - j && s[i] != s[j] && s[j] != s[k] && s[k] != s[i]) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
}


Fixed submission:
// g++ .cpp && ./a.out
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

typedef pair<int, int> p;
typedef long long ll;

const int mod = 1000000007;
const int inf = 1000000007;

int main() {
  int n;
  string s;
  cin >> n >> s;
  ll cnt = 0;
  vector<ll> cntR(n);
  vector<ll> cntB(n);
  vector<ll> cntG(n);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (s[j] == 'R')
        cntR[i]++;
      if (s[j] == 'B')
        cntB[i]++;
      if (s[j] == 'G')
        cntG[i]++;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      {
        if (s[i] == 'R' && s[j] == 'B') {
          cnt += cntG[j];
          if (s[2 * (j - i) + i] == 'G' && 2 * (j - i) + i < n)
            cnt--;
        }
        if (s[i] == 'R' && s[j] == 'G') {
          cnt += cntB[j];
          if (s[2 * (j - i) + i] == 'B' && 2 * (j - i) + i < n)
            cnt--;
        }
        if (s[i] == 'B' && s[j] == 'R') {
          cnt += cntG[j];
          if (s[2 * (j - i) + i] == 'G' && 2 * (j - i) + i < n)
            cnt--;
        }
        if (s[i] == 'B' && s[j] == 'G') {
          cnt += cntR[j];
          if (s[2 * (j - i) + i] == 'R' && 2 * (j - i) + i < n)
            cnt--;
        }
        if (s[i] == 'G' && s[j] == 'B') {
          cnt += cntR[j];
          if (s[2 * (j - i) + i] == 'R' && 2 * (j - i) + i < n)
            cnt--;
        }
        if (s[i] == 'G' && s[j] == 'R') {
          cnt += cntB[j];
          if (s[2 * (j - i) + i] == 'B' && 2 * (j - i) + i < n)
            cnt--;
        }
      }
    }
  }
  cout << cnt << endl;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define hhh cerr << "hhh" << endl
#define see(x) cerr << (#x) << '=' << (x) << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int maxn = 3e5 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

inline int id(char c) {
  if (c == 'R')
    return 0;
  if (c == 'G')
    return 1;
  if (c == 'B')
    return 2;
  return -1;
}

int cnt[3][maxn];
bool vis[maxn];
char s[maxn];

int main() {
  int n = read();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; ++i)
    cnt[id(s[i])][i] = 1;
  for (int c = 0; c < 3; ++c) {
    for (int i = n - 1; i; --i)
      cnt[c][i] += cnt[c][i + 1];
  }
  ll ans = 0;
  for (int i = 1; i <= n - 2; ++i) {
    for (int j = i + 1; j <= n - 1; ++j)
      if (s[i] != s[j]) {
        memset(vis, 0, sizeof(vis));
        vis[id(s[i])] = 1;
        vis[id(s[j])] = 1;
        int c;
        for (int i = 0; i < 3; ++i)
          if (!vis[i]) {
            c = i;
            break;
          }
        ans += cnt[c][j + 1];
        if (2 * j - i <= n && id(s[2 * j - i]) == c)
          ans--;
      }
  }
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
#define hhh cerr << "hhh" << endl
#define see(x) cerr << (#x) << '=' << (x) << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int maxn = 3e5 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

inline int id(char c) {
  if (c == 'R')
    return 0;
  if (c == 'G')
    return 1;
  if (c == 'B')
    return 2;
  return -1;
}

int cnt[3][maxn];
bool vis[3];
char s[maxn];

int main() {
  int n = read();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; ++i)
    cnt[id(s[i])][i] = 1;
  for (int c = 0; c < 3; ++c) {
    for (int i = n - 1; i; --i)
      cnt[c][i] += cnt[c][i + 1];
  }
  ll ans = 0;
  for (int i = 1; i <= n - 2; ++i) {
    for (int j = i + 1; j <= n - 1; ++j)
      if (s[i] != s[j]) {
        memset(vis, 0, sizeof(vis));
        vis[id(s[i])] = 1;
        vis[id(s[j])] = 1;
        int c;
        for (int i = 0; i < 3; ++i)
          if (!vis[i]) {
            c = i;
            break;
          }
        ans += cnt[c][j + 1];
        if (2 * j - i <= n && id(s[2 * j - i]) == c)
          ans--;
      }
  }
  cout << ans << endl;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define FORD(i, a, b) for (int i = a; i > (int)(b); i--)
#define PPC(x) __builtin_popcount(x)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i)&1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;

const int maxN = 1 << 13;

int pref[400][maxN];
char T[maxN];

void solve() {
  int n;
  scanf("%d %s", &n, T + 1);
  FOR(i, 1, n + 1) {
    for (char c : {'R', 'G', 'B'})
      pref[i][c] = pref[i - 1][c];
    pref[i][T[i]]++;
  }

  long long res = 0;
  FOR(i, 1, n + 1) FOR(j, i + 2, n + 1) if (T[i] != T[j]) {
    char x = T[i] ^ T[j] ^ 'R' ^ 'G' ^ 'B';
    int k = (i + j) / 2;
    res += pref[j][x] - pref[i][x];
    if ((i - j) % 2 == 0 and T[k] == x)
      res--;
  }

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

int main() {
  int t;
  t = 1; // scanf ("%d", &t);
  while (t--)
    solve();
  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define FORD(i, a, b) for (int i = a; i > (int)(b); i--)
#define PPC(x) __builtin_popcount(x)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i)&1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;

const int maxN = 1 << 13;

int pref[maxN][400];
char T[maxN];

void solve() {
  int n;
  scanf("%d %s", &n, T + 1);
  FOR(i, 1, n + 1) {
    for (char c : {'R', 'G', 'B'})
      pref[i][c] = pref[i - 1][c];
    pref[i][T[i]]++;
  }

  long long res = 0;
  FOR(i, 1, n + 1) FOR(j, i + 2, n + 1) if (T[i] != T[j]) {
    char x = T[i] ^ T[j] ^ 'R' ^ 'G' ^ 'B';
    int k = (i + j) / 2;
    res += pref[j][x] - pref[i][x];
    if ((i - j) % 2 == 0 and T[k] == x)
      res--;
  }

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

int main() {
  int t;
  t = 1; // scanf ("%d", &t);
  while (t--)
    solve();
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define X first
#define Y second
#define MP make_pair
#define PI acos(-1.0)
#define eps 1e-8
const int maxn = 5e4 + 10;
const int maxm = 1.5e7 + 10;
const int mod = 998244353;
using namespace std;

int n;
int a[maxn];
char s[maxn];
vector<int> st[3];

int main() {
  scanf("%d%s", &n, s);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      a[i] = 0;
    else if (s[i] == 'G')
      a[i] = 1;
    else
      a[i] = 2;
    st[a[i]].pb(i);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= 2; j++) {
      for (int k = 0; k <= 2; k++) {
        if (j != k && j != a[i] && k != a[i]) {
          vector<int>::iterator it = lower_bound(st[j].begin(), st[j].end(), i);
          for (; it != st[j].end(); it++) {
            int tmp = 2 * (*it) - i;
            ans += st[k].end() - lower_bound(st[k].begin(), st[k].end(), *it);
            it = lower_bound(st[k].begin(), st[k].end(), tmp);
            if (it != st[k].end() && *it == tmp)
              ans--;
          }
        }
      }
    }
  }
  printf("%lld", ans);
  // system("pause");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define X first
#define Y second
#define MP make_pair
#define PI acos(-1.0)
#define eps 1e-8
const int maxn = 5e4 + 10;
const int maxm = 1.5e7 + 10;
const int mod = 998244353;
using namespace std;

int n;
int a[maxn];
char s[maxn];
vector<int> st[3];

int main() {
  scanf("%d%s", &n, s);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      a[i] = 0;
    else if (s[i] == 'G')
      a[i] = 1;
    else
      a[i] = 2;
    st[a[i]].pb(i);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= 2; j++) {
      for (int k = 0; k <= 2; k++) {
        if (j != k && j != a[i] && k != a[i]) {
          vector<int>::iterator it = lower_bound(st[j].begin(), st[j].end(), i);
          for (; it != st[j].end(); it++) {
            int tmp = 2 * (*it) - i;
            ans += st[k].end() - lower_bound(st[k].begin(), st[k].end(), *it);
            vector<int>::iterator itt =
                lower_bound(st[k].begin(), st[k].end(), tmp);
            if (itt != st[k].end() && *itt == tmp)
              ans--;
          }
        }
      }
    }
  }
  printf("%lld", ans);
  // system("pause");
  return 0;
}
Problem p02714 (C++)

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

using namespace std;

int main(void) {
  // Your code here
  int N;
  string S;
  int i;
  long long r = 0, g = 0, b = 0;
  long long x = 0, y = 0, z = 0;

  cin >> N >> S;

  for (i = 0; i < N; i++) {
    if (S.at(i) == 'R')
      r++;
    if (S.at(i) == 'G')
      g++;
    if (S.at(i) == 'B')
      b++;
  }

  long long all = r * g * b;

  for (x = 0; x < N - 2; x++) {
    for (y = x + 1; y < N - 1; y++) {
      z = y * 2 - x;
      if (z > N - 1;
          S.at(x) == S.at(z) || S.at(y) == S.at(z) || S.at(x) == S.at(y))
        continue;
      all--;
    }
  }

  cout << all << endl;
}


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

using namespace std;

int main(void) {
  // Your code here
  int N;
  string S;
  int i;
  long long r = 0, g = 0, b = 0;
  long long x = 0, y = 0, z = 0;

  cin >> N >> S;

  for (i = 0; i < N; i++) {
    if (S.at(i) == 'R')
      r++;
    if (S.at(i) == 'G')
      g++;
    if (S.at(i) == 'B')
      b++;
  }

  long long all = r * g * b;

  for (x = 0; x < N - 2; x++) {
    for (y = x + 1; y < N - 1; y++) {
      z = y * 2 - x;
      if (z > N - 1 || S.at(x) == S.at(z) || S.at(y) == S.at(z) ||
          S.at(x) == S.at(y))
        continue;
      all--;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cfenv>
#include <cmath>
#include <deque>
#include <forward_list>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef long long ll;

using namespace std;

int main() {
  int N, sum = 0;
  char S[4002];
  cin >> N;
  cin >> S;
  ll R = 0;
  ll G = 0;
  ll B = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      R++;
    } else if (S[i] == 'G') {
      G++;
    } else if (S[i] == 'B') {
      B++;
    }
  }
  ll cnt = R * G * B;
  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      int k = (j - i) + j;
      if (S[i] != S[j] && S[j] != S[k] && S[k] != S[i] && k < N) {
        cnt--;
      }
    }
  }
  cout << cnt;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cfenv>
#include <cmath>
#include <deque>
#include <forward_list>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef long long ll;

using namespace std;

int main() {
  int N, sum = 0;
  char S[4002];
  cin >> N;
  cin >> S;
  ll R = 0;
  ll G = 0;
  ll B = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      R++;
    } else if (S[i] == 'G') {
      G++;
    } else if (S[i] == 'B') {
      B++;
    }
  }
  ll cnt = R * G * B;
  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      int k = (j - i) + j;
      if (k < N) {
        if (S[i] != S[j] && S[j] != S[k] && S[k] != S[i]) {
          cnt--;
        }
      }
    }
  }
  cout << cnt;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int pan(char a, char b, char c) {
  if (a != b && b != c && c != a)
    return 1;
  return 0;
}
int main() {
  int n;
  cin >> n;
  char a[n];
  cin >> a;
  long long cnt = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int j = 1; j + i < n - 1; j++) {
      for (int x = 1; x + j + i < n; x++)
        if (j != x && pan(a[i], a[i + j], a[i + j + x]))
          cnt++;
    }
  }
  cout << cnt;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int pan(char a, char b, char c) {
  if (a != b && b != c && c != a)
    return 1;
  return 0;
}
int main() {
  int n;
  cin >> n;
  char a[n];
  cin >> a;
  long long cnt = 0;
  int x = 0, y = 0, z = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 'R')
      x++;
    if (a[i] == 'G')
      y++;
    if (a[i] == 'B')
      z++;
  }
  cnt = (long long)x * y * z;
  for (int i = 0; i < n; i++) {
    for (int d = 1; i + d + d < n; d++)
      if (pan(a[i], a[i + d], a[i + d + d]))
        cnt--;
  }
  cout << cnt;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
  int n;
  string s;
  cin >> n >> 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;
  REP(i, r.size()) {
    REP(j, g.size()) {
      REP(k, b.size()) {
        vector<int> ind = {r[i], g[j], b[k]};
        sort(ALL(ind));
        if (ind[1] - ind[0] != ind[2] - ind[1])
          ans++;
      }
    }
  }

  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
  int n;
  string s;
  cin >> n >> 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 = r.size() * g.size() * b.size();
  REP(i, n) {
    for (int k = i + 2; k < n; k += 2) {
      int j = (i + k) / 2;
      if (s[i] != s[j] && s[j] != s[k] && s[k] != s[i])
        ans--;
    }
  }

  cout << ans << '\n';
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e3 + 5;
int main() {
  ll i, j, k, n, sum[maxn][3] = {0}, ans = 0;
  char s[maxn];
  scanf("%d%s", &n, s + 1);
  for (i = n; i >= 1; i--) {
    sum[i][0] = sum[i + 1][0];
    sum[i][1] = sum[i + 1][1];
    sum[i][2] = sum[i + 1][2];
    if (s[i] == 'R')
      sum[i][0]++;
    if (s[i] == 'G')
      sum[i][1]++;
    if (s[i] == 'B')
      sum[i][2]++;
  }
  for (i = 1; i <= n; i++) {
    for (j = i + 1; j <= n; j++) {
      if (s[i] == s[j])
        continue;
      if (s[i] != 'R' && s[j] != 'R') {
        ans = ans + sum[j + 1][0];
        if (s[j + (j - i)] == 'R')
          ans--;
      }
      if (s[i] != 'G' && s[j] != 'G') {
        ans = ans + sum[j + 1][1];
        if (s[j + (j - i)] == 'G')
          ans--;
      }
      if (s[i] != 'B' && s[j] != 'B') {
        ans = ans + sum[j + 1][2];
        if (s[j + (j - i)] == 'B')
          ans--;
      }
    }
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 9;
int main() {
  ll i, j, k, n, sum[maxn][3] = {0}, ans = 0;
  char s[maxn];
  scanf("%d%s", &n, s + 1);
  for (i = n; i >= 1; i--) {
    sum[i][0] = sum[i + 1][0];
    sum[i][1] = sum[i + 1][1];
    sum[i][2] = sum[i + 1][2];
    if (s[i] == 'R')
      sum[i][0]++;
    if (s[i] == 'G')
      sum[i][1]++;
    if (s[i] == 'B')
      sum[i][2]++;
  }
  for (i = 1; i <= n; i++) {
    for (j = i + 1; j <= n; j++) {
      if (s[i] == s[j])
        continue;
      if (s[i] != 'R' && s[j] != 'R') {
        ans = ans + sum[j + 1][0];
        if (s[j + (j - i)] == 'R')
          ans--;
      }
      if (s[i] != 'G' && s[j] != 'G') {
        ans = ans + sum[j + 1][1];
        if (s[j + (j - i)] == 'G')
          ans--;
      }
      if (s[i] != 'B' && s[j] != 'B') {
        ans = ans + sum[j + 1][2];
        if (s[j + (j - i)] == 'B')
          ans--;
      }
    }
  }
  printf("%lld\n", ans);
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  string x, y, z;

  long long ans = 0;
  for (int i = 0; i < n - 2; i++) {
    x = s[i];
    for (int j = i + 1; j < n - 1; j++) {
      y = s[j];
      if (y == x)
        continue;
      for (int k = j + 1; k < n; k++) {
        z = s[k];
        if (x == z || y == z)
          continue;
        if (j - i != k - j)
          ans++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  string x, y, z;

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

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

  cout << ans << endl;

  return 0;
}
Problem p02714 (C++)

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

int main() {
  int n;
  cin >> n;
  int R[1001] = {};
  int G[1001] = {};
  int B[1001] = {};
  int Rcnt = 0;
  int Gcnt = 0;
  int Bcnt = 0;
  string s;
  cin >> s;
  long long int ans = 0;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'R') {
      R[i] = 1;
      Rcnt++;
    } else if (s[i] == 'G') {
      G[i] = 1;
      Gcnt++;
    } else if (s[i] == 'B') {
      B[i] = 1;
      Bcnt++;
    }
  }
  for (int i = 0; i < s.size(); i++)
    for (int j = 0; j < s.size(); j++) {
      if (R[i] == 1 && G[j] == 1) {
        ans += Bcnt;
        int temp = abs(j - i);
        int temp_2 = i + j;
        if (temp + max(i, j) <= s.size()) {
          if (B[temp + max(i, j)] == 1)
            ans--;
        }
        if (min(i, j) - temp >= 0) {
          if (B[min(i, j) - temp] == 1)
            ans--;
        }
        if (temp_2 % 2 == 0) {
          if (B[temp_2 / 2] == 1)
            ans--;
        }
      }
    }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  int R[4001] = {};
  int G[4001] = {};
  int B[4001] = {};
  int Rcnt = 0;
  int Gcnt = 0;
  int Bcnt = 0;
  string s;
  cin >> s;
  long long int ans = 0;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'R') {
      R[i] = 1;
      Rcnt++;
    } else if (s[i] == 'G') {
      G[i] = 1;
      Gcnt++;
    } else if (s[i] == 'B') {
      B[i] = 1;
      Bcnt++;
    }
  }
  for (int i = 0; i < s.size(); i++)
    for (int j = 0; j < s.size(); j++) {
      if (R[i] == 1 && G[j] == 1) {
        ans += Bcnt;
        int temp = abs(j - i);
        int temp_2 = i + j;
        if (temp + max(i, j) <= s.size()) {
          if (B[temp + max(i, j)] == 1)
            ans--;
        }
        if (min(i, j) - temp >= 0) {
          if (B[min(i, j) - temp] == 1)
            ans--;
        }
        if (temp_2 % 2 == 0) {
          if (B[temp_2 / 2] == 1)
            ans--;
        }
      }
    }
  cout << ans << endl;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<ll>

int main() {

  int N;
  string S;
  cin >> N >> S;

  ll ans = 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[i] != S[j] && S[j] != S[k] && S[k] != S[i] && (j - i) != (k - j))
          ans++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<ll>

int main() {

  int N;
  string S;
  cin >> N >> S;

  ll ans = 0;
  ll r = 0;
  ll g = 0;
  ll b = 0;

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

  ans = r * g * b;

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

  cout << ans << endl;

  return 0;
}