Language: C++

#include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

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

	int N=in();
	string S=in<string>();
	vector<vector<int>> C(3);
	REP(i,N){
		if(S[i]=='R') C[0].push_back(i);
		if(S[i]=='G') C[1].push_back(i);
		if(S[i]=='B') C[2].push_back(i);
	}
	int NR=C[0].size(),NG=C[1].size(),NB=C[2].size();
	ll A=0;
	REP(i,NR){
		REP(j,NG){
			int R=C[0][i],G=C[1][j];
			int DA = G-R;
			if(DA<=0) continue;

			auto pos = lower_bound(ALL(C[2]),G);
			auto dist = distance(C[2].begin(),pos);
			int Q = G+DA;
			auto res = binary_search(ALL(C[2]),Q);
			if(res) A += max(C[2].size()-1ULL-dist,0ULL);
			else A += max(C[2].size()-dist,0ULL);
		}
		REP(j,NB){
			int R=C[0][i],B=C[2][j];
			int DA = B-R;
			if(DA<=0) continue;

			auto pos = lower_bound(ALL(C[1]),B);
			auto dist = distance(C[1].begin(),pos);
			int Q = B+DA;
			auto res = binary_search(ALL(C[1]),Q);
			if(res) A += max(C[1].size()-1ULL-dist,0ULL);
			else A += max(C[1].size()-dist,0ULL);
		}
	}
	REP(i,NG){
		REP(j,NR){
			int G=C[1][i],R=C[0][j];
			int DA = R-G;
			if(DA<=0) continue;

			auto pos = lower_bound(ALL(C[2]),R);
			auto dist = distance(C[2].begin(),pos);
			int Q = R+DA;
			auto res = binary_search(ALL(C[2]),Q);
			if(res) A += max(C[2].size()-1ULL-dist,0ULL);
			else A += max(C[2].size()-dist,0ULL);
		}
		REP(j,NB){
			int G=C[1][i],B=C[2][j];
			int DA = B-G;
			if(DA<=0) continue;

			auto pos = lower_bound(ALL(C[0]),B);
			auto dist = distance(C[0].begin(),pos);
			int Q = B+DA;
			auto res = binary_search(ALL(C[0]),Q);
			if(res) A += max(C[0].size()-1ULL-dist,0ULL);
			else A += max(C[0].size()-dist,0ULL);
		}
	}
	REP(i,NB){
		REP(j,NR){
			int B=C[2][i],R=C[0][j];
			int DA = R-B;
			if(DA<=0) continue;

			auto pos = lower_bound(ALL(C[1]),R);
			auto dist = distance(C[1].begin(),pos);
			int Q = R+DA;
			auto res = binary_search(ALL(C[1]),Q);
			if(res) A += max(C[1].size()-1ULL-dist,0ULL);
			else A += max(C[1].size()-dist,0ULL);
		}
		REP(j,NG){
			int B=C[2][i],G=C[1][j];
			int DA = G-B;
			if(DA<=0) continue;

			auto pos = lower_bound(ALL(C[0]),G);
			auto dist = distance(C[0].begin(),pos);
			int Q = G+DA;
			auto res = binary_search(ALL(C[0]),Q);
			if(res) A += max(C[0].size()-1ULL-dist,0ULL);
			else A += max(C[0].size()-dist,0ULL);
		}
	}
	out(A);
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:52:38: error: no matching function for call to 'max(long unsigned int, long long unsigned int)'
   52 |                         else A += max(C[2].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:52:38: note:   deduced conflicting types for parameter 'const _Tp' ('long unsigned int' and 'long long unsigned int')
   52 |                         else A += max(C[2].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:52:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'long unsigned int'
   52 |                         else A += max(C[2].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
a.cc:64:38: error: no matching function for call to 'max(long unsigned int, long long unsigned int)'
   64 |                         else A += max(C[1].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:64:38: note:   deduced conflicting types for parameter 'const _Tp' ('long unsigned int' and 'long long unsigned int')
   64 |                         else A += max(C[1].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:64:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'long unsigned int'
   64 |                         else A += max(C[1].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
a.cc:78:38: error: no matching function for call to 'max(long unsigned int, long long unsigned int)'
   78 |                         else A += max(C[2].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:78:38: note:   deduced conflicting types for parameter 'const _Tp' ('long unsigned int' and 'long long unsigned int')
   78 |                         else A += max(C[2].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:78:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'long unsigned int'
   78 |                         else A += max(C[2].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
a.cc:90:38: error: no matching function for call to 'max(long unsigned int, long long unsigned int)'
   90 |                         else A += max(C[0].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:90:38: note:   deduced conflicting types for parameter 'const _Tp' ('long unsigned int' and 'long long unsigned int')
   90 |                         else A += max(C[0].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:90:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'long unsigned int'
   90 |                         else A += max(C[0].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
a.cc:104:38: error: no matching function for call to 'max(long unsigned int, long long unsigned int)'
  104 |                         else A += max(C[1].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:104:38: note:   deduced conflicting types for parameter 'const _Tp' ('long unsigned int' and 'long long unsigned int')
  104 |                         else A += max(C[1].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:104:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'long unsigned int'
  104 |                         else A += max(C[1].size()-dist,0ULL);
      |                                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
a.cc:11