Language: C++

#include <iostream>
#include <map>
#include <set>

int countAligned(const std::map<char, std::set<int>> &char2pos,
                 const char a, const char b, const char c) {
  int ans = 0;
  for (int i : char2pos[a]) {
    for (int j : char2pos[b]) {
      if ((i + j) & 1 != 0) {
        continue;
      }
      int k = (i + j) >> 1;
      if (char2pos[c].find(k) != char2pos[c].end()) {
        ans += 1;
      }
    }
  }
  return ans;
}

int main() {
  int len, std::string s;
  std::cin >> len;
  std::cin >> s;
  std::map<char, std::set<int>> char2pos;
  for (int pos = 0; pos < s.size(); pos++) {
    char2pos[s[pos]].push_back(pos);
  }
  int ans = char2pos['R'].size() * char2pos['G'].size() * char2pos['B'].size();
  ans -= countAligned(char2pos, 'R', 'G', 'B');
  ans -= countAligned(char2pos, 'R', 'B', 'G');
  ans -= countAligned(char2pos, 'G', 'B', 'R');
  return ans;
}


Compiler output:
a.cc: In function 'int countAligned(const std::map<char, std::set<int> >&, char, char, char)':
a.cc:8:26: error: passing 'const std::map<char, std::set<int> >' as 'this' argument discards qualifiers [-fpermissive]
    8 |   for (int i : char2pos[a]) {
      |                          ^
In file included from /usr/include/c++/14/map:63,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_map.h:504:7: note:   in call to 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = char; _Tp = std::set<int>; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, std::set<int> > >; mapped_type = std::set<int>; key_type = char]'
  504 |       operator[](const key_type& __k)
      |       ^~~~~~~~
a.cc:9:28: error: passing 'const std::map<char, std::set<int> >' as 'this' argument discards qualifiers [-fpermissive]
    9 |     for (int j : char2pos[b]) {
      |                            ^
/usr/include/c++/14/bits/stl_map.h:504:7: note:   in call to 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = char; _Tp = std::set<int>; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, std::set<int> > >; mapped_type = std::set<int>; key_type = char]'
  504 |       operator[](const key_type& __k)
      |       ^~~~~~~~
a.cc:14:21: error: passing 'const std::map<char, std::set<int> >' as 'this' argument discards qualifiers [-fpermissive]
   14 |       if (char2pos[c].find(k) != char2pos[c].end()) {
      |                     ^
/usr/include/c++/14/bits/stl_map.h:504:7: note:   in call to 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = char; _Tp = std::set<int>; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, std::set<int> > >; mapped_type = std::set<int>; key_type = char]'
  504 |       operator[](const key_type& __k)
      |       ^~~~~~~~
a.cc:14:44: error: passing 'const std::map<char, std::set<int> >' as 'this' argument discards qualifiers [-fpermissive]
   14 |       if (char2pos[c].find(k) != char2pos[c].end()) {
      |                                            ^
/usr/include/c++/14/bits/stl_map.h:504:7: note:   in call to 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = char; _Tp = std::set<int>; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, std::set<int> > >; mapped_type = std::set<int>; key_type = char]'
  504 |       operator[](const key_type& __k)
      |       ^~~~~~~~
a.cc: In function 'int main()':
a.cc:23:24: error: qualified-id in declaration before 's'
   23 |   int len, std::string s;
      |                        ^
a.cc:25:15: error: 's' was not declared in this scope
   25 |   std::cin >> s;
      |               ^