Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOCAL 1
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define fore(i, a, b) for (int i = (a), _b = (b); i < (_b); ++i)
#define fort(i, a, b) for (int i = (a), _b = (b); i <= (_b); ++i)
#define ford(i, a, b) for (int i = (a), _b = (b); i >= (_b); --i)
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &arg1) {
  cerr << name << ": " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &arg1, Args &...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " /";
  __f(comma + 1, args...);
}

template <class T> bool mini(T &a, T b) { return a > b ? (a = b, 1) : 0; }
template <class T> bool maxi(T &a, T b) { return a < b ? (a = b, 1) : 0; }

typedef long long LL;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

const int INF = 1e9 + 7;

int main() {
#if LOCAL
  freopen("CP.inp", "r", stdin);
//    freopen("CP.out", "w", stdout);
#endif // LOCAL
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vi a(n);
  fore(i, 0, n) cin >> a[i];
  sort(a.begin(), a.end());
  LL ans = 0;
  fore(i, 0, n) fore(j, i + 1, n) {
    ans += upper_bound(a.begin() + j + 1, a.end(), a[i] + a[j] - 1) -
           lower_bound(a.begin() + j + 1, a.end(), a[j] - a[i] + 1);
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOCAL 0
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define fore(i, a, b) for (int i = (a), _b = (b); i < (_b); ++i)
#define fort(i, a, b) for (int i = (a), _b = (b); i <= (_b); ++i)
#define ford(i, a, b) for (int i = (a), _b = (b); i >= (_b); --i)
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &arg1) {
  cerr << name << ": " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &arg1, Args &...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " /";
  __f(comma + 1, args...);
}

template <class T> bool mini(T &a, T b) { return a > b ? (a = b, 1) : 0; }
template <class T> bool maxi(T &a, T b) { return a < b ? (a = b, 1) : 0; }

typedef long long LL;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

const int INF = 1e9 + 7;

int main() {
#if LOCAL
  freopen("CP.inp", "r", stdin);
//    freopen("CP.out", "w", stdout);
#endif // LOCAL
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vi a(n);
  fore(i, 0, n) cin >> a[i];
  sort(a.begin(), a.end());
  LL ans = 0;
  fore(i, 0, n) fore(j, i + 1, n) {
    ans += upper_bound(a.begin() + j + 1, a.end(), a[i] + a[j] - 1) -
           lower_bound(a.begin() + j + 1, a.end(), a[j] - a[i] + 1);
  }
  cout << ans << '\n';
  return 0;
}