prithivMLmods/Coder-Stat huggingface dataset:
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define int long long
using namespace std;
int n;
int a[100005], num[100005], sum;
int cal(int x) { return x * (x - 1) / 2; }
signed main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), num[a[i]]++;
  for (int i = 1; i <= n; i++)
    sum += cal(num[i]);
  for (int i = 1; i <= n; i++)
    printf("%lld\n", sum - cal(num[a[i]]) + cal(num[a[i]] - 1));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define int long long
using namespace std;
int n;
int a[1000005], num[1000005], sum;
int cal(int x) { return x * (x - 1) / 2; }
signed main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), num[a[i]]++;
  for (int i = 1; i <= n; i++)
    sum += cal(num[i]);
  for (int i = 1; i <= n; i++)
    printf("%lld\n", sum - cal(num[a[i]]) + cal(num[a[i]] - 1));
  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

long long mp[30005] = {0};

signed main() {

  long long n;
  cin >> n;

  long long a[n + 5];

  set<long long> st;

  for (long long i = 0; i < n; ++i) {
    /* code */
    cin >> a[i];
    st.insert(a[i]);
    mp[a[i]]++;
  }

  long long sum = 0;

  for (long long ele : st) {
    long long temp = (mp[ele] * (mp[ele] - 1)) / 2;
    long long zero = 0;
    sum += (max(zero, temp));
  }

  for (long long i = 0; i < n; i++) {
    cout << sum - (mp[a[i]] - 1) << '\n';
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

long long mp[300005] = {0};

signed main() {

  long long n;
  cin >> n;

  long long a[n + 5];

  set<long long> st;

  for (long long i = 0; i < n; ++i) {
    /* code */
    cin >> a[i];
    st.insert(a[i]);
    mp[a[i]]++;
  }

  long long sum = 0;

  for (long long ele : st) {
    long long temp = (mp[ele] * (mp[ele] - 1)) / 2;
    long long zero = 0;
    sum += (max(zero, temp));
  }

  for (long long i = 0; i < n; i++) {
    cout << sum - (mp[a[i]] - 1) << '\n';
  }

  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
const int INF = 2147483647; // int max
const int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n;
  cin >> n;
  unordered_map<ll, ll> m;
  vector<ll> v(n);
  rep(i, n) {
    int in;
    cin >> in;
    v[i] = in;
    m[in]++;
  }
  unordered_map<ll, ll> memo;
  for (int i = 1; i <= n; i++) {
    ll ans = 0;
    if (memo.find(v.at(i - 1)) == memo.end()) {
      for (auto a : m) {
        if (a.first == v.at(i - 1)) {
          ans += (a.second - 1) * (a.second - 2) / 2;
        } else {
          ans += a.second * (a.second - 1) / 2;
        }
      }
      cout << ans << endl;
      memo[v.at(i - 1)] = ans;
    } else {
      cout << memo.at(v.at(i - 1)) << endl;
    }
  }
  return 0;
}
/*
やっほい！
　　　　　　　やほほい！
　　　　+　　 　*
　　　 ∧∧　　. ∧∞∧　*
*　ヽ(=´ω｀)人(´ω｀*)ﾉ
　.～（ O x.） （ 　 O)～　+
。*　 　∪　　　　∪
*/

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
const int INF = 2147483647; // int max
const int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n;
  cin >> n;
  unordered_map<ll, ll> m;
  vector<ll> v(n);
  rep(i, n) {
    int in;
    cin >> in;
    v[i] = in;
    m[in]++;
  }
  ll sum = 0;
  for (auto a : m) {
    sum += a.second * (a.second - 1) / 2;
  }
  for (int i = 0; i < n; i++) {
    cout << sum - (m.at(v[i])) * (m.at(v[i]) - 1) / 2 +
                (m.at(v[i]) - 1) * (m.at(v[i]) - 2) / 2
         << endl;
  }
  return 0;
}
/*
やっほい！
　　　　　　　やほほい！
　　　　+　　 　*
　　　 ∧∧　　. ∧∞∧　*
*　ヽ(=´ω｀)人(´ω｀*)ﾉ
　.～（ O x.） （ 　 O)～　+
。*　 　∪　　　　∪
*/
Problem p02732 (C++)

Buggy submission (status: Runtime Error):

#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, N) for (int i = 0; i < N; i++)
#define ll long long
#define int long long

using namespace std;

class Fact {
public:
  vector<ll> f;
  Fact(ll n) {
    f = vector<ll>(n + 1);
    f[0] = 1;
    f[1] = 1;
    for (int i = 2; i <= n; i++)
      f[i] = f[i - 1] * i;
  }
  ll get(ll n) { return f[n]; }
  ll comb(ll n, ll r) { return get(n) / (get(n - r) * get(r)); }
};

signed main(void) {
  int N;
  cin >> N;
  Fact f(N + 1);
  vector<int> A(N), cnt(N);
  int totComb = 0;
  rep(i, N) {
    cin >> A[i];
    A[i]--;
    cnt[A[i]]++;
  }

  rep(i, N) if (cnt[i] >= 2) totComb += f.comb(cnt[i], 2);

  rep(i, N) { cout << totComb - (cnt[A[i]] - 1) << endl; }

  return 0;
}

Fixed submission:

#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, N) for (int i = 0; i < N; i++)
#define ll long long
#define int long long

using namespace std;

class Fact {
public:
  vector<ll> f;
  Fact(ll n) {
    f = vector<ll>(n + 1);
    f[0] = 1;
    f[1] = 1;
    for (int i = 2; i <= n; i++)
      f[i] = f[i - 1] * i;
  }
  ll get(ll n) { return f[n]; }
  ll comb(ll n, ll r) { return get(n) / (get(n - r) * get(r)); }
};

signed main(void) {
  int N;
  cin >> N;
  Fact f(N + 1);
  vector<int> A(N), cnt(N);
  int totComb = 0;
  rep(i, N) {
    cin >> A[i];
    A[i]--;
    cnt[A[i]]++;
  }

  rep(i, N) if (cnt[i] >= 2) totComb += (cnt[i] * (cnt[i] - 1)) / 2;

  rep(i, N) { cout << totComb - (cnt[A[i]] - 1) << endl; }

  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;
long long a[N], b[N];

int main() {

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

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  long long int n = 0;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[a[i]]++;
  }

  long long sum = 0;
  for (int i = 0; i < N; i++) {
    sum += (b[i] * (b[i] - 1) / 2);
  }

  for (int i = 0; i < n; i++) {
    cout << sum - b[a[i]] + 1 << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;
long long a[N], b[N];

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  long long int n = 0;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[a[i]]++;
  }

  long long sum = 0;
  for (int i = 0; i < N; i++) {
    sum += (b[i] * (b[i] - 1) / 2);
  }

  for (int i = 0; i < n; i++) {
    cout << sum - b[a[i]] + 1 << endl;
  }

  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
template <class T> void chmin(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void chmax(T &x, T y) {
  if (x > y)
    x = y;
}
int N;
map<int, ll> mp;
int A[100010];
ll sum;

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i]]++;
  }
  for (auto u : mp) {
    ll cnt = u.second;
    sum += cnt * (cnt - (ll)1) / (ll)2;
  }
  for (int i = 0; i < N; i++) {
    ll c = mp[A[i]];
    cout << sum - c + 1 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
template <class T> void chmin(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void chmax(T &x, T y) {
  if (x > y)
    x = y;
}
int N;
map<int, ll> mp;
int A[200010];
ll sum;

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i]]++;
  }
  for (auto u : mp) {
    ll cnt = u.second;
    sum += cnt * (cnt - (ll)1) / (ll)2;
  }
  for (int i = 0; i < N; i++) {
    ll c = mp[A[i]];
    cout << sum - c + 1 << endl;
  }
  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, a) for (auto &i : a)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
