Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG // TLEの原因になるので注意！！！！！！！！！！！
#include <bits/stdc++.h>
#include <cmath>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.xise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long

// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

bool chmn(int &a, int b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n;
  cin >> n;
  multiset<ll> S;
  ll b[n];
  rep(i, n) {
    ll a;
    cin >> a;
    b[i] = a;
    S.insert(a);
  }
  ll tot = 0;
  ll d[20005];
  rep(i, 20001) {
    ll m = S.count(i);
    d[i] = m;
    tot += m * (m - 1) / 2;
  }
  rep(i, n) {
    ll ans = tot;
    ll e = d[b[i]];
    ans -= (e - 1) * e / 2;
    ans += (e - 1) * (e - 2) / 2;
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#define _GLIBCXX_DEBUG // TLEの原因になるので注意！！！！！！！！！！！
#include <bits/stdc++.h>
#include <cmath>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.xise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long

// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

bool chmn(int &a, int b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll n;
  cin >> n;
  multiset<ll> S;
  ll b[n];
  rep(i, n) {
    ll a;
    cin >> a;
    b[i] = a;
    S.insert(a);
  }
  ll tot = 0;
  ll d[200005];
  rep(i, 200001) {
    ll m = S.count(i);
    d[i] = m;
    tot += m * (m - 1) / 2;
  }
  rep(i, n) {
    ll ans = tot;
    ll e = d[b[i]];
    ans -= (e - 1) * e / 2;
    ans += (e - 1) * (e - 2) / 2;
    cout << ans << endl;
  }
  return 0;
}