Language: C++

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>

using namespace std;

long long combination(int n) {
    return (n > 0) ? n * (n - 1) / 2 : 0;
}

int main() {
    int N;
    cin >> N;
    vector<int> a = vector<int>();
    for (int i = 0; i < N; ++i) {
        int temp;
        cin >> temp;
        a.push_back(temp);
    }

    map<int, int> ballNum2IndexNum = map<int, int>();
    map<int, vector<int> > ballNum2Indexes = map<int, vector<int> >();
    for (int i = 0; i < N; ++i) {
        int ai = a[i];
        if (ballNum2IndexNum.count(ai) == 0) {
            ballNum2IndexNum.insert(make_pair(ai, 1));
            ballNum2Indexes.insert(make_pair(ai, vector<int>({ i })));
        }
        else {
            ballNum2IndexNum.at(ai) += 1;
            ballNum2Indexes.at(ai).push_back(i);
        }
    }
    map<int, long long> ballNum2NumOfCombinations = map<int, long long>();
    for (map<int, int>::const_iterator it = ballNum2IndexNum.begin(); it != ballNum2IndexNum.end(); ++it) {
        int ballNum = it->first;
        ballNum2NumOfCombinations.insert(make_pair(ballNum, combination(it->second)));
    }
    long long totalCombination = 0;
    for (map<int, long long>::const_iterator it = ballNum2NumOfCombinations.begin(); it != ballNum2NumOfCombinations.end(); ++it) {
        totalCombination += it->second;
    }

    vector<int> res = vector<int>();
    for (int i = 0; i < N; ++i) {
        int ai = a[i];
        int indexNum = ballNum2IndexNum.at[ai];
        if (indexNum == 1) {
            res.push_back(totalCombination);
        }
        else { // if (indexNum >= 2) {
            res.push_back(totalCombination - combination(indexNum - 1));
        }
    }
    // for (map<int, int>::const_iterator it = ballNum2IndexNum.begin(); it != ballNum2IndexNum.end(); ++it) {
    //     int ai = it->first;
    //     const vector<int>& indexes = ballNum2Indexes.at(ai);
    //     for (int i = 0; i < indexes.size(); ++i) {
    //         int comb = combination(ballNum2IndexNum.at(ai) - 1);
    //         for (map<int, int>::const_iterator it = ballNum2NumOfCombinations.begin(); it != ballNum2NumOfCombinations.end(); ++it) {
    //             if (ai != it->first) {
    //                 comb += it->second;
    //             }
    //         }
    //         res[indexes[i]] = comb;
    //     }
    // }
    // for (int i = 0; i < N; ++i) {
    //     int ai = a[i];
    //     int numOfCombinations = 0;
    //     for (map<int, int>::const_iterator it = ballNum2IndexNum.begin(); it != ballNum2IndexNum.end(); ++it) {
    //         if (ai == it->first) {
    //             numOfCombinations += combination((it->second) - 1);
    //             // numOfCombinations += memo.at(it->second - 1);
    //         }
    //         else {
    //             numOfCombinations += combination(it->second);
    //             // numOfCombinations += memo.at(it->second);
    //         }
    //     }
    //     res.push_back(numOfCombinations);
    // }

    for (int i = 0; i < N; ++i) {
        cout << res[i] << endl;
    }
    return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:49:43: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   49 |         int indexNum = ballNum2IndexNum.at[ai];
      |                                           ^