signed _main();
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> using v = vector<T>;
template <class T> using vv = vector<v<T>>;
const int MOD = 1e9 + 7;
const long long INF = 1LL << 60;

signed _main() {
  int N;
  cin >> N;
  v<int> A(N);
  unordered_map<int, int> map;
  rep(i, N) {
    cin >> A[i];
    map[A[i]]++;
  }
  rep(i, N) {
    map[A[i]]--;
    int ans = 0;
    fore(j, map) { ans += j.second * (j.second - 1) / 2; }
    cout << ans << endl;
    map[A[i]]++;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, a) for (auto &i : a)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
signed _main();
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> using v = vector<T>;
template <class T> using vv = vector<v<T>>;
const int MOD = 1e9 + 7;
const long long INF = 1LL << 60;

signed _main() {
  int N;
  cin >> N;
  v<int> A(N);
  unordered_map<int, int> map;
  rep(i, N) {
    cin >> A[i];
    map[A[i]]++;
  }
  int ans = 0;
  fore(j, map) { ans += j.second * (j.second - 1) / 2; }
  rep(i, N) { cout << ans - map[A[i]] + 1 << endl; }

  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <vector>

#define REP(i, n, N) for (ll i = (n); i < (N); i++)
// #define RREP(i, n ,N) for(ll i = (N-1); i >= (n); i--)
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << " " << (b) << endl

using namespace std;
typedef long long ll;

const long long MOD = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, 0, n) { cin >> a.at(i); }

  vector<ll> comb(n + 1, 0);
  vector<ll> comb_self(n + 1, 0);

  vector<ll> copy = a;
  sort(copy.begin(), copy.end());
  ll count = 1;
  ll tmp = copy.at(0);
  REP(i, 0, n) {
    if (i == 0) {
      continue;
    }
    if (tmp != copy.at(i)) {
      if (count >= 2) {
        comb.at(tmp) = count * (count - 1) / 2;
        comb_self.at(tmp) = (count - 2) * (count - 1) / 2;
      }
      tmp = copy.at(i);
      count = 1;
    } else {
      count++;
    }
  }

  if (count >= 2) {
    comb.at(tmp) = count * (count - 1) / 2;
    comb_self.at(tmp) = (count - 2) * (count - 1) / 2;
  }

  //    ll sum = accumulate(comb.begin(), comb.end(), 0);
  REP(i, 0, n) {
    p(accumulate(comb.begin(), comb.end(), ll(0)) - comb.at(a.at(i)) +
      comb_self.at(a.at(i)));
  }
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <vector>

#define REP(i, n, N) for (ll i = (n); i < (N); i++)
// #define RREP(i, n ,N) for(ll i = (N-1); i >= (n); i--)
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << " " << (b) << endl

using namespace std;
typedef long long ll;

const long long MOD = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, 0, n) { cin >> a.at(i); }

  vector<ll> comb(n + 1, 0);
  vector<ll> comb_self(n + 1, 0);

  vector<ll> copy = a;
  sort(copy.begin(), copy.end());
  ll count = 1;
  ll tmp = copy.at(0);
  REP(i, 0, n) {
    if (i == 0) {
      continue;
    }
    if (tmp != copy.at(i)) {
      if (count >= 2) {
        comb.at(tmp) = count * (count - 1) / 2;
        comb_self.at(tmp) = (count - 2) * (count - 1) / 2;
      }
      tmp = copy.at(i);
      count = 1;
    } else {
      count++;
    }
  }

  if (count >= 2) {
    comb.at(tmp) = count * (count - 1) / 2;
    comb_self.at(tmp) = (count - 2) * (count - 1) / 2;
  }

  ll sum = accumulate(comb.begin(), comb.end(), 0LL);
  REP(i, 0, n) { p(sum - comb.at(a.at(i)) + comb_self.at(a.at(i))); }
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100010;
ll n, a[N], num[N];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i], num[a[i]]++;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (num[i] - 1) * num[i] / 2;
    // cout<<num[i]<<endl;
  }
  //	cout<<ans<<endl;
  for (int i = 0; i < n; i++)
    cout << ans - num[a[i]] + 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200010;
