Problem p02714 (C++)

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

int main() {
  int N;
  long long ans = 0;
  cin >> N;
  vector<int> r(0), g(0), b(0);
  vector<char> c(N);

  for (int i = 0; i < N; i++) {
    cin >> c.at(i);
    if (c.at(i) == 'R') {
      r.push_back(i + 1);
    } else if (c.at(i) == 'G') {
      g.push_back(i + 1);
    } else {
      b.push_back(i + 1);
    }
  }

  vector<int> a(2);
  sort(b.begin(), b.end());
  ans = r.size() * g.size() * b.size();
  int temp;

  for (int i = 0; i < r.size(); i++) {
    for (int j = 0; j < g.size(); j++) {
      a.at(0) = r.at(i);
      a.at(1) = g.at(j);
      sort(a.begin(), a.end());

      temp = 2 * a.at(1) - a.at(0);
      if (temp > b.at((b.size() - 1) / 2) && temp <= b.at(b.size() - 1)) {
        for (int k = (b.size() - 1) / 2; k < b.size(); k++) {
          if (b.at(k) == temp) {
            ans--;
            break;
          }
        }
      } else if (temp <= b.at((b.size() - 1) / 2) && temp >= b.at(0)) {
        for (int k = (b.size() - 1) / 2; k > -1; k--) {
          if (b.at(k) == temp) {
            ans--;
            break;
          }
        }
      }

      temp = a.at(1) + a.at(0);
      if (temp > 2 * b.at((b.size() - 1) / 2) &&
          temp <= 2 * b.at(b.size() - 1)) {
        for (int k = (b.size() - 1) / 2; k < b.size(); k++) {
          if (2 * b.at(k) == temp) {
            ans--;
            break;
          }
        }
      } else if (temp <= 2 * b.at((b.size() - 1) / 2) && temp >= 2 * b.at(0)) {
        for (int k = (b.size() - 1) / 2; k > -1; k--) {
          if (2 * b.at(k) == temp) {
            ans--;
            break;
          }
        }
      }

      temp = 2 * a.at(0) - a.at(1);
      if (temp > b.at((b.size() - 1) / 2) && temp <= b.at(b.size() - 1)) {
        for (int k = (b.size() - 1) / 2; k < b.size(); k++) {
          if (b.at(k) == temp) {
            ans--;
            break;
          }
        }
      } else if (temp <= b.at((b.size() - 1) / 2) && temp >= b.at(0)) {
        for (int k = (b.size() - 1) / 2; k > -1; k--) {
          if (b.at(k) == temp) {
            ans--;
            break;
          }
        }
      }
    }
  }
  cout << ans;
}

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

int main() {
  int N;
  long long ans = 0;
  cin >> N;
  vector<int> r(0), g(0), b(0);
  vector<char> c(N);

  for (int i = 0; i < N; i++) {
    cin >> c.at(i);
    if (c.at(i) == 'R') {
      r.push_back(i + 1);
    } else if (c.at(i) == 'G') {
      g.push_back(i + 1);
    } else {
      b.push_back(i + 1);
    }
  }

  vector<int> a(2);
  sort(b.begin(), b.end());
  ans = r.size() * g.size() * b.size();
  int temp;

  for (int i = 0; i < r.size(); i++) {
    if (b.size() == 0) {
      break;
    }
    for (int j = 0; j < g.size(); j++) {
      a.at(0) = r.at(i);
      a.at(1) = g.at(j);
      sort(a.begin(), a.end());

      temp = 2 * a.at(1) - a.at(0);
      if (temp > b.at((b.size() - 1) / 2) && temp <= b.at(b.size() - 1)) {
        for (int k = (b.size() - 1) / 2; k < b.size(); k++) {
          if (b.at(k) == temp) {
            ans--;
            break;
          }
        }
      } else if (temp <= b.at((b.size() - 1) / 2) && temp >= b.at(0)) {
        for (int k = (b.size() - 1) / 2; k > -1; k--) {
          if (b.at(k) == temp) {
            ans--;
            break;
          }
        }
      }

      temp = a.at(1) + a.at(0);
      if (temp > 2 * b.at((b.size() - 1) / 2) &&
          temp <= 2 * b.at(b.size() - 1)) {
        for (int k = (b.size() - 1) / 2; k < b.size(); k++) {
          if (2 * b.at(k) == temp) {
            ans--;
            break;
          }
        }
      } else if (temp <= 2 * b.at((b.size() - 1) / 2) && temp >= 2 * b.at(0)) {
        for (int k = (b.size() - 1) / 2; k > -1; k--) {
          if (2 * b.at(k) == temp) {
            ans--;
            break;
          }
        }
      }

      temp = 2 * a.at(0) - a.at(1);
      if (temp > b.at((b.size() - 1) / 2) && temp <= b.at(b.size() - 1)) {
        for (int k = (b.size() - 1) / 2; k < b.size(); k++) {
          if (b.at(k) == temp) {
            ans--;
            break;
          }
        }
      } else if (temp <= b.at((b.size() - 1) / 2) && temp >= b.at(0)) {
        for (int k = (b.size() - 1) / 2; k > -1; k--) {
          if (b.at(k) == temp) {
            ans--;
            break;
          }
        }
      }
    }
  }
  cout << ans;
}