Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:16777216")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
// #include"functions.h"
// #include"highprecision.h"
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define np next_permutation
#define fi first
#define sc second
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pff pair<float, float>
#define pdd pair<double, double>
#define psi pair<string, int>
#define pis pair<int, string>
#define ppi pair<pii, int>
#define pdi pair<pii, pii>
#define LN 84757
#define INF 1e9
#define LLINF 1e18
#define MAXINT 0x7fffffff
#define MAXLL 0x7fffffffffffffff
#define EPS 1e-5
#define DEPS 1e-9
#define LDEPS 1e-13
#define MOD 1000000007
#define E 2.7182818284
#define PI 3.1415926535
#define rep(a, b) for (int a = 0; a < b; a++)
#define rep(a, b) for (int a = 0; a < b; a++)
#define forn(a, n) for (int a = 1; a <= n; a++)
#define forr(a, n) for (int a = n; a >= 1; a--)
#define repn(a, n, p) for (int a = 0; a < n; a += p)
#define repr(a, n) for (int a = n - 1; a >= 0; a--)
#define foreach(a, b) for (__typeof(b.begin()) a = b.begin(); a != b.end(); a++)
#define fillemp(a) memset(a, 0, sizeof(a))
#define fillinf(a) memset(a, 0x3f, sizeof(a))
#define frestream                                                              \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define stdf(a, b)                                                             \
  freopen(a, "r", stdin);                                                      \
  freopen(b, "w", stdout);
#define inputf(a) freopen(a, "r", stdin);
#define outputf(a) freopen(a, "w", stdout);
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

int n;
int a[100010];
map<int, ll> f, m;
int main(int argc, char **argv) {
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    f[a[i]]++;
  }
  ll sum = 0ll;
  rep(j, n) {
    int i = a[j];
    if (m[i])
      continue;
    m[i] = (f[i] * (f[i] - 1ll)) / 2ll;
    // cout<<m[i]<<" "<<i<<endl;
    sum += m[i];
  }
  rep(i, n) {
    ll ans = sum - (f[a[i]] * (f[a[i]] - 1ll)) / 2ll;
    ans += ((f[a[i]] - 1ll) * (f[a[i]] - 2ll)) / 2ll;
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#pragma comment(linker, "/stack:16777216")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
// #include"functions.h"
// #include"highprecision.h"
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define np next_permutation
#define fi first
#define sc second
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pff pair<float, float>
#define pdd pair<double, double>
#define psi pair<string, int>
#define pis pair<int, string>
#define ppi pair<pii, int>
#define pdi pair<pii, pii>
#define LN 84757
#define INF 1e9
#define LLINF 1e18
#define MAXINT 0x7fffffff
#define MAXLL 0x7fffffffffffffff
#define EPS 1e-5
#define DEPS 1e-9
#define LDEPS 1e-13
#define MOD 1000000007
#define E 2.7182818284
#define PI 3.1415926535
#define rep(a, b) for (int a = 0; a < b; a++)
#define rep(a, b) for (int a = 0; a < b; a++)
#define forn(a, n) for (int a = 1; a <= n; a++)
#define forr(a, n) for (int a = n; a >= 1; a--)
#define repn(a, n, p) for (int a = 0; a < n; a += p)
#define repr(a, n) for (int a = n - 1; a >= 0; a--)
#define foreach(a, b) for (__typeof(b.begin()) a = b.begin(); a != b.end(); a++)
#define fillemp(a) memset(a, 0, sizeof(a))
#define fillinf(a) memset(a, 0x3f, sizeof(a))
#define frestream                                                              \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define stdf(a, b)                                                             \
  freopen(a, "r", stdin);                                                      \
  freopen(b, "w", stdout);
#define inputf(a) freopen(a, "r", stdin);
#define outputf(a) freopen(a, "w", stdout);
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

int n;
ll a[200010];
ll f[200010], m[200010];
int main(int argc, char **argv) {
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    f[a[i]]++;
  }
  ll sum = 0ll;
  rep(j, n) {
    int i = a[j];
    if (m[i])
      continue;
    m[i] = (f[i] * (f[i] - 1ll)) / 2ll;
    // cout<<m[i]<<" "<<i<<endl;
    sum += m[i];
  }
  rep(i, n) {
    ll ans = sum - (f[a[i]] * (f[a[i]] - 1ll)) / 2ll;
    ans += ((f[a[i]] - 1ll) * (f[a[i]] - 2ll)) / 2ll;
    cout << ans << endl;
  }
  return 0;
}