ll n, a[N], num[N];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i], num[a[i]]++;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (num[i] - 1) * num[i] / 2;
    // cout<<num[i]<<endl;
  }
  //	cout<<ans<<endl;
  for (int i = 0; i < n; i++)
    cout << ans - num[a[i]] + 1 << endl;
  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define inf 1000000000000000000

#define ll long long
#define max(A, B) ((A) > (B) ? (A) : (B))
#define min(A, B) ((A) < (B) ? (A) : (B))
#define pair(A, B) make_pair(A, B)

using namespace std;

typedef pair<int, int> pi;
typedef pair<pair<int, int>, int> pii;

void perform() {
  ll b[100001] = {0};
  ll d[100001] = {0};
  int n;
  cin >> n;
  ll arr[n];
  ll totalDP = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    b[t]++;
    arr[i] = t;
    totalDP -= d[t];
    d[t] = (b[t] * (b[t] - 1)) / 2;
    totalDP += d[t];
  }

  for (int i = 0; i < n; i++) {
    ll ans = totalDP;
    if (totalDP && b[arr[i]] - 1 > 0) {
      ans -= d[arr[i]];
      ans += ((b[arr[i]] - 1) * (b[arr[i]] - 2)) / 2;
    }
    cout << ans << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1; // cin >> t;
  while (t--) {
    perform();
  }
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define inf 1000000000000000000

#define ll long long
#define max(A, B) ((A) > (B) ? (A) : (B))
#define min(A, B) ((A) < (B) ? (A) : (B))
#define pair(A, B) make_pair(A, B)

using namespace std;

typedef pair<int, int> pi;
typedef pair<pair<int, int>, int> pii;

void perform() {
  ll b[200001] = {0};
  ll d[200001] = {0};
  int n;
  cin >> n;
  ll arr[n];
  ll totalDP = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    b[t]++;
    arr[i] = t;
    totalDP -= d[t];
    d[t] = (b[t] * (b[t] - 1)) / 2;
    totalDP += d[t];
  }

  for (int i = 0; i < n; i++) {
    ll ans = totalDP;
    if (totalDP && b[arr[i]] - 1 > 0) {
      ans -= d[arr[i]];
      ans += ((b[arr[i]] - 1) * (b[arr[i]] - 2)) / 2;
    }
    cout << ans << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1; // cin >> t;
  while (t--) {
    perform();
  }
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long long con(long long n) { return n * (n - 1) / 2; }

int main() {
  long long N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<int> cnt(N);

  for (int i = 0; i < N; i++)
    cnt.at(A.at(i))++;

  long long sum = 0;
  for (int i = 0; i < N; i++) {
    sum += con(cnt.at(i));
  }
  for (int i = 0; i < N; i++) {
    long long ans = sum - con(cnt.at(A.at(i))) + con(cnt.at(A.at(i)) - 1);
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

long long con(long long n) { return n * (n - 1) / 2; }

int main() {
  long long N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    A.at(i)--;
  }
  vector<int> cnt(N);

  for (int i = 0; i < N; i++)
    cnt.at(A.at(i))++;

  long long sum = 0;
  for (int i = 0; i < N; i++) {
    sum += con(cnt.at(i));
  }
  for (int i = 0; i < N; i++) {
    long long ans = sum - con(cnt.at(A.at(i))) + con(cnt.at(A.at(i)) - 1);
    cout << ans << endl;
  }
  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 7;
int n;
int a[N];
int occ[N];
long long ans;

int main() {
  //  freopen("in.in" , "r" , stdin);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += occ[a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    ans -= --occ[a[i]];
    cout << ans << "\n";
    ans += occ[a[i]]++;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;
int n;
int a[N];
int occ[N];
long long ans;

int main() {
  //  freopen("in.in" , "r" , stdin);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += occ[a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    ans -= --occ[a[i]];
    cout << ans << "\n";
    ans += occ[a[i]]++;
  }
  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N;
  int A[100000];
  int cnt[100000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }

  for (int i = 0; i < N; i++) {
    cnt[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    cnt[A[i]] += 1;
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += (long long)cnt[i] * (cnt[i] - 1) / 2;
  }
  for (int i = 0; i < N; i++) {
    if (cnt[A[i]] > 1) {
      cout << ans - cnt[A[i]] + 1 << "\n";
    } else {
      cout << ans << "\n";
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N;
  int A[200000];
  int cnt[200000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }

  for (int i = 0; i < N; i++) {
    cnt[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    cnt[A[i]] += 1;
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += (long long)cnt[i] * (cnt[i] - 1) / 2;
  }
  for (int i = 0; i < N; i++) {
    if (cnt[A[i]] > 1) {
      cout << ans - cnt[A[i]] + 1 << "\n";
    } else {
      cout << ans << "\n";
    }
  }
}
Problem p02732 (C++)

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

const int maxn = 1e5 + 50;
long long a[maxn], cnt[maxn];
set<int> temp;

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (cnt[i])
      ans += (cnt[i] - 1) * cnt[i] / 2;
  }
  for (int i = 1; i <= n; ++i)
    cout << ans - cnt[a[i]] + 1 << endl;
}

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

const int maxn = 2e5 + 50;
long long a[maxn], cnt[maxn];
set<int> temp;

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (cnt[i])
      ans += (cnt[i] - 1) * cnt[i] / 2;
  }
  for (int i = 1; i <= n; ++i)
    cout << ans - cnt[a[i]] + 1 << endl;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

using ll = long long;

ll N;
ll A[100005];
ll cnt[100005];

ll calc(ll n) { return n * (n - 1) / 2; }

int main() {
  cin >> N;
  ll a;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
    cnt[A[i]]++;
  }
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    sum += calc(cnt[i]);
  }
  for (int i = 0; i < N; i++) {
    cout << sum - cnt[A[i]] + 1 << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

using ll = long long;

ll N;
ll A[200005];
ll cnt[200005];

ll calc(ll n) { return n * (n - 1) / 2; }

int main() {
  cin >> N;
  ll a;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
    cnt[A[i]]++;
  }
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    sum += calc(cnt[i]);
  }
  for (int i = 0; i < N; i++) {
    cout << sum - cnt[A[i]] + 1 << endl;
  }

  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;

ll A[100010];
ll cnt[100010];
int main() {
  int N;
  cin >> N;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    ans += cnt[A[i]];
    cnt[A[i]]++;
  }
  for (int i = 0; i < N; i++) {
    cout << ans - cnt[A[i]] + 1 << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;

ll A[200010];
ll cnt[200010];
int main() {
  int N;
  cin >> N;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    ans += cnt[A[i]];
    cnt[A[i]]++;
  }
  for (int i = 0; i < N; i++) {
    cout << ans - cnt[A[i]] + 1 << endl;
  }
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i <= m; i++)
#define read(n) cin >> n;
#define write(n) cout << n;
using namespace std;
const int N = 1e5 + 5;
int n, cnt[N], x[N];
ll sum = 0;
int main() {
  cin >> n;
  rep(i, n) cin >> x[i], cnt[x[i]]++;
  rep(i, n + 1) sum += 1ll * cnt[i] * (cnt[i] - 1) / 2;
  //	cout<<sum<<"\n";
  rep(j, n) {
    ll ans = sum;
    int i = x[j];
    if (cnt[i] != 0) {
      ans = ans - 1ll * cnt[i] * (cnt[i] - 1) / 2 +
            1ll * (cnt[i] - 1) * (cnt[i] - 2) / 2;
    }
    cout << ans << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i <= m; i++)
#define read(n) cin >> n;
#define write(n) cout << n;
using namespace std;
const int N = 2e5 + 5;
int n, cnt[N], x[N];
ll sum = 0;
int main() {
  cin >> n;
  rep(i, n) cin >> x[i], cnt[x[i]]++;
  rep(i, n + 1) sum += 1ll * cnt[i] * (cnt[i] - 1) / 2;
  //	cout<<sum<<"\n";
  rep(j, n) {
    ll ans = sum;
    int i = x[j];
    if (cnt[i] != 0) {
      ans = ans - 1ll * cnt[i] * (cnt[i] - 1) / 2 +
            1ll * (cnt[i] - 1) * (cnt[i] - 2) / 2;
    }
    cout << ans << "\n";
  }
  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int cnt[100010], a[100010];

int main() {
  int n, c = 0;
  ll ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> a[i];
    // if(cnt[a[i]]==0)c++;
    cnt[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[i]) {
      ans += cnt[i] * (cnt[i] - 1) / 2;
    }
  }
  for (int i = 0; i < n; i++) {
    ans -= cnt[a[i]] * (cnt[a[i]] - 1) / 2;
    cnt[a[i]]--;
    // ans+=cnt[a[i]]*(cnt[a[i]]-1)/2;

    cout << ans + cnt[a[i]] * (cnt[a[i]] - 1) / 2 << '\n';
    cnt[a[i]]++;
    ans += cnt[a[i]] * (cnt[a[i]] - 1) / 2;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll cnt[200010], a[200010];

int main() {
  int n, c = 0;
  ll ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> a[i];
    // if(cnt[a[i]]==0)c++;
    cnt[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[i]) {
      ans += cnt[i] * (cnt[i] - 1) / 2;
    }
  }
  for (int i = 0; i < n; i++) {
    ans -= cnt[a[i]] * (cnt[a[i]] - 1) / 2;
    cnt[a[i]]--;
    // ans+=cnt[a[i]]*(cnt[a[i]]-1)/2;

    cout << ans + cnt[a[i]] * (cnt[a[i]] - 1) / 2 << '\n';
    cnt[a[i]]++;
    ans += cnt[a[i]] * (cnt[a[i]] - 1) / 2;
  }
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SpeeD ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define endl '\n'
// pair<int, int> *p=new pair<int, int>[n];
// std::vector<pair<int,int>> vp;
// v.push_back(make_pair(x,y))
// pair<int, int> p[1000001];
// string *s=new string [n];
// int freq[e];
// freq[a[i]-'a']++; freq[a[i]]++;
using namespace std;
ll n, t, m, c, x, y, sum, mx, mn, avg, pos, dif, j, dis;

map<ll, ll> mp;
bool vis[(int)1e5];
bool isPrime(int x) {
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0)
      return false;
  return true;
}

int main(void) {

  SpeeD cin >> n;
  ll *a = new ll[n];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }

  for (int i = 0; i < n; i++) {
    if (!vis[a[i]])
      m += (mp[a[i]] * (mp[a[i]] - 1)) / 2;
    vis[a[i]] = true;
  }
  for (int i = 0; i < n; i++) {
    cout << m - mp[a[i]] + 1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define SpeeD ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define endl '\n'
// pair<int, int> *p=new pair<int, int>[n];
// std::vector<pair<int,int>> vp;
// v.push_back(make_pair(x,y))
// pair<int, int> p[1000001];
// string *s=new string [n];
// int freq[e];
// freq[a[i]-'a']++; freq[a[i]]++;
using namespace std;
ll n, t, m, c, x, y, sum, mx, mn, avg, pos, dif, j, dis;

map<ll, ll> mp;
bool vis[(int)1e9];
bool isPrime(int x) {
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0)
      return false;
  return true;
}

int main(void) {

  SpeeD cin >> n;
  ll *a = new ll[n];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }

  for (int i = 0; i < n; i++) {
    if (!vis[a[i]])
      m += (mp[a[i]] * (mp[a[i]] - 1)) / 2;
    vis[a[i]] = true;
  }
  for (int i = 0; i < n; i++) {
    cout << m - mp[a[i]] + 1 << endl;
  }
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
/* ***********************************************
Author        :taciturnity
Created Time  :2020/2/21 8:23:51
File Name     :TEST.cpp
************************************************ */

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define INF 0x3f3f3f3f
#define rep(i, x, y) for (int i = x; i <= y; ++i)
#define fori(i, x, y) for (int i = x; i >= y; --i)
#define bug puts("--------------")
#define out(x) cout << x << '\n';
inline char gc() {
  char op[2];
  scanf("%s", op);
  return op[0];
}
const int N = 1e5 + 10;
const double eps = 0.00000001;
const ll mod = 1e9 + 7;
using namespace std;
ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
map<int, int> hap;
int a[N];
int main() {
  int n = read();
  rep(i, 1, n) a[i] = read(), hap[a[i]]++;
  ll ans = 0;
  for (auto p = hap.begin(); p != hap.end(); ++p) {
    ll d = p->second;
    ans += (d - 1) * d / 2;
  }
  rep(i, 1, n) {
    ll d = hap[a[i]];
    ll tmp = ans;
    tmp -= d * (d - 1) / 2;
    d--;
    tmp += (d - 1) * d / 2;
    cout << tmp << endl;
  }
}

Fixed submission:
/* ***********************************************
Author        :taciturnity
Created Time  :2020/2/21 8:23:51
File Name     :TEST.cpp
************************************************ */

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define INF 0x3f3f3f3f
#define rep(i, x, y) for (int i = x; i <= y; ++i)
#define fori(i, x, y) for (int i = x; i >= y; --i)
#define bug puts("--------------")
#define out(x) cout << x << '\n';
inline char gc() {
  char op[2];
  scanf("%s", op);
  return op[0];
}
const int N = 2e5 + 10;
const double eps = 0.00000001;
const ll mod = 1e9 + 7;
using namespace std;
ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
map<int, int> hap;
int a[N];
int main() {
  int n = read();
  rep(i, 1, n) a[i] = read(), hap[a[i]]++;
  ll ans = 0;
  for (auto p = hap.begin(); p != hap.end(); ++p) {
    ll d = p->second;
    ans += (d - 1) * d / 2;
  }
  rep(i, 1, n) {
    ll d = hap[a[i]];
    ll tmp = ans;
    tmp -= d * (d - 1) / 2;
    d--;
    tmp += (d - 1) * d / 2;
    cout << tmp << endl;
  }
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, c) for (int i = 0; i < (int)c; ++i)
using llint = long long int;
using namespace std;

int main() {
  int N;
  map<llint, llint> mp;
  vector<llint> A(N);
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    mp[A[i]]++;
  }

  llint num = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    llint seco = itr->second;
    if (seco == 0)
      continue;
    else
      num += seco * (seco - 1) / 2;
  }

  for (int i = 0; i < N; i++) {
    if (mp[A[i]] < 2)
      cout << num << endl;
    else
      cout << num - mp[A[i]] + 1 << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, c) for (int i = 0; i < (int)c; ++i)
using llint = long long int;
using namespace std;

int main() {
  int N;
  map<llint, llint> mp;
  int A[200006];
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    mp[A[i]]++;
  }

  llint num = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    llint seco = itr->second;
    if (seco == 0)
      continue;
    else
      num += seco * (seco - 1) / 2;
  }

  for (int i = 0; i < N; i++) {
    if (mp[A[i]] < 2)
      cout << num << endl;
    else
      cout << num - mp[A[i]] + 1 << endl;
  }

  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define x first
#define y second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define sz(a) (int)(a.size())
ll ans = 0;
int n;
int b[100005];
map<int, ll> a;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    a[b[i]]++;
  }
  for (auto x : a)
    ans += x.y * (x.y - 1) / 2;
  for (int i = 0; i < n; i++) {
    ans -= a[b[i]] * (a[b[i]] - 1) / 2;
    a[b[i]]--;
    ans += a[b[i]] * (a[b[i]] - 1) / 2;
    cout << ans << endl;
    ans -= a[b[i]] * (a[b[i]] - 1) / 2;
    a[b[i]]++;
    ans += a[b[i]] * (a[b[i]] - 1) / 2;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define x first
#define y second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define sz(a) (int)(a.size())
ll ans = 0;
int n;
int b[200005];
map<int, ll> a;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    a[b[i]]++;
  }
  for (auto x : a)
    ans += x.y * (x.y - 1) / 2;
  for (int i = 0; i < n; i++) {
    ans -= a[b[i]] * (a[b[i]] - 1) / 2;
    a[b[i]]--;
    ans += a[b[i]] * (a[b[i]] - 1) / 2;
    cout << ans << endl;
    ans -= a[b[i]] * (a[b[i]] - 1) / 2;
    a[b[i]]++;
    ans += a[b[i]] * (a[b[i]] - 1) / 2;
  }
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <map>
#define F(i, x, y) for (int i = x; i <= y; ++i)
using namespace std;
int n, a[100005];
long long sum;
map<int, int> m;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    ++m[a[i]];
  }
  for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
    int t = it->second;
    sum += 1ll * (t - 1) * t / 2;
  }
  for (int i = 1; i <= n; ++i) {
    printf("%lld\n", sum - m[a[i]] + 1);
  }
  return 0;
}


