This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_D"
#include "../../template.h"
#include "../count_inversions.h"
void solve() {
int n; cin >> n;
auto a = read_vector<int> (n);
cout << count_inversions(a) << endl;
}
#line 1 "DP/tests/aizu_alds1_5_d_count_inversions.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_D"
#line 1 "template.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
// For printing pair, container, etc.
// Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html
template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template<class Con, class = decltype(begin(declval<Con>()))>
typename enable_if<!is_same<Con, string>::value, ostream&>::type
operator << (ostream& out, const Con& con) {
out << '{';
for (auto beg = con.begin(), it = beg; it != con.end(); it++) {
out << (it == beg ? "" : ", ") << *it;
}
return out << '}';
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
if constexpr(i == tuple_size<T>::value) return out << ")";
else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup);
}
template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) {
return print_tuple_utils<0, tuple<U...>>(out, t);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long get_rand(long long r) {
return uniform_int_distribution<long long> (0, r-1)(rng);
}
template<typename T>
vector<T> read_vector(int n) {
vector<T> res(n);
for (int& x : res) cin >> x;
return res;
}
void solve();
int main() {
ios::sync_with_stdio(0); cin.tie(0);
solve();
return 0;
}
#line 1 "Misc/compress.h"
// Compressor {{{
/* Example usage:
auto compressor = CompressorBuilder<T>{vs}.build();
int x = compessor.must_eq(vs[0]);
compressor.compress_inplace(vs);
*/
// Based on https://suisen-cp.github.io/cp-library-cpp/library/util/coordinate_compressor.hpp
template<typename T>
struct CompressorBuilder {
// Do not use directly. Use builder.build()
struct Compressor {
// Number of unique keys
int size() const { return xs.size(); }
void compress_inplace(std::vector<T>& vals) {
for (int& val : vals) {
val = must_eq(val);
}
}
[[nodiscard]] std::vector<T> compress(const std::vector<T>& vals) {
std::vector<T> res(vals.size());
for (int i = 0; i < static_cast<int> (res.size()); ++i) {
res[i] = must_eq(vals[i]);
}
return res;
}
bool has_key(const T& key) const {
return std::binary_search(xs.begin(), xs.end(), key);
}
#define LB(key) std::lower_bound(xs.begin(), xs.end(), key)
#define UB(key) std::upper_bound(xs.begin(), xs.end(), key)
std::optional<int> eq(const T& key) {
auto it = LB(key);
return it == xs.end() ? std::nullopt : std::optional<int>{it - xs.begin()};
}
std::optional<int> geq(const T& key) {
auto it = LB(key);
return it == xs.end() ? std::nullopt : std::optional<int>{it - xs.begin()};
}
std::optional<int> gt(const T& key) {
auto it = UB(key);
return it == xs.end() ? std::nullopt : std::optional<int>{it - xs.begin()};
}
std::optional<int> leq(const T& key) {
auto it = UB(key);
return it == xs.begin() ? std::nullopt : std::optional<int>{it - xs.begin() - 1};
}
std::optional<int> lt(const T& key) {
auto it = LB(key);
return it == xs.begin() ? std::nullopt : std::optional<int>{it - xs.begin() - 1};
}
// throw exception if no such key is found
int must_eq(const T& key) {
auto it = LB(key);
assert(it != xs.end());
return it - xs.begin();
}
// throw exception if no such key is found
int must_geq(const T& key) {
auto it = LB(key);
assert(it != xs.end());
return it - xs.begin();
}
// throw exception if no such key is found
int must_gt(const T& key) {
auto it = UB(key);
assert(it != xs.end());
return it - xs.begin();
}
// throw exception if no such key is found
int must_leq(const T& key) {
auto it = UB(key);
assert(it != xs.begin());
return it - xs.begin() - 1;
}
// throw exception if no such key is found
int must_lt(const T& key) {
auto it = LB(key);
assert(it != xs.begin());
return it - xs.begin() - 1;
}
#undef LB
#undef UB
std::vector<T> xs;
};
auto build() {
std::sort(xs.begin(), xs.end());
xs.erase(std::unique(xs.begin(), xs.end()), xs.end());
return Compressor{xs};
}
void add(const T& key) { xs.push_back(key); }
void add(T&& key) { xs.push_back(std::move(key)); }
std::vector<T> xs;
};
// }}}
#line 1 "DataStructure/Fenwick/Fenwick.h"
// 1D Fenwick {{{
// 0 based index
//
// Tested:
// - https://judge.yosupo.jp/problem/static_range_sum
// - https://judge.yosupo.jp/problem/point_add_range_sum
template<
typename T // need to support operators + -
> struct Fenwick {
Fenwick(int _n) : n(_n), f(_n + 1) {}
// a[u] += val
void update(int u, T val) {
assert(0 <= u && u < n);
++u;
for (; u <= n; u += u & -u) {
f[u] += val;
}
}
// return a[0] + .. + a[u-1]
T get(int u) const {
assert(0 <= u && u <= n);
T res = 0;
for (; u > 0; u -= u & -u) {
res += f[u];
}
return res;
}
// return a[l] + .. + a[r-1]
T get(int l, int r) const {
assert(0 <= l && l <= r && r <= n);
if (l == r) return 0; // empty
return get(r) - get(l);
}
void reset() {
std::fill(f.begin(), f.end(), T(0));
}
int n;
vector<T> f;
};
// }}}
#line 3 "DP/count_inversions.h"
// Given vector vs, return number of inversions
template<typename T>
long long count_inversions(vector<T> vs) {
int n = vs.size();
auto compressor = CompressorBuilder<T>{vs}.build();
compressor.compress_inplace(vs);
Fenwick<int> bit(n);
long long res = 0;
for (auto v : vs) {
res += bit.get(v+1, n);
bit.update(v, +1);
}
return res;
}
#line 5 "DP/tests/aizu_alds1_5_d_count_inversions.test.cpp"
void solve() {
int n; cin >> n;
auto a = read_vector<int> (n);
cout << count_inversions(a) << endl;
}