Language: C

#include<algorithm>
#include<bitset>
#include<cassert>
#include<cfloat>
#include<climits>
#include<cmath>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<array>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

#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 FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T& t, const V& v) { t = v; }
template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T& t, const V& v) {
	for (auto& e : t) fill_v(e, v);
}


//ユークリッド互除法
template<typename T>
T gcd(T x, T y) {

	if (y == 0) {
		return x;
	}
	else {
		return gcd(y, x % y);
	}
}

template <typename T>
bool chmax(T & a, const T & b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
template <typename T>
bool chmin(T & a, const T & b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}


#define ARRAY_MAX 100005
const int INF = 1e9 + 7;
constexpr ll MOD = 1e9 + 7;

int dx4[4] = { 1,0,0,-1 };
int dy4[4] = { 0,1,-1,0 };
int dx2[2] = { 1,0 };
int dy2[2] = { 0,1 };

/******************************************************************************************/
typedef tuple<int,int,int> tpl;

template <typename T> bool next_combination(const T first, const T last, int k) {
    const T subset = first + k;
    // empty container | k = 0 | k == n 
    if (first == last || first == subset || last == subset) {
        return false;
    }
    T src = subset;
    while (first != src) {
        src--;
        if (*src < *(last - 1)) {
            T dest = subset;
            while (*src >= *dest) {
                dest++;
            }
            iter_swap(src, dest);
            rotate(src + 1, dest + 1, last);
            rotate(subset, subset + (last - dest) - 1, last);
            return true;
        }
    }
    // restore
    rotate(first, subset, last);
    return false;
}

int main() {

	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(10);

	int N;
	cin >> N;
	string S;
	cin >> S;
	S = ' '+S;

	vector<int> R(N+10,0),G(N+10,0),B(N+10,0);

	for(int i = 1;i < S.size();i++){
		if(S[i] == 'R'){
			R[i] = 1;
		}

		if(S[i] == 'G'){
			G[i] = 1;
		}

		if(S[i] == 'B'){
			B[i] = 1;
		}	
	}

	for (int i = 0; i < R.size() - 1; i++)
	{
		R[i+1]+=R[i];
		G[i+1]+=G[i];
		B[i+1]+=B[i];
	}

	

	ll ans = 0;
	for(int i = 1;i < S.size();i++){

		for(int j = i+1;j < S.size();j++){
			//cout << i << " " << j << " ans = " << ans << endl;
			if(S[i] == S[j])continue;

			int diff = j-i;
			if(S[i] == 'R'){

				if(S[j] == 'G'){

					//RG

					//Bを探す
					//j+1～NまでのB
					ll cntb = B[N]-B[j];
					//if(j+diff > N)continue;
					if(j+diff <= N && S[j+diff] == 'B'){
						//これは選べない
						//cout << i-diff << " " << i << " " << j << endl;
						//cout << S[i] << " " << S[j] << " " << S[i-diff] << endl;
						cntb--;
					}
					ans += cntb;
				}else{

					//RB

					//Gを探す
					ll cntg = G[N]-G[j];
					//if(j+diff > N)continue;
					if(j+diff <= N && S[j+diff] == 'G'){
						//これは選べない
						cntg--;
					}
					ans += cntg;
				}

			}
			/**********************************/
			if(S[i] == 'G'){

				if(S[j] == 'B'){
					//GB

					//Rを探す
					
					ll cntr = R[N]-R[j];
					//if(j+diff > N)continue;
					if(j+diff <= N && S[j+diff] == 'R'){
						//これは選べない
						cntr--;
					}
					ans += cntr;
				}else{
					//GR

					//Bを探す
					ll cntb = B[N]-B[j];
					//if(j+diff > N)continue;
					if(j+diff <= N && S[j+diff] == 'B'){
						//これは選べない
						cntb--;
					}
					ans += cntb;
				}

			}

			/*************************/
			if(S[i] == 'B'){

				if(S[j] == 'G'){
					//GB

					//Rを探す
					
					ll cntr = R[N]-R[j];
					//if(j+diff > N)continue;
					if(j+diff <= N && S[j+diff] == 'R'){
						//これは選べない
						cntr--;
					}
					ans += cntr;
				}else{
					//BR

					//Gを探す
					ll cntg = G[N]-G[j];
					//if(j+diff > N)continue;
					if(j+diff <= N && S[j+diff] == 'G'){
						//これは選べない
						cntg--;
					}
					ans += cntg;
				}

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






Compiler output:
main.c:1:9: fatal error: algorithm: No such file or directory
    1 | #include<algorithm>
      |         ^~~~~~~~~~~
compilation terminated.