Fixed submission:
#include <cstdio>
#include <map>
#define F(i, x, y) for (int i = x; i <= y; ++i)
using namespace std;
int n, a[200005];
long long sum;
map<int, int> m;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    ++m[a[i]];
  }
  for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
    int t = it->second;
    sum += 1ll * (t - 1) * t / 2;
  }
  for (int i = 1; i <= n; ++i) {
    printf("%lld\n", sum - m[a[i]] + 1);
  }
  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int fa[100008], fb[100008], fc[100008];
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", fa + i);
    fb[i] = fa[i];
  }
  sort(fb, fb + n);
  long long sum = 0, s = 1;
  for (int i = 1; i < n; i++) {
    if (fb[i] == fb[i - 1])
      s++;
    else {
      fc[fb[i - 1]] = s;
      sum += (s - 1) * s / 2;
      s = 1;
    }
  }
  fc[fb[n - 1]] = s;
  sum += (s - 1) * s / 2;
  for (int i = 0; i < n; i++) {
    int x = fc[fa[i]];
    printf("%lld\n", sum - x + 1);
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int fa[200008], fb[200008], fc[200008];
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", fa + i);
    fb[i] = fa[i];
  }
  sort(fb, fb + n);
  long long sum = 0, s = 1;
  for (int i = 1; i < n; i++) {
    if (fb[i] == fb[i - 1])
      s++;
    else {
      fc[fb[i - 1]] = s;
      sum += (s - 1) * s / 2;
      s = 1;
    }
  }
  fc[fb[n - 1]] = s;
  sum += (s - 1) * s / 2;
  for (int i = 0; i < n; i++) {
    int x = fc[fa[i]];
    printf("%lld\n", sum - x + 1);
  }
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, c) for (int i = 0; i < (int)c; ++i)
using llint = long long int;
using namespace std;

