#include #include using namespace std ; /*ifstream cin ("input") ; ofstream cout ("output") ;*/ class FenwickTree { public: FenwickTree(int n){ this -> n = n ; aib.resize (n + 1) ; } void Update (int pos, int value) { update (pos, value) ; } int Query (int a, int b) { return query (b) - query (a - 1); } private: int n ; vector aib ; int lsb (int x) { return x & -x ; } void update (int pos, int value) { for (; pos <= n; pos += lsb (pos)) { aib [pos] += value ; } } int query (int pos) { int s = 0 ; while (pos) { s += aib [pos] ; pos -= lsb (pos) ; } return s ; } }; int main(int argc, char const *argv[]) { int n ; cin >> n ; FenwickTree *F = new FenwickTree (2 * n) ; vector last (n + 1, 0) ; long long solution = 0 ; for (int i = 1 ; i <= 2 * n ; ++ i) { int x ; cin >> x ; /*cout << x << '\n' ;*/ if (last [x] == 0) { F -> Update (i, 1) ; last [x] = i; } else { solution += 1LL * F -> Query (last[x] + 1, i) ; //cout << "am adunat " << 1LL * F -> Query (last[x], i) << " pentru " << x << '\n' ; F -> Update (last[x], -1) ; } } cout << solution << '\n' ; return 0; }