Problem:
Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[allow(dead_code)]
mod utils {
    use std::io::{self, BufRead, BufReader, Stdin, StdinLock};
    use std::mem;
    use std::ops::{Deref, DerefMut};
    use std::str::{self, FromStr};

    pub struct Scanner<R: BufRead> {
        reader: R,
        buf: Vec<u8>,
    }

    impl<R: BufRead> Scanner<R> {
        pub fn new(reader: R) -> Scanner<R> {
            Scanner {
                reader: reader,
                buf: Vec::new(),
            }
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_until(|&b| b == b' ' || b == b'\n');
            self.parse()
        }

        pub fn reads(&mut self) -> &str {
            self.read_until(|&b| b == b' ' || b == b'\n');
            unsafe { str::from_utf8_unchecked(&self.buf) }
        }

        pub fn readl(&mut self) -> &str {
            self.read_until(|&b| b == b'\n');
            unsafe { str::from_utf8_unchecked(&self.buf) }
        }

        pub fn read_vec<T: FromStr>(&mut self, count: usize) -> Vec<T> {
            let mut vec = Vec::with_capacity(count);

            for _ in 0..count {
                vec.push(self.read());
            }

            return vec;
        }

        fn read_until<P: FnMut(&u8) -> bool>(&mut self, mut predicate: P) {
            self.buf.clear();
            let mut first = true;

            // https://doc.rust-lang.org/src/std/io/mod.rs.html#1623-1649
            loop {
                let (done, used) = {
                    let available = self.reader.fill_buf().unwrap();
                    if first && available.len() == 0 {
                        panic!("EOF reached.");
                    }

                    match available.iter().position(&mut predicate) {
                        Some(0) if first => {
                            (false, 1)
                        }
                        Some(i) => {
                            self.buf.extend_from_slice(&available[..i]);
                            (true, i + 1)
                        }
                        None => {
                            self.buf.extend_from_slice(available);
                            (false, available.len())
                        }
                    }
                };

                self.reader.consume(used);
                if done || used == 0 {
                    return;
                }

                first = false;
            }
        }

        #[inline]
        fn parse<T: FromStr>(&mut self) -> T {
            // 文字列をT型へパースする
            let elem = unsafe { str::from_utf8_unchecked(&self.buf) };
            elem.parse()
                .unwrap_or_else(|_| panic!(format!("failed parsing: {}", elem)))
        }
    }

    pub struct StdinScanner {
        stdin: Stdin,
        scanner: Scanner<BufReader<StdinLock<'static>>>,
    }

    impl StdinScanner {
        pub fn new() -> StdinScanner {
            let stdin = io::stdin();
            let stdinlock = unsafe { mem::transmute(stdin.lock()) };
            let reader = BufReader::new(stdinlock);
            let scanner = Scanner::new(reader);
            StdinScanner {
                stdin: stdin,
                scanner: scanner,
            }
        }
    }

    impl Deref for StdinScanner {
        type Target = Scanner<BufReader<StdinLock<'static>>>;

        #[inline]
        fn deref(&self) -> &Self::Target {
            &self.scanner
        }
    }

    impl DerefMut for StdinScanner {
        #[inline]
        fn deref_mut(&mut self) -> &mut Self::Target {
            &mut self.scanner
        }
    }
}

use std::collections::HashMap;

fn main() {
    let mut scanner = utils::StdinScanner::new();
    let n: usize = scanner.read();

    let a: Vec<usize> = scanner.read_vec(n);

    let mut counts = HashMap::<usize, usize>::new();

    for ai in &a {
        let entry = counts.entry(*ai).or_insert(0);
        *entry += 1;
    }

    let mut combinations = HashMap::<usize, usize>::with_capacity(counts.len());

    let mut sum = 0;
    for (k, v) in counts.iter_mut() {
        let combination = *v * (*v - 1) / 2;
        combinations.insert(*k, combination);
        if *v > 1 {
            sum += combination;
        }
    }

    for ai in &a {
        let count = counts[ai];
        if count == 1 {
            println!("{}", sum);
        } else if count == 2 {
            println!("{}", sum - 1);
        } else {
            let diff = count * (count - 1) / 2 - (count - 1) * (count - 2) / 2;
            println!("{}", sum - diff);
        }
    }
}