int main() {
  int N;
  map<int, llint> mp;
  vector<int> A(N, 0);
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    mp[A[i]]++;
  }

  llint num = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    llint seco = itr->second;
    if (seco == 0)
      continue;
    else
      num += seco * (seco - 1) / 2;
  }

  for (int i = 0; i < N; i++) {
    if (mp[A[i]] < 2)
      cout << num << endl;
    else
      cout << num - (mp[A[i]] * (mp[A[i]] - 1) / 2) +
                  ((mp[A[i]] - 1) * (mp[A[i]] - 2) / 2)
           << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, c) for (int i = 0; i < (int)c; ++i)
using llint = long long int;
using namespace std;

int main() {
  int N;
  map<int, llint> mp;
  int A[300000];
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    mp[A[i]]++;
  }

  llint num = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    llint seco = itr->second;
    if (seco == 0)
      continue;
    else
      num += seco * (seco - 1) / 2;
  }

  for (int i = 0; i < N; i++) {
    if (mp[A[i]] < 2)
      cout << num << endl;
    else
      cout << num - (mp[A[i]] * (mp[A[i]] - 1) / 2) +
                  ((mp[A[i]] - 1) * (mp[A[i]] - 2) / 2)
           << endl;
  }

  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define eps 1e-6
#define ll long long
using namespace std;
const int N = 1e3 + 5;
ll a[N], sum, b[N];
ll c(int x, int y) {
  ll a = 1, b = 1;
  for (int i = y; i > y - x; i--)
    a *= i;
  for (int i = 1; i <= x; i++)
    b *= i;
  return a / b;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, x;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    a[x]++;
    b[i] = x;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] > 1)
      sum += c(2, a[i]);
  }
  // cout<<sum<<endl;
  // for(int i=1;i<=n;i++)cout<<a[b[i]]<<" ";
  // cout<<endl;
  for (int i = 1; i <= n; i++) {
    if (a[b[i]] >= 2)
      cout << sum - a[b[i]] + 1 << endl;
    else
      cout << sum << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define eps 1e-6
#define ll long long
using namespace std;
const int N = 2e5 + 5;
ll a[N], sum, b[N];
ll c(int x, int y) {
  ll a = 1, b = 1;
  for (int i = y; i > y - x; i--)
    a *= i;
  for (int i = 1; i <= x; i++)
    b *= i;
  return a / b;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, x;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    a[x]++;
    b[i] = x;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] > 1)
      sum += c(2, a[i]);
  }
  // cout<<sum<<endl;
  // for(int i=1;i<=n;i++)cout<<a[b[i]]<<" ";
  // cout<<endl;
  for (int i = 1; i <= n; i++) {
    if (a[b[i]] >= 2)
      cout << sum - a[b[i]] + 1 << endl;
    else
      cout << sum << endl;
  }
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 100010, M = 110;

