Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#include <float.h>
#include <time.h>

#define loop(i, start, end) for (i = (start); i <= (end); i++)
#define loopIterators(it, vec) for (it = (vec).begin(); it != (vec).end(); it++)
#define loopIteratorsRev(it, vec)                                              \
  for (it = (vec).rbegin(); it != (vec).rend(); it++)
#define loopRev(i, start, end) for (i = (start); i >= (end); i--)
#define FLASH                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define testCases(t)                                                           \
  cin >> t;                                                                    \
  while (t--)
#define PN(n) cout << (n)
#define PN1(n) cout << (n) << " "
#define PN2(a, b) cout << (a) << " " << (b) << " "
#define PNN1(n) cout << (n) << endl
#define PNN2(a, b) cout << (a) << " " << (b) << endl
#define PNN3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define PrintArray(ar, n)                                                      \
  for (i = 0; i < n; i++)                                                      \
    cout << ar[i] << " ";                                                      \
  cout << endl
#define PrintObject(ar, it)                                                    \
  loopIterators(it, ar) cout << *it << " ";                                    \
  cout << endl
#define PrintMap(map, it) loopIterators(it, map) PNN2(it->first, it->second)
#define sz(a) sizeof(a)
#define IN1(n) cin >> n
#define IN2(a, b) cin >> a >> b
#define IN3(a, b, c) cin >> a >> b >> c
#define ALL(s) s.begin(), s.end()
#define SWAP(a, b, c)                                                          \
  c = a;                                                                       \
  a = b;                                                                       \
  b = c
#define STRSP(s) scanf("%[^\n]%*c", s)
#define ENTER PNN1("");
#define MOD7 1000000007
#define MOD9 10000009
#define MAX 1000000000
#define llPair std::pair<lli, lli>
#define iiPair std::pair<int, int>
// #define ONLINE_JUDGE

typedef long long int lli;
typedef long int li;
typedef unsigned long long int ulli;
typedef long double ld;

using namespace std;

int main(void) {
  FLASH

#ifndef ONLINE_JUDGE
  freopen("input-stream.txt", "r", stdin);
  freopen("output-stream.txt", "w", stdout);
#endif

  int n, i;
  IN1(n);
  int ar[n];
  std::map<int, int> d;

  loop(i, 0, n - 1) IN1(ar[i]);
  loop(i, 0, n - 1)++ d[ar[i]];

  lli total = 0LL;
  for (auto cur : d)
    total += 1LL * cur.second * (cur.second - 1) / 2;

  // PNN1(total);
  loop(i, 0, n - 1) {
    lli now = d[ar[i]];
    lli cur = total - 1LL * now * (now - 1) / 2;
    --now;
    cur += 1LL * now * (now - 1) / 2;

    PNN1(cur);
  }

#ifndef ONLINE_JUDGE
  fclose(stdin);
  fclose(stdout);
#endif

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#include <float.h>
#include <time.h>

#define loop(i, start, end) for (i = (start); i <= (end); i++)
#define loopIterators(it, vec) for (it = (vec).begin(); it != (vec).end(); it++)
#define loopIteratorsRev(it, vec)                                              \
  for (it = (vec).rbegin(); it != (vec).rend(); it++)
#define loopRev(i, start, end) for (i = (start); i >= (end); i--)
#define FLASH                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define testCases(t)                                                           \
  cin >> t;                                                                    \
  while (t--)
#define PN(n) cout << (n)
#define PN1(n) cout << (n) << " "
#define PN2(a, b) cout << (a) << " " << (b) << " "
#define PNN1(n) cout << (n) << endl
#define PNN2(a, b) cout << (a) << " " << (b) << endl
#define PNN3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define PrintArray(ar, n)                                                      \
  for (i = 0; i < n; i++)                                                      \
    cout << ar[i] << " ";                                                      \
  cout << endl
#define PrintObject(ar, it)                                                    \
  loopIterators(it, ar) cout << *it << " ";                                    \
  cout << endl
#define PrintMap(map, it) loopIterators(it, map) PNN2(it->first, it->second)
#define sz(a) sizeof(a)
#define IN1(n) cin >> n
#define IN2(a, b) cin >> a >> b
#define IN3(a, b, c) cin >> a >> b >> c
#define ALL(s) s.begin(), s.end()
#define SWAP(a, b, c)                                                          \
  c = a;                                                                       \
  a = b;                                                                       \
  b = c
#define STRSP(s) scanf("%[^\n]%*c", s)
#define ENTER PNN1("");
#define MOD7 1000000007
#define MOD9 10000009
#define MAX 1000000000
#define llPair std::pair<lli, lli>
#define iiPair std::pair<int, int>
#define ONLINE_JUDGE

typedef long long int lli;
typedef long int li;
typedef unsigned long long int ulli;
typedef long double ld;

using namespace std;

int main(void) {
  FLASH

#ifndef ONLINE_JUDGE
  freopen("input-stream.txt", "r", stdin);
  freopen("output-stream.txt", "w", stdout);
#endif

  int n, i;
  IN1(n);
  int ar[n];
  std::map<int, int> d;

  loop(i, 0, n - 1) IN1(ar[i]);
  loop(i, 0, n - 1)++ d[ar[i]];

  lli total = 0LL;
  for (auto cur : d)
    total += 1LL * cur.second * (cur.second - 1) / 2;

  // PNN1(total);
  loop(i, 0, n - 1) {
    lli now = d[ar[i]];
    lli cur = total - 1LL * now * (now - 1) / 2;
    --now;
    cur += 1LL * now * (now - 1) / 2;

    PNN1(cur);
  }

#ifndef ONLINE_JUDGE
  fclose(stdin);
  fclose(stdout);
#endif

  return 0;
}