int n;
int a[N];
int cnt[N];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    a[i] = x;
    cnt[x]++;
  }

  LL tot = 0;
  for (int i = 1; i <= n; i++)
    tot += (LL)cnt[i] * (cnt[i] - 1) / 2LL;

  for (int i = 0; i < n; i++) {
    LL t = tot;
    int x = a[i];
    t -= (LL)cnt[x] * (cnt[x] - 1) / 2LL;
    t += (LL)(cnt[x] - 2) * (cnt[x] - 1) / 2LL;
    printf("%lld\n", t);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 200010, M = 110;

int n;
int a[N];
int cnt[N];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    a[i] = x;
    cnt[x]++;
  }

  LL tot = 0;
  for (int i = 1; i <= n; i++)
    tot += (LL)cnt[i] * (cnt[i] - 1) / 2LL;

  for (int i = 0; i < n; i++) {
    LL t = tot;
    int x = a[i];
    t -= (LL)cnt[x] * (cnt[x] - 1) / 2LL;
    t += (LL)(cnt[x] - 2) * (cnt[x] - 1) / 2LL;
    printf("%lld\n", t);
  }
  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a, b, c, i, j, k, l, m, n, ans, tmp, hoge, fuga, piyo;
ll x[10010], y[200200];
string s, str;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    y[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    y[x[i] - 1]++;
  }
  for (int i = 0; i < n; i++) {
    ans += y[i] * (y[i] - 1) / 2;
  }
  for (int i = 0; i < n; i++) {
    cout << ans - (y[x[i] - 1] - 1) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a, b, c, i, j, k, l, m, n, ans, tmp, hoge, fuga, piyo;
ll x[200200], y[200200];
string s, str;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    y[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    y[x[i] - 1]++;
  }
  for (int i = 0; i < n; i++) {
    ans += y[i] * (y[i] - 1) / 2;
  }
  for (int i = 0; i < n; i++) {
    cout << ans - (y[x[i] - 1] - 1) << endl;
  }
  return 0;
}
Problem p02732 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <ciso646>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <utility>
#include <vector>

// 解説ac

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
int momm(int a) {
  int sum = 0;
  for (int i = 1; i < a; i++)
    sum += i;
  return sum;
}
int main() { // ゼロツーかわいい！！！！
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) a[i]--;
  vector<int> cnt(n);
  rep(i, n) cnt[a[i]]++;
  long long tot = 0;
  rep(i, n) // 消去前のansを作り適宜消去していく
      tot += momm(cnt[i]);
  rep(i, n) {
    long long ans = tot;
    ans -= momm(cnt[a[i]]); // まるごと消去した
    cnt[a[i]]--;
    ans += momm(cnt[a[i]]); // 復元すべきところを復元した
    cout << ans << endl;
    cnt[a[i]]++;
  }
}


Fixed submission:
#include <algorithm>
#include <ciso646>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <utility>
#include <vector>

// 解説ac

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
long long momm(long long a) { return a * (a - 1) / 2; }
int main() { // ゼロツーかわいい！！！！
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) a[i]--;
  vector<int> cnt(n);
  rep(i, n) cnt[a[i]]++;
  long long tot = 0;
  rep(i, n) // 消去前のansを作り適宜消去していく
      tot += momm(cnt[i]);
  rep(i, n) {
    long long ans = tot;
    ans -= momm(cnt[a[i]]); // まるごと消去した
    cnt[a[i]]--;
    ans += momm(cnt[a[i]]); // 復元すべきところを復元した
    cout << ans << endl;
    cnt[a[i]]++;
  }
}
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H);
  for (int i = 0; i < H; ++i)
    cin >> S[i];
  int ans = inf;
  int tmp;
  int index;
  bool out;
  bool outout;
  map<int, int> amap;
  for (int bit = 0; bit < (1 << (H - 1)); ++bit) {
    for (int i = 0; i < H; ++i)
      amap[i] = 0;
    outout = true;
    tmp = 0;
    for (int i = 0; i < H - 1; ++i) {
      if ((bit & (1 << i)) != 0)
        tmp++;
    }
    for (int i = 0; i < W; ++i) {
      index = 0;
      out = true;
      for (int j = 0; j < H; ++j) {
        if (S[j][i] == '1') {
          amap[index]++;
          if (amap[index] > K)
            out = false;
        }
        if ((bit & (1 << j)) != 0)
          index++;
      }
      if (!out) {
        if (!outout)
          break;
        tmp++;
        for (int j = 0; j < H; ++j)
          amap[j] = 0;
        i--;
        outout = false;
      }
      outout = true;
    }
    if (!outout)
      continue;
    ans = min(ans, tmp);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H);
  for (int i = 0; i < H; ++i)
    cin >> S[i];
  int ans = inf;
  int tmp;
  int index;
  bool out;
  bool outout;
  map<int, int> amap;
  for (int bit = 0; bit < (1 << (H - 1)); ++bit) {
    for (int i = 0; i < H; ++i)
      amap[i] = 0;
    outout = true;
    tmp = 0;
    for (int i = 0; i < H - 1; ++i) {
      if ((bit & (1 << i)) != 0)
        tmp++;
    }
    for (int i = 0; i < W; ++i) {
      index = 0;
      out = true;
      for (int j = 0; j < H; ++j) {
        if (S[j][i] == '1') {
          amap[index]++;
          if (amap[index] > K)
            out = false;
        }
        if ((bit & (1 << j)) != 0)
          index++;
      }
      if (!out) {
        if (!outout)
          break;
        tmp++;
        for (int j = 0; j < H; ++j)
          amap[j] = 0;
        i--;
        outout = false;
      } else
        outout = true;
    }
    if (!outout)
      continue;
    ans = min(ans, tmp);
  }
  cout << ans << endl;
}
Problem p02733 (Python)

Buggy submission (status: Time Limit Exceeded):
import numpy as np
from itertools import product

H, W, K = map(int, input().split())
G = np.array([list(map(int, list(input()))) for _ in range(H)])

ans = float("inf")
for pattern in product([0, 1], repeat=H - 1):
    # スライスで横方向への割り方を表現する
    div = [0] + [i for i, p in enumerate(pattern, start=1) if p == 1] + [10]

    # 横方向へ割った後、各ブロックについて縦方向にホワイトチョコの数を修正
    rows = []
    for i in range(len(div) - 1):
        rows.append(np.sum(G[div[i] : div[i + 1]], axis=0))

    # 縦方向にすでにK個より多いホワイトチョコが含まれている場合はどうにもならない
    if [r for r in rows if np.any(r > K)]:
        continue

    tmp_ans = 0
    counts = [0] * len(rows)
    w = 0
    while w < W:
        for r in range(len(rows)):
            counts[r] += rows[r][w]
        if any([c > K for c in counts]):
            counts = [0] * len(rows)
            w -= 1
            tmp_ans += 1
        w += 1

    # 横に割った分を加算
    tmp_ans += len(div) - 2
    ans = min(ans, tmp_ans)

print(ans)


Fixed submission:
import numpy as np
from itertools import product

H, W, K = map(int, input().split())
G = np.array([list(map(int, list(input()))) for _ in range(H)])

ans = float("inf")
for pattern in product([0, 1], repeat=H - 1):
    # スライスで横方向への割り方を表現する
    div = [0] + [i for i, p in enumerate(pattern, start=1) if p == 1] + [10]

    # 横方向へ割った後、各ブロックについて縦方向にホワイトチョコの数を修正
    rows = []
    for i in range(len(div) - 1):
        rows.append(np.sum(G[div[i] : div[i + 1]], axis=0))

    # 縦方向にすでにK個より多いホワイトチョコが含まれている場合はどうにもならない
    if [r for r in rows if np.any(r > K)]:
        continue

    # 縦方向に貪欲に割っていく
    rows = [r.tolist() for r in rows]
    tmp_ans = 0
    counts = [0] * len(rows)
    w = 0
    while w < W:
        for r in range(len(rows)):
            counts[r] += rows[r][w]
        if any([c > K for c in counts]):
            counts = [0] * len(rows)
            w -= 1
            tmp_ans += 1
        w += 1

    # 横に割った分を加算
    tmp_ans += len(div) - 2
    ans = min(ans, tmp_ans)

print(ans)