Problem:
The Number of Inversions
For a given sequence $A = \{a_0, a_1, ... a_{n-1}\}$, the number of pairs $(i, j)$ where $a_i > a_j$ and $i < j$, is called the number of inversions. The number of inversions is equal to the number of swaps of Bubble Sort defined in the following program:
bubbleSort(A)
  cnt = 0 // the number of inversions
  for i = 0 to A.length-1
    for j = A.length-1 downto i+1
      if A[j] < A[j-1]
	swap(A[j], A[j-1])
	cnt++

  return cnt
For the given sequence $A$, print the number of inversions of $A$. Note that you should not use the above program, which brings Time Limit Exceeded.
Input
In the first line, an integer $n$, the number of elements in $A$, is given. In the second line, the elements $a_i$ ($i = 0, 1, .. n-1$) are given separated by space characters.
output
Print the number of inversions in a line.
Constraints
$ 1 \leq n \leq 200,000$
$ 0 \leq a_i \leq 10^9$
$a_i$ are all different
Sample Input 1
5
3 5 2 1 4
Sample Output 1
6
Sample Input 2
3
3 1 2
Sample Output 2
2

Submitted solutions:

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

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vstr = vector<string>;
using vchar = vector<char>;
using vvchar = vector<vector<char>>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vvvb = vector<vector<vector<bool>>>;
using pii = pair<int,int>;
using pll = pair<long long, long long>;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<pair<int,ll>>>;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};

#define rep(i, a, b) for(ll i = (ll)a; i < (ll)b; i++)
#define REP(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define rrep(i, a, b) for(ll i = (ll)b-1; i >= (ll)a; i--)
#define RREP(i, a, b) for(ll i = (ll)b; i >= (ll)a; i--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YESNO(flag) cout << (flag ? "Yes" : "No") << "\n"
#define spa " "
#define mint modint<MOD>

const int inf = 1070000000;
const long long INF = 4500000000000000000;
//const long long MOD = 998244353;
const long long MOD = 1000000007;
const double pi = 3.141592653589793238;
const double eps = (1e-10);
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";

template<typename T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template<typename S, typename T> pair<S, T> operator+(pair<S, T> a, pair<S, T> b) {
    pair<S, T> ans;
    ans.first = a.first + b.first;
    ans.second = a.second + b.second;
    return ans;
} 
template<typename T> vector<T> operator+(vector<T> a, vector<T> b) {
    int N = max(a.size(), b.size());
    vector<T> c(N);
    for(int i = 0; i < N; i++) {
        if(i >= a.size()) c[i] = b[i];
        else if(i >= b.size()) c[i] = a[i];
        else c[i] = a[i] + b[i];
    }
    return c;
}
template<typename T> vector<T> operator*(vector<T> a, vector<T> b) {
    int N = max(a.size(), b.size()), n = min(a.size(), b.size());
    vector<T> c(N, 0);
    for(int i = 0; i < n; i++) {
        c[i] = a[i] * b[i];
    }
    return c;
}

//拡張ユークリッド
ll ext_gcd(ll a, ll b, ll &x, ll &y) {
    if(b == 0) {
        x = 1, y = 0;
        return a;
    }

    ll q = a/b;
    ll g = ext_gcd(b, a-q*b, x, y);
    ll z = x-q*y;
    x = y, y = z;
    return g;
}
//mod mにおけるaの逆元. aとmは互いに素.
ll inverse(ll a, ll m) {
    ll x, y;
    ext_gcd(a, m, x, y);
    x = (x+m) % m;
    return x;
}
//modint
template<std::uint_fast64_t mod> struct modint {
    using u64 = std::uint_fast64_t;
    u64 val;

    modint(u64 v = 0) : val(v % mod) {}

    bool operator==(modint other) {
        return val == other.val;
    }
    bool operator<(const modint& other) const {
        return val < other.val;
    }
    // operator> (大なり)
    bool operator>(const modint& other) const {
        return val > other.val;
    }
    modint operator+(modint other) {
        modint<mod> ans;
        ans.val = val + other.val;
        if(ans.val >= mod) ans.val -= mod;
        return ans;
    }
    modint operator-(modint other) {
        modint<mod> ans;
        if(val < other.val) val += mod;
        ans.val = val - other.val;
        return ans;
    }
    modint operator*(modint other) {
        modint<mod> ans;
        ans.val = (val * other.val) % mod;
        return ans;
    }
    modint operator/(modint other) {
        modint<mod> ans;
        ans.val = (val * inverse(other.val, mod)) % mod;
        return ans;
    }
    modint &operator+=(modint other) {
        *this = *this + other;
        return *this;
    }
    modint &operator-=(modint other) {
        *this = *this - other;
        return *this;
    }
    modint &operator*=(modint other) {
        *this = *this * other;
        return *this;
    }
    modint &operator/=(modint other) {
        *this = *this / other;
        return *this;
    }
    modint &operator++() {
        *this += 1;
        return *this;
    }
    modint &operator++(int) {
        ++(*this);
        return *this;
    }
    modint &operator--() {
        *this -= 1;
        return *this;
    }
    modint &operator--(int) {
        --(*this);
        return *this;
    }
};
//modintの入出力
template<std::uint_fast64_t mod> std::ostream& operator<<(std::ostream &os, const modint<mod> &x) {
    // modint の 'val' メンバを出力ストリーム(os)に渡す
    os << x.val;
    return os; // ストリームを返す (チェーンのため)
}
template<std::uint_fast64_t mod> std::istream& operator>>(std::istream& is, modint<mod> &x) {
    // modintが内部で使っているu64型を取得
    using u64 = typename modint<mod>::u64; 
    
    u64 input_value;
    is >> input_value; // (1) istream(cin) から一時変数に値を読み込む
    
    // (2) 読み込んだ値を使って modint を再構築する
    //     modintのコンストラクタが (input_value % mod) を処理してくれる
    x = modint<mod>(input_value); 
    
    return is; // (3) ストリームを返す (チェーンのため)
}
//累乗, x^n
template<typename T> T pow(T x, int n) {
    T ans = 1;
    while(n > 0) {
        if(n & 1) ans *= x;
        x *= x;
        n >>= 1; 
    }
    return ans;
}
//階乗, n!
template<typename T> T fact(T n) {
    if(n == 0) return 1;
    return n * fact(n-1);
}
//nPk
template<typename T> T perm(T n, T k) {
    if(k == 0) return 1;
    return n * perm(n-1, k-1);
}
//nCk
template<typename T> T comb(T n, T k) {
    if(k > n) return 0;
    return perm(n, min(k, n-k))/fact(min(k, n-k));
}
//ord(n)
ll ord(ll n, ll p) {
    int ans = 0;
    while(n % p == 0) {
        n /= p;
        ans++;
    }
    return ans;
}
//ルジャンドルの公式ord(n!)
ll factord(ll n, ll p) {
    ll ans = 0;
    ll q = p, tmp = n;
    while(tmp > 0) {
        tmp = n/q;
        ans += tmp;
        q *= p;
    }
    return ans;
}
//素因数分解, O(√N)
vector<pair<ll,ll>> factorize(ll N) {
    vector<pair<ll,ll>> ans;
    for(ll p = 2; p*p <= N; p++) {
        int ex = 0;
        while(N % p == 0) {
            N /= p;
            ex++;
        }
        if(ex != 0) ans.push_back({p,ex});
    }
    if(N != 1) ans.push_back({N,1});
    return ans;
}
//素数判定
bool isprime(long long x) {
    if(x == 1) return false;
    
    for(long long i = 2; i*i <= x; i++) {
        if(x % i == 0) return false;
    }
    return true;
}
//エラトステネスの篩
struct Eratosthenes {
    vector<bool> prime;
    vector<int> minfactor;
    vector<int> mobius;
    vector<ll> euler;

    //コンストラクタ O(NloglogN)
    Eratosthenes(int N) : prime(N+1, true),
                          minfactor(N+1, -1),
                          mobius(N+1, 1),
                          euler(N+1, 1) {
        prime[0] = false;
        prime[1] = false;
        minfactor[1] = 1;
        REP(i,1,N) euler[i] = i;

        for(int p = 2; p <= N; p++) {
            if(!prime[p]) continue;

            minfactor[p] = p;
            mobius[p] = -1;
            euler[p] = p-1;

            for(int q = 2; p*q <= N; q++) {
                prime[p*q] = false;
                if(minfactor[p*q] == -1) minfactor[p*q] = p;
                if(q % p == 0) mobius[p*q] = 0;
                else mobius[p*q] *= -1;
                euler[p*q] = euler[p*q]*(p-1)/p;
            }
        }
    }

    //素因数分解 O(logn)
    vector<pair<int,int>> factorize(int n) {
        vector<pair<int,int>> ans;
        while(n > 1) {
            int p = minfactor[n], exp = 0;
            while(n % p == 0) {
                n /= p;
                exp++;
            }
            ans.push_back({p, exp});
        }
        return ans;
    }
    //約数列挙 O(σ(n))
    vector<int> divisors(int n) {
        vector<int> ans = {1};
        while(n > 1) {
            int p = minfactor[n], d = 1, s = ans.size();
            while(n % p == 0) {
                d *= p;
                for(int i = 0; i < s; i++) {
                    ans.push_back(ans[i]*d);
                }
                n /= p;
            }
        }
        return ans;
    }
    //素数関数 O(n)
    int count(int n) {
        int ans = 0;
        REP(i,2,n) if(prime[i]) ans++;
        return ans;
    }
};
//高速ゼータ変換
template<typename T> void fast_zeta(vector<T> &f) {
    int N = f.size()-1;
    Eratosthenes sieve(N);
    for(int p = 2; p < N; p++) {
        if(!sieve.prime[p]) continue;
        
        for(int k = N/p; k >= 1; k--) {
            f[k] += f[k*p];
        }
    }
}
//高速メビウス変換
template<typename T> void fast_mobius(vector<T> &F) {
    int N = F.size()-1;
    Eratosthenes sieve(N);
    for(int p = 2; p < N; p++) {
        if(!sieve.prime[p]) continue;

        for(int k = 1; k*p <= N; k++) {
            F[k] -= F[k*p];
        }
    }
}
//添字GCD畳み込み
template<typename T> vector<T> gcd_conv(vector<T> &f, vector<T> &g) {
    int N = max(f.size(), g.size());
    vector<T> F(N, 0), G(N, 0), H(N);
    for(int i = 0; i < f.size(); i++) F[i] = f[i];
    for(int i = 0; i < g.size(); i++) G[i] = g[i];
    fast_zeta(F);
    fast_zeta(G);
    
    for(int i = 0; i < N; i++) H[i] = F[i]*G[i];
    fast_mobius(H);
    
    return H;
}

//行列積
template<typename T> vector<vector<T>> matrix_product(vector<vector<T>> A, vector<vector<T>> B) {
    int H = A.size(), W = A[0].size(), P = B.size(), Q = B[0].size();
    if(W != P) {
        cout << "matrix_size_error" << "\n";
        return vector<vector<T>>(0);
    }

    vector<vector<T>> C(H, vector<T>(Q, 0));
    rep(i,0,H) rep(j,0,Q) {
        rep(k,0,W) {
            C[i][j] += A[i][k]*B[k][j];
        }
    }
    return C;
}
//行列累乗
template<typename T> vector<vector<T>> matrix_pow(vector<vector<T>> A, ll n) {
    int N = A.size();
    vector<vector<T>> ans(N, vector<T>(N, 0));
    rep(i,0,N) ans[i][i] = 1;
    
    while(n > 0) {
        if(n & 1) ans = matrix_product(ans, A);
        A = matrix_product(A, A);
        n >>= 1; 
    }
    return ans;
}

//1+2+...+n = n(n+1)/2
ll tri(ll n) {
    ll x = n*(n+1);
    return x/2;
}

//座標圧縮
vector<int> compress(vector<int> A) {
    vector<int> B = A;

    sort(B.begin(), B.end());
    B.erase(unique(B.begin(), B.end()), B.end());

    vector<int> res(A.size());
    for(int i = 0; i < (int)A.size(); i++) {
        res[i] = lower_bound(B.begin(), B.end(), A[i]) - B.begin();
    }
    return res;
}

//正方形グリッドの右回転, 左回転
void rotate_right(vector<vector<char>> &S) {
    int N = S.size();
    auto T = S;
    rep(i,0,N) rep(j,0,N) {
        T[i][j] = S[N-1-j][i];
    }
    S = T;
}
void rotate_left(vector<vector<char>> &S) {
    int N = S.size();
    auto T = S;
    rep(i,0,N) rep(j,0,N) {
        T[i][j] = S[j][N-1-i];
    }
    S = T;
}

//2点(a,b)と(x,y)の距離
long double dist(pair<long double, long double> a, pair<long double, long double> b) {
    return sqrt((a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second));
}
long double man(pair<long double, long double> a, pair<long double, long double> b) {
    return abs(a.first-b.first) + abs(a.second-b.second);
}
//内分点
pair<long double, long double> internal_division(pair<long double, long double> a, pair<long double, long double> b, long double p) {
    long double x = a.first + (b.first - a.first) * p;
    long double y = a.second + (b.second - a.second) * p;
    return {x, y};
}

//桁数
int digit(ll N) {
    if(N <= 9) return 1;

    return 1 + digit(N / 10);
}
//A進数文字列NをB進数に変換. A,Bは10以下
string base_change(string N, ll A, ll B) {
    ll X = stoll(N), Y = 0;

    ll i = 1;
    while(X != 0) {
        Y += (X % 10) * i;
        i *= A;
        X /= 10;
    }

    X = Y, Y = 0;
    i = 1;
    while(X != 0) {
        Y += (X % B) * i;
        i *= 10;
        X /= B;
    }

    return to_string(Y);
}
//回文判定
bool palin(string S) {
    rep(i,0,S.size()) {
        if(S[i] != S[S.size()-1-i]) return false;
    }
    return true;
}

//hh:mm:ss to second
int convert_to_time(string S) {
    ll h = 10*(S[0]-'0') + (S[1]-'0');
    ll m = 10*(S[3]-'0') + (S[4]-'0');
    ll s = 10*(S[6]-'0') + (S[7]-'0');
    
    return h*3600+m*60+s;
}

//DFS
void dfs(const Graph &G, int v, vector<bool> &seen) { 
    seen[v] = true;
    
    for (auto next_v : G[v]) { 
        if (!seen[next_v]) {
            dfs(G, next_v, seen);
        }
    }
}
void griddfs(const vector<vector<char>> &G, int x, int y, vector<vector<bool>> &seen) {
    int H = G.size(), W = G[0].size();
    seen[x][y] = true;
    
    for (int i = 0; i < 8; i++) { 
        int nx = x + dx[i], ny = y + dy[i];
        if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;
        if (!seen[nx][ny] and G[nx][ny] != '#') griddfs(G, nx, ny, seen);
    }
}
//BFS
void bfs(const Graph &G, int v, vector<int> &dist) {
    rep(i,0,dist.size()) dist[i] = inf;
    dist[v] = 0;
    queue<int> q;
    q.push(v);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(auto next_u : G[u]) {
            if(dist[next_u] == inf) {
                dist[next_u] = dist[u] + 1;
                q.push(next_u);
            }
        }
    }
}
void gridbfs(const vector<vector<char>> &G, int x, int y, vector<vector<int>> &dist) {
    int H = G.size(), W = G[0].size();
    dist[x][y] = 0;
    queue<pair<int, int>> q;
    q.push(make_pair(x, y));
    while(!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        for(int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;
            if(dist[nx][ny] == inf and G[nx][ny] != '#') {
                dist[nx][ny] = dist[x][y] + 1;
                q.push(make_pair(nx, ny));
            }
        }
    }
}
//ワーシャル・フロイド
vector<vector<ll>> floyd(vector<vector<ll>> G) {
    int N = G.size();
    auto dp = G;
    for (int k = 0; k < N; k++){
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if(dp[i][j] > dp[i][k] + dp[k][j] and dp[i][k] != INF and dp[k][j] != INF) {
                    dp[i][j] = dp[i][k] + dp[k][j];
                }
            }
        }
    }
    return dp;
}
//サイクルがあるか判定
bool cycle(const Graph &G, int v, vector<bool> &seen,  vector<bool> &finished) {
    seen[v] = true;    // 行きがけ時に true になる
    for (auto v2 : G[v]) {
        //if (v2 が逆戻りの場合) continue;  // 無向グラフの場合は必要

        // 頂点 v2 がすでに探索済みの場合はスキップ 
        if (finished[v2]) continue;

        // サイクル検出
        if (seen[v2] && !finished[v2]) return true;

        // 頂点 v2 を再帰的に探索
        if (cycle(G, v2, seen, finished)) return true;
    }
    finished[v] = true;  // 帰りがけ時に true になる
    return false;
}

//Union-Find
struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
    vector<ll> cnt;
    
    UnionFind(int N) : par(N),
                       cnt(N, 1) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++) par[i] = i;
    }
    
    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }
    
    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
        cnt[ry] += cnt[rx];
    }
    
    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

    ll num(int x) { //データxと同じ木に属するデータの数を得る
        return cnt[x] = cnt[root(x)];
    }
};

//最小全域木
ll Kruskal(const WeightedGraph &G) {
    return 0;
}

//BIT
struct BIT {
    vector<long long> array;
    const int n;

    BIT(int _n) : array(_n, 0), n(_n) {}

    //a_0 + ... + a_i
    long long sum(int i) {
        long long s = 0;
        while(i >= 0) {
            s += array[i];
            i -= (i+1) & -(i+1);
        }
        return s;
    }
    //a_i + ... + a_j
    long long sum(int i, int j) {
        long long ans_i = sum(i-1);
        long long ans_j = sum(j);
        return ans_j - ans_i;
    }
    //a_i += x
    void add(int i, long long x) {
        while(i < n) {
            array[i] += x;
            i += (i+1) & -(i+1);
        }
    }
};
//セグ木
struct SegTree {
    int n, sz;
    vector<long long> node;

    SegTree(vector<long long> a) {
        sz = a.size();

        n = 1;
        while(n < sz) n *= 2;
        node.resize(2*n-1);
        for(int i = 0; i < sz; i++) node[n-1+i] = a[i];
        for(int i = n-2; i >= 0; i--) node[i] = max(node[2*i+1], node[2*i+2]);
    }
    //[a,b)における最大値
    long long getmax(int a, int b, int k = 0, int l = 0, int r = -1) {
        if(r < 0) r = n;
        if(r <= a or b <= l) return -INF;
        if(a <= l and r <= b) return node[k];

        long long vl = getmax(a, b, 2*k+1, l, (l+r)/2);
        long long vr = getmax(a, b, 2*k+2, (l+r)/2, r);
        return max(vl, vr);
    }
    void update(int i, long long x) {
        i = n-1+i;
        node[i] = x;
        while(i > 0) {
            i = (i-1)/2;
            node[i] = max(node[2*i+1], node[2*i+2]);
        }
    }
    //[a,b)でa_i >= xなる最小のiを求める
    int getminindex(int a, int b, long long x, int l = 0, int r = -1) {
        return 0;
    }
};
//遅延セグ木
struct LazySegTree {
    int n;
    vector<ll> node, lazy;

    LazySegTree(vector<ll> a) {
        int sz = a.size();
        
        n = 1;
        while(n < sz) n *= 2;
        node.resize(2*n-1);
        lazy.resize(2*n-1, 0);

        for(int i = 0; i < sz; i++) node[n-1+i] = a[i];
        for(int i = n-2; i >= 0; i--) node[i] = node[2*i+1] + node[2*i+2];
    }

    void eval(int k, int l, int r) {
        if(lazy[k] != 0) {
            node[k] += lazy[k];

            if(r - l > 1) {
                lazy[2*k+1] += lazy[k]/2;
                lazy[2*k+2] += lazy[k]/2;
            }

            lazy[k] = 0;
        }
    }
    //区間[a,b)に値xを加算
    void add(int a, int b, long long x, int k = 0, int l = 0, int r = -1) {
        if(r < 0) r = n;
        
        eval(k, l, r);

        if(r <= a or b <= l) return;
        if(a <= l and r <= b) {
            lazy[k] += (r-l)*x;
            eval(k, l, r);
        }else {
            add(a, b, x, 2*k+1, l, (l+r)/2);
            add(a, b, x, 2*k+2, (l+r)/2, r);
            node[k] = node[2*k+1] + node[2*k+2];
        }
    }
    //区間[a,b)の和を取得
    ll getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
        if(r < 0) r = n;
        if(r <= a or b <= l) return 0;

        eval(k, l, r);
        if(a <= l and r <= b) return node[k];
        ll vl = getsum(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getsum(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }
};
//平方分割
struct Backet {
    vector<ll> array;
    vector<ll> backet;
    int N, M;

    Backet(vector<ll> a) {
        array = a;
        N = a.size(), M = ceil(sqrt(N));
        
        backet.resize((N+M-1)/M);
        for(int i = 0; i < N; i++) {
            backet[i/M] += a[i];
        }
    }
    //a_i + ... + a_j
    void add(int i, ll x) {
        array[i] += x;
        backet[i/M] += x;
    }
    ll getsum(int i, int j) {
        int l = i/M + 1, r = j/M;
        ll s = 0;
        if(l > r) {
            REP(k,i,j) s += array[k];
        }else {
            rep(k,i,l*M) s += array[k];
            rep(k,l,r) s += backet[k];
            REP(k,r*M,j) s += array[k];
        }
        return s;
    }
};

void solve() {
}

int main() {
    int N; cin >> N;
    vi A(N);
    rep(i,0,N) {
        cin >> A[i];
    }
    A = compress(A);
    ll ans = 0;
    BIT tree(N);
    rep(i,0,N) {
        ans += i - tree.sum(A[i]);
        tree.add(A[i], 1);
    }
    cout << ans << "\n";

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 6292
final_rank: 11

code_snippet: #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using ll = long long;
using ld = long double;
ll INF = 2e18;
#define PI 3.14159265358979323846264338327950l
// namespace multip = boost::multiprecision;
// using lll = multip::int128_t;
using P = pair<ll, ll>;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
using vl = vc<ll>; using vvl = vv<ll>; using vvvl = vv<vl>; using vvvvl = vv<vvl>;
using vs = vc<string>; using vvs = vv<string>;
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define drep(i,n) for (ll i = (n)-1; i >= 0; i--)
#define nfor(i, s, n) for (ll i = (s); i < (n); i++)
#define dfor(i, s, n) for (ll i = (s)-1; i>=n; i--)
#define nall(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define em emplace
#define pob pop_back
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define YN { cout << "Yes" << endl; } else { cout << "No" << endl; }
#define dame cout << -1 << endl
#define minval(vec) *min_element(nall(vec))
#define maxval(vec) *max_element(nall(vec))
#define binaryS(vec, x) *lower_bound(nall(vec), x)
#define yu_qurid(x, y) (x) * (x) + (y) * (y)
#define mannhattan(x1, x2, y1, y2) abs(x1-x2)+abs(y1-y2)
#define vc_cout(v){ll n= size(v); rep(i,n)cout<<v[i]<<" ";cout << endl;}
#define vvc_cout(v){ll n= size(v);rep(i,n){rep(j,size(v[i])){cout<<v[i][j]<<" ";}cout<<endl;}}
// 少数の出力
void print(ld x) { printf("%.20Lf\n", x); }

// グリット用
vector<ll> dx = {1, 0, -1, 0};
vector<ll> dy = {0, 1, 0, -1};
bool in_grid(ll i, ll j, ll h, ll w) {
    return 0 <= i and i < h and 0 <= j and j < w;
}

// ALDS1_5_Bよりマージソート
ll merge(vector<ll> &A, ll left, ll mid, ll right) {
    ll n1 = mid - left, n2 = right - mid;
    vector<ll> L(n1+1);
    vector<ll> R(n2+1);
    for(int i = 0; i < n1; i++) {
        L[i] = A[left + i];
    }
    for(int i = 0; i < n2; i++) {
        R[i] = A[mid + i];
    }
    L[n1] = INF;
    R[n2] = INF;
    ll i = 0, j = 0, cnt=0;
    for(int k = left; k < right; k++) {
        if (L[i] <= R[j]) {
            A[k] = L[i];
            i++;
        }
        else {
            A[k] = R[j];
            j++;
            cnt += n1 - i;
        }
    }
    return cnt;
}
ll mergesort(vector<ll> &A, ll left, ll right) {
    ll v1=0, v2=0, v3=0;
    if (left + 1 < right) {
        ll mid = (left + right) / 2;
        v1 = mergesort(A, left, mid);
        v2 = mergesort(A, mid, right);
        v3 = merge(A, left, mid, right);
    }
    return v1 + v2 + v3;
}

// main
void solve() {
    ll n;
    cin >> n;
    vector<ll> A(n);
    nfor(i, 0, n) {
        cin >> A[i];
    }
    ll ans = mergesort(A, 0, n);
    cout << ans << endl;
    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 6636
final_rank: 9

code_snippet: #include <iostream>
using namespace std;

#define MAX 200000
#define SENTINEL 2000000000
typedef long long llong;

int L[MAX / 2 + 2], R[MAX / 2 + 2];

llong merge(int A[], int n, int left, int mid, int right) {
    int i, j, k;
    llong cnt = 0;
    int n1 = mid - left;
    int n2 = right - mid;
    for ( i = 0; i < n1; i++ ) L[i] = A[left + i];
    for ( i = 0; i < n2; i++ ) R[i] = A[mid + i];
    L[n1] = R[n2] = SENTINEL;
    i = j = 0;
    for ( k = left; k < right; k++ ) {
        if ( L[i] <= R[j] ) {
            A[k] = L[i++];
        } else {
            A[k] = R[j++];
            cnt += n1 - i;
        }
    }
    return cnt;
}

llong mergeSort(int A[], int n, int left, int right) {
    int mid;
    llong v1, v2, v3;
    if ( left + 1 < right ) {
        mid = (left + right) / 2;
        v1 = mergeSort(A, n, left, mid);
        v2 = mergeSort(A, n, mid, right);
        v3 = merge(A, n, left, mid, right);
        return v1 + v2 + v3;
    } else return 0;
}

int main() {
    int A[MAX], n, i;

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

    llong ans = mergeSort(A, n, 0, n);
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4972
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;

int main(){
    ll n; cin>>n;
    vector<ll> a(n);
    for(ll& a1:a) cin>>a1;

    vector<ll> sorted=a;
    sort(sorted.begin(),sorted.end());
    for(ll& a1:a) a1=lower_bound(sorted.begin(),sorted.end(),a1)-sorted.begin()+1;

    vector<ll> A(n+1,0);
    ll ans=0;
    for(int i=0;i<n;i++){
        ll idx=a[i];
        while(idx<=n){
            A[idx]+=1;
            idx+=idx&-idx;
        }

        ll t=a[i]-1;
        ll sum=0;
        while(t>0){
            sum+=A[t];
            t-=t&-t;
        }
        ans+=a[i]-sum-1;
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 70
memory_kb: 7816
final_rank: 12

code_snippet: #include <cstdio>
#include <cstdint>
#include <cinttypes>

#define N (300000)

static int v[2][N];

static constexpr bool isR(int lc, int rc, int lv, int rv){
    if(lc <= 0) return true;
    if(rc <= 0) return false;
    if(lv <= rv) return false;
    return true;
}

int main(){
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d", &v[0][i]);
    int w = 1, w2 = w << 1, k = 0;
    int lc, rc;
    int *out;
    uint64_t scnt = 0;
    while(w < n){
        int *lp = v[k], *rp = v[k] + w;
        k = (k == 1) ? 0 : 1;
        out = v[k];
        int i = w2;
        while(i < n){
            lc = w;
            rc = w;
            for(int j = w2; j > 0; j--){
                if(isR(lc, rc, *lp, *rp)){
                    *out = *rp;
                    rp++;
                    rc--;
                    scnt += lc;
                }
                else {
                    *out = *lp;
                    lp++;
                    lc--;
                }
                out++;
            }
            lp += w;
            rp += w;
            i += w2;
        }
        if(i - w > n){
            lc = w2 - (i - n); 
            rc = 0;
        }
        else {
            lc = w;
            rc = w - (i - n);
        }
        for(int j = n - (i - w2); j > 0; j--){
            if(isR(lc, rc, *lp, *rp)){
                *out = *rp;
                rp++;
                rc--;
                scnt += lc;
            }
            else {
                *out = *lp;
                lp++;
                lc--;
            }
            out++;
        }
        w <<= 1;
        w2 <<= 1;
    }
    printf("%" PRIu64 "\n", scnt);
}
accuracy: 1
time_ms: 20
memory_kb: 4488
final_rank: 3

code_snippet: #include <cstdio>
#include <cstdint>
#include <cinttypes>

#define N (300000)

static int v[2][N];

static constexpr bool isR(int lc, int rc, int lv, int rv){
    if(lc <= 0) return true;
    if(rc <= 0) return false;
    if(lv <= rv) return false;
    return true;
}

int main(){
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d", &v[0][i]);
    int w = 1, w2 = w << 1, k = 0;
    int lc, rc;
    int *out;
    uint64_t scnt = 0;
    while(w < n){
        int *lp = v[k], *rp = v[k] + w;
        k = (k == 1) ? 0 : 1;
        out = v[k];
        int i = w2;
        while(i < n){
            lc = w;
            rc = w;
            for(int j = w2; j > 0; j--){
                if(isR(lc, rc, *lp, *rp)){
                    *out = *rp;
                    rp++;
                    rc--;
                    scnt += lc;
                }
                else {
                    *out = *lp;
                    lp++;
                    lc--;
                }
                out++;
            }
            lp += w;
            rp += w;
            i += w2;
        }
        if(i - w > n){
            lc = w2 - (i - n); 
            rc = 0;
        }
        else {
            lc = w;
            rc = w - (i - n);
        }
        //printf("A: %d, %d\n", i - w2, n - (i - w2));
        for(int j = n - (i - w2); j > 0; j--){
            //printf("A: %d, %d\n", lc, rc);
            if(isR(lc, rc, *lp, *rp)){
                *out = *rp;
                rp++;
                rc--;
                scnt += lc;
            }
            else {
                *out = *lp;
                lp++;
                lc--;
            }
            out++;
        }
        w <<= 1;
        w2 <<= 1;
        //printf("%d: ", w);
        //for(int i = 0; i < n; i++) printf("%d ", v[k][i]);
        //printf("\n");
    }
    printf("%" PRIu64 "\n", scnt);
}
accuracy: 1
time_ms: 20
memory_kb: 4468
final_rank: 1

code_snippet: #include <cstdio>
#include <cstdint>
#include <cinttypes>

#define N (200001)

static int v[2][N];

static constexpr bool isR(int lc, int rc, int lv, int rv){
    if(lc <= 0) return true;
    if(rc <= 0) return false;
    if(lv <= rv) return false;
    return true;
}

int main(){
    int n;
    scanf("%d", &n);
    uint64_t scnt = 0;
    for(int i = 0; i < n; i++) scanf("%d", &v[0][i]);
    int w = 1, w2 = w << 1, k = 0;
    int lc, rc;
    int *out;
    while(w < n){
        int *lp = v[k], *rp = v[k] + w;
        k = (k == 1) ? 0 : 1;
        out = v[k];
        int i = w2;
        while(i < n){
            lc = w;
            rc = w;
            for(int j = w2; j > 0; j--){
                if(isR(lc, rc, *lp, *rp)){
                    *out = *rp;
                    rp++;
                    rc--;
                    scnt += lc;
                }
                else {
                    *out = *lp;
                    lp++;
                    lc--;
                }
                out++;
            }
            lp += w;
            rp += w;
            i += w2;
        }
        if(i - w > n){
            lc = w2 - (i - n); 
            rc = 0;
        }
        else {
            lc = w;
            rc = w - (i - n);
        }
        for(int j = n - (i - w2); j > 0; j--){
            //printf("A: %d, %d\n", lc, rc);
            if(isR(lc, rc, *lp, *rp)){
                *out = *rp;
                rp++;
                rc--;
                scnt += lc;
            }
            else {
                *out = *lp;
                lp++;
                lc--;
            }
            out++;
        }
        w <<= 1;
        w2 <<= 1;
        //printf("%d: ", w);
        //for(int i = 0; i < n; i++) printf("%d ", v[k][i]);
        //printf("\n");
    }
    printf("%" PRIu64 "\n", scnt);
}
accuracy: 0.8333333333333334
time_ms: 10
memory_kb: 3764
final_rank: 17

code_snippet: #include <cstdio>
#include <cstdint>
#include <cinttypes>

#define N (210000)

static int v[2][N];

static constexpr bool isR(int lc, int rc, int lv, int rv){
    if(lc <= 0) return true;
    if(rc <= 0) return false;
    if(lv <= rv) return false;
    return true;
}

int main(){
    int n;
    scanf("%d", &n);
    uint64_t scnt = 0;
    for(int i = 0; i < n; i++) scanf("%d", &v[0][i]);
    int w = 1, w2 = w << 1, k = 0;
    int lc, rc;
    int *out;
    while(w < n){
        int *lp = v[k], *rp = v[k] + w;
        k = (k == 1) ? 0 : 1;
        out = v[k];
        int i = w2;
        while(i < n){
            lc = w;
            rc = w;
            for(int j = w2; j > 0; j--){
                if(isR(lc, rc, *lp, *rp)){
                    *out = *rp;
                    rp++;
                    rc--;
                    scnt += lc;
                }
                else {
                    *out = *lp;
                    lp++;
                    lc--;
                }
                out++;
            }
            lp += w;
            rp += w;
            i += w2;
        }
        if(i - w > n){
            lc = w2 - (i - n); 
            rc = 0;
        }
        else {
            lc = w;
            rc = w - (i - n);
        }
        for(int j = n - (i - w2); j > 0; j--){
            if(isR(lc, rc, *lp, *rp)){
                *out = *rp;
                rp++;
                rc--;
                scnt += lc;
            }
            else {
                *out = *lp;
                lp++;
                lc--;
            }
            out++;
        }
        w <<= 1;
        w2 <<= 1;
        //printf("%d: ", w);
        //for(int i = 0; i < n; i++) printf("%d ", v[k][i]);
        //printf("\n");
    }
    //printf("%" PRIu64 "\n", rcnt);
    printf("%" PRIu64 "\n", scnt);
}
accuracy: 0.8333333333333334
time_ms: 10
memory_kb: 3760
final_rank: 16

code_snippet: #include <cstdio>
#include <cstdint>
#include <cinttypes>

#define N (200000)

static int v[2][N];
static uint64_t rcnt = 0;

static constexpr bool isR(int lc, int rc, int lv, int rv){
    if(lc <= 0) return true;
    if(rc <= 0) return false;
    if(lv <= rv) return false;
    return true;
}

int main(){
    int n;
    scanf("%d", &n);
    uint64_t scnt = 0;
    for(int i = 0; i < n; i++) scanf("%d", &v[0][i]);
    int w = 1, w2 = w << 1, k = 0;
    int lc, rc;
    int *out;
    while(w < n){
        int *lp = v[k], *rp = v[k] + w;
        k = (k == 1) ? 0 : 1;
        out = v[k];
        int i = w2;
        while(i < n){
            lc = w;
            rc = w;
            for(int j = w2; j > 0; j--){
                if(isR(lc, rc, *lp, *rp)){
                    *out = *rp;
                    rp++;
                    rc--;
                    scnt += lc;
                }
                else {
                    *out = *lp;
                    lp++;
                    lc--;
                }
                out++;
            }
            lp += w;
            rp += w;
            i += w2;
        }
        if(i - w > n){
            lc = w2 - (i - n); 
            rc = 0;
        }
        else {
            lc = w;
            rc = w - (i - n);
        }
        for(int j = n - (i - w2); j > 0; j--){
            if(isR(lc, rc, *lp, *rp)){
                *out = *rp;
                rp++;
                rc--;
                scnt += lc;
            }
            else {
                *out = *lp;
                lp++;
                lc--;
            }
            out++;
        }
        w <<= 1;
        w2 <<= 1;
        //printf("%d: ", w);
        //for(int i = 0; i < n; i++) printf("%d ", v[k][i]);
        //printf("\n");
    }
    //printf("%" PRIu64 "\n", rcnt);
    printf("%" PRIu64 "\n", scnt);
}
accuracy: 0.8333333333333334
time_ms: 10
memory_kb: 3720
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std ;
#define pb push_back
#define ll long long 
#define ld long double
#define pii pair<int,int>
#define N 200100


ll mid(ll l , ll r){
    return (l+r)/2;
}

ll t[4*N] , arr[N];





void build(ll ind , ll l , ll r){
    if(l==r){
        t[ind] = 0;
        return;
    }
    ll m  = mid(l,r);
    build(2*ind,l,m);
    build(2*ind+1,m+1,r);
    t[ind] = 0;
    // cout<<"ind: "<<ind<<" l: "<<l<<" r: "<<r<<" ans: "<<t[ind].ans<<endl;
}

void update(ll ind , ll l , ll r , ll pos){
    if(pos<l or pos>r)return;
    if(l==r){
        t[ind]++;
        return;
    }
    ll m = mid(l,r);
    if(m>=pos)update(2*ind,l,m,pos);
    else update(2*ind+1,m+1,r,pos);

    t[ind] = t[2*ind] + t[2*ind+1];
}


ll sum(ll ind, ll l , ll r, ll ql , ll qr){
    if(r<ql or l>qr)return 0;
    if(l>=ql and r<=qr)return t[ind];
    ll m = mid(l,r);
    return sum(2*ind,l,m,ql,qr) + sum(2*ind+1,m+1,r,ql,qr);
}

void solve(){

   ll n;
   cin>>n;
   set<ll>s;
   map<ll,ll>mp;
   for(ll i=1;i<=n;i++){
    cin>>arr[i];
    s.insert(arr[i]);
   }
   ll ind = 0;
   for(auto x: s){
    mp[x] = ind+1;
    ind++;
   }

   for(ll i=1;i<=n;i++){
    arr[i] = mp[arr[i]];
    // cout<<arr[i]<<" ";
   }
   // cout<<endl;
   // cout<<ind<<endl;

   ll ans = 0;
   build(1,1,ind);

   for(ll i=1;i<=n;i++){
    ll val = arr[i];
    ans += (i-1) - sum(1,1,ind,1,val);
    update(1,1,ind,val);
   }

   cout<<ans<<endl;




}



int main(){

   ios::sync_with_stdio(false);
   cin.tie(nullptr);

    int t =1;
    // cin>>t;
    while(t--)solve();
}
accuracy: 1
time_ms: 190
memory_kb: 31400
final_rank: 14

code_snippet: #include <cstdio>

#define N (1<<(30-5))

int v[N], w[N];

int main(){
    int n, m;
    scanf("%d", &n);
    int cnt = 0;
    int a, b, max_a = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &m);
        a = m >> 5;
        b = m & 31;
        v[a] |= (1 << b);
        w[a] += 1;
        if(max_a < a) max_a = a;
        for(int j = max_a; j > a; j--) cnt += w[j];
        for(int j = 31; j > b; j--) cnt += (v[a] & (1 << j)) ? 1 : 0;
    }
    printf("%d\n",cnt);
}
accuracy: 0.6
time_ms: 50
memory_kb: 5032
final_rank: 20

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

long long mergeAndCount(vector<long long>& arr, int left, int mid, int right) {
    vector<long long> leftArr(arr.begin() + left, arr.begin() + mid + 1);
    vector<long long> rightArr(arr.begin() + mid + 1, arr.begin() + right + 1);

    int i = 0, j = 0, k = left;
    long long swaps = 0;

    while (i < leftArr.size() && j < rightArr.size()) {
        if (leftArr[i] <= rightArr[j]) {
            arr[k++] = leftArr[i++];
        } else {
            arr[k++] = rightArr[j++];
            swaps += (leftArr.size() - i); // all remaining left elements are greater
        }
    }

    while (i < leftArr.size()) arr[k++] = leftArr[i++];
    while (j < rightArr.size()) arr[k++] = rightArr[j++];

    return swaps;
}

long long mergeSortAndCount(vector<long long>& arr, int left, int right) {
    if (left >= right) return 0;

    int mid = left + (right - left) / 2;
    long long count = 0;

    count += mergeSortAndCount(arr, left, mid);
    count += mergeSortAndCount(arr, mid + 1, right);
    count += mergeAndCount(arr, left, mid, right);

    return count;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<long long> arr(n);
    for (int i = 0; i < n; i++) cin >> arr[i];

    cout << mergeSortAndCount(arr, 0, n - 1) << "\n";

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 6156
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<n; i++)

#define MAX 200000
#define SENTINEL 2000000000
using ll = long long;


int L[MAX / 2 + 2], R[MAX / 2 + 2];

ll merge(int A[], int n, int left, int mid, int right) {
    ll cnt = 0;
    int n1 = mid - left;
    int n2 = right - mid;

    rep(i,n1) L[i] = A[left+i];
    rep(i,n2) R[i] = A[mid+i];
    L[n1] = R[n2] = SENTINEL;
    int i = 0, j = 0;
    for (int k=left; k<right; k++) {
        if (L[i] <= R[j]) {
            A[k] = L[i++];
        } else {
            // 配列LとRでswapが起こったらcntをn1-iだけ増やす
            A[k] = R[j++];
            cnt += n1 - i;
        }
    }
    return cnt;
}

ll mergeSort(int A[], int n, int left, int right) {
    int mid;
    ll v1, v2, v3;
    if (left+1 < right) {
        mid = (left + right) / 2;
        v1 = mergeSort(A, n, left, mid);
        v2 = mergeSort(A, n, mid, right);
        v3 = merge(A, n, left, mid, right);
        return v1 + v2 + v3;
    } else return 0;
}

int main() {
    int A[MAX], n, i;

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

    ll ans = mergeSort(A,n,0,n);
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4924
final_rank: 5

code_snippet: #include <stdio.h>

#define MAX 200000
#define INFTY 2000000000

int L[MAX/2 + 2], R[MAX/2 + 2];

long long merge(int A[], int left, int mid, int right) {
    long long cnt = 0;
    int n1 = mid - left;
    int n2 = right - mid;

    for (int i = 0; i < n1; i++) L[i] = A[left + i];
    for (int i = 0; i < n2; i++) R[i] = A[mid + i];
    L[n1] = INFTY;
    R[n2] = INFTY;

    int i = 0, j = 0;
    for (int k = left; k < right; k++) {
        if (L[i] <= R[j]) {
            A[k] = L[i++];
        } else {
            A[k] = R[j++];
            // L[i] > R[j] の場合、L[i]以降の左側配列の要素は全てR[j]より大きい
            // よって、その個数(n1 - i)を反転数に加算する
            cnt += (n1 - i);
        }
    }
    return cnt;
}

long long mergeSort(int A[], int left, int right) {
    // 要素が1つ以下なら反転数は0
    if (left + 1 >= right) {
        return 0;
    }
    
    long long v1, v2, v3;
    int mid = (left + right) / 2;
    
    // 再帰的に左右の部分配列の反転数を計算
    v1 = mergeSort(A, left, mid);
    v2 = mergeSort(A, mid, right);
    
    // マージする際の、左右の部分配列間に存在する反転数を計算
    v3 = merge(A, left, mid, right);
    
    return v1 + v2 + v3;
}

int main() {
    int n;
    int A[MAX];

    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &A[i]);
    }
    
    long long ans = mergeSort(A, 0, n);
    
    printf("%lld\n", ans);

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4480
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
using namespace std;

// 反転数を数えるためのマージ処理
long long mergeAndCount(vector<int>& A, int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;
    vector<int> L(n1), R(n2);
    for (int i = 0; i < n1; ++i) L[i] = A[left + i];
    for (int i = 0; i < n2; ++i) R[i] = A[mid + i];

    long long inv = 0;
    int i = 0, j = 0;
    for (int k = left; k < right; ++k) {
        if (i < n1 && (j >= n2 || L[i] <= R[j])) {
            A[k] = L[i++];
        } else {
            A[k] = R[j++];
            inv += n1 - i;  // L[i] > R[j] のとき反転が発生
        }
    }
    return inv;
}

// 再帰的に反転数を数える
long long countInversions(vector<int>& A, int left, int right) {
    if (right - left <= 1) return 0;
    int mid = (left + right) / 2;
    long long inv = 0;
    inv += countInversions(A, left, mid);
    inv += countInversions(A, mid, right);
    inv += mergeAndCount(A, left, mid, right);
    return inv;
}

int main() {
    int n;
    cin >> n;
    vector<int> A(n);
    for (int i = 0; i < n; ++i) cin >> A[i];

    long long result = countInversions(A, 0, n);
    cout << result << endl;
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4652
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

ll mergeCount(vector<int>& A, int left, int right) {
    if (right - left <= 1) return 0;

    int mid = (left + right) / 2;
    ll count = 0;

    count += mergeCount(A, left, mid);
    count += mergeCount(A, mid, right);

    vector<int> sorted;
    int i = left, j = mid;

    while (i < mid && j < right) {
        if (A[i] <= A[j]) {
            sorted.push_back(A[i++]);
        } else {
            sorted.push_back(A[j++]);
            count += mid - i;  // i番目以降すべてに対して反転
        }
    }

    while (i < mid) sorted.push_back(A[i++]);
    while (j < right) sorted.push_back(A[j++]);

    for (int k = 0; k < sorted.size(); ++k) {
        A[left + k] = sorted[k];
    }

    return count;
}



int main() {
    int n;
    cin >> n;
    vector<int> A(n);
    for (int i = 0; i < n; ++i) cin >> A[i];

    cout << mergeCount(A, 0, n) << endl;

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 5444
final_rank: 10

code_snippet: // #ifndef ONLINE_JUDGE
// #define _GLIBCXX_DEBUG//[]で配列外参照をするとエラーにしてくれる。上下のやつがないとTLEになるので注意 ABC311Eのサンプル4みたいなデバック中のTLEは防げないので注意
// #endif

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;


template<typename T> using vc = vector<T>;//prioriy_queueに必要なのでここにこれ書いてます
template<typename T> using vv = vc<vc<T>>;

//-------------1.型系---------------
using ll = long long;
ll INF = 2e18;

// #include <boost/multiprecision/cpp_int.hpp>//インストール的なのをしてないとできないので注意
// namespace multip = boost::multiprecision;
// //using lll = multip::cpp_int;//無制限を使いたいときはこっちを使う
// using lll = multip::int128_t;

using ld = long double;
using bl = bool;
// using mint = modint998244353;
//using mint = modint1000000007;
//using mint = modint;//使うときはコメントアウトを外す
//mint::set_mod(m);//使うときはコメントアウトを外す

template<class T> using pq = priority_queue<T, vc<T>>;//大きい順
template<class T> using pq_g = priority_queue<T, vc<T>, greater<T>>;//小さい順
//-----------------------------------



//-------------2.配列系--------------
using vl = vc<ll>; using vvl = vv<ll>; using vvvl = vv<vl>; using vvvvl = vv<vvl>;
using vs = vc<string>; using vvs = vv<string>;
using vb = vc<bl>; using vvb = vv<bl>; using vvvb = vv<vb>;
using vld = vc<ld>; using vvld = vv<ld>; using vvvld = vv<vld>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
// using vmint = vc<mint>; using vvmint = vv<mint>; using vvvmint = vv<vmint>;

//配列外参照対策のやつは一番上の行にあります

#define rep(i,n) for(ll i = 0; i < (n); ++i)//↓でrepを使うので書いてます
template<class T>istream& operator>>(istream& i, vc<T>& v) { rep(j, size(v))i >> v[j]; return i; }

using ar2 = array<ll, 2>;

//----------------------------------



//--------3.コード短縮化とか---------
#define rep(i,n) for(ll i = 0; i < (n); ++i)
#define rrep(i,n) for(ll i = 1; i <= (n); ++i)
#define drep(i,n) for(ll i = (n)-1; i >= 0; --i)
#define nfor(i,s,n) for(ll i=s;i<n;i++)//i=s,s+1...n-1 ノーマルfor
#define dfor(i,s,n) for(ll i = (s)-1; i>=n;i--)//s-1スタートでnまで落ちる

#define nall(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)

#define pb push_back
#define eb emplace_back
#define em emplace
#define pob pop_back


#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define YN {cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}// if(a==b)YN;
#define dame cout<<-1<<endl


#define vc_unique(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define vc_rotate(v) rotate(v.begin(),v.begin()+1,v.end());

#define pop_cnt(s) ll(popcount(uint64_t(s)))

#define next_p(v) next_permutation(v.begin(),v.end())

//if (regex_match(s, regex("")))YN;//文字列sの判定を行う。コメントアウトを外して「""」の中に判定する内容を入れる

//-------------------------------




//---------4.グリッド系----------
vl di = { 0,1,0,-1 };//vl di={0,1,1,1,0,-1,-1,-1};
vl dj = { 1,0,-1,0 };//vl dj={1,1,0,-1,-1,-1,0,1};

bool out_grid(ll i, ll j, ll h, ll w) {//trueならcontinue
    return (!(0 <= i && i < h && 0 <= j && j < w));
}

#define vvl_kaiten_r(v) {ll n = size(v);vvl nx(n,vl(n));rep(i,n)rep(j,n)nx[j][n-i-1]=v[i][j];swap(nx,v);}//時計回りに90°回転
#define vvl_kaiten_l(v) {ll n = size(v);vvl nx(n,vl(n));rep(i,n)rep(j,n)nx[n-j-1][i]=v[i][j];swap(nx,v);}//反時計周りに90°回転

#define vs_kaiten_r(v) {ll n = size(v);vs nx(n,string(n,'.'));rep(i,n)rep(j,n)nx[j][n-i-1]=v[i][j];swap(nx,v);}//文字列版 時計回りに90°回転
#define vs_kaiten_l(v) {ll n = size(v);vs nx(n,string(n,'.'));rep(i,n)rep(j,n)nx[n-j-1][i]=v[i][j];swap(nx,v);}//文字列版　反時計周りに90°回転


#define vvl_tenti(v) {ll n = size(v);vvl nx(n,vl(n));rep(i,n)rep(j,n)nx[j][i]=v[i][j];swap(nx,v);}
#define vs_tenti(v) {ll n = size(v); vs nx(n, string(n,'.')); rep(i, n)rep(j, n)nx[j][i] = v[i][j]; swap(nx, v);}

//--------------------------------




//-----------5.数学系--------------
#define yu_qurid(x,y) ((x)*(x)+(y)*(y))//ユークリッド距離 sqrtはしてないなので注意
#define mannhattan(x1,x2,y1,y2) (abs(x1-x2)+abs(y1-y2)) // マンハッタン距離 = |x1-x2|+|y1-y2|

template<class T>T tousa_sum1(T l, T d, T r) {//初項,公差,末項 で総和を求める
    T wide = (r - l) / d + 1;
    return (l + r) * wide / 2;
}
template<class T>T tousa_sum2(T a, T d, T n) {//初項,交差,項数 で総和を求める
    return (a * 2 + d * (n - 1)) * n / 2;
}
ll kousa_kousuu(ll l, ll r, ll d) {//初項,末項,交差 で等差数列の項数を求める
    return (r - l) / d + 1;
}
// mint touhi_sum(mint a, mint r, ll n) {//初項,公比,項数で等比数列の総和を求める
//     if (r == 1) {
//         return a * n;
//     }
//     mint bunsi = a * (r.pow(n) - mint(1));
//     mint bunbo = r - 1;
//     return bunsi / bunbo;
// }

ll nc2(ll x) { return x * (x - 1) / 2; }
ll nc3(ll x) { return x * (x - 1) * (x - 2) / 6; }

//----------------------------------------------




//-----------6.デバックや出力系------------------
void print(ld x) { printf("%.20Lf\n", x); }

void mukou_debug(vvl to, bool yukou) {//GRAPH × GRAPH用の無向グラフを出力する
    ll n = size(to); ll cnt = 0;//辺の本数
    vc<pair<ll, ll>>v; rep(i, n)for (ll t : to[i])  if (i < t || yukou)cnt++, v.eb(i + 1, t + 1);//有向グラフなら全部OK、違うなら無向なのでf<tのみ見る、using Pのやつを別のにしたいときのためにPを使わずにpair<ll,ll>にしてる
    cout << n << ' ' << cnt << endl; for (auto [f, t] : v)cout << f << ' ' << t << endl;
}

#define vc_cout(v){ll n = size(v);rep(i,n)cout<<v[i]<<endl;}//一次元配列を出力する
#define vv_cout(v){ll n = size(v);rep(i,n){rep(j,size(v[i])){cout<<v[i][j]<<' ';}cout<<endl;}}//二次元配列を出力する

template <typename T>
struct BIT {
    int n;          // 配列の要素数(数列の要素数+1)
    vector<T> bit;  // データの格納先
    BIT(int n_) : n(n_ + 1), bit(n, 0) {}
    void add(int i, T x) {
        i++;
        for (int idx = i; idx < n; idx += (idx & -idx)) {
            bit[idx] += x;
        }
    }
    // 0番目からl－1番目までの累積和
    T sum(int i) {
        T s(0);
        for (int idx = i; idx > 0; idx -= (idx & -idx)) {
            s += bit[idx];
        }
        return s;
    }
    // [l, r)の要素の総和
    T sum(int l, int r){
        T ret_l = sum(l);
        T ret_r = sum(r);
        return ret_r - ret_l;
    }

    // 累積和が x 以上になる最小のインデックスを返す(x >= 0)
    int lower_bound(T x) {
        int idx = 0;
        int k = 1;
        while (k * 2 < n) k *= 2; // 最大の 2^k

        for (; k > 0; k /= 2) {
            if (idx + k < n && bit[idx + k] < x) {
                x -= bit[idx + k];
                idx += k;
            }
        }
        return idx; // 0-indexed int idx = 0;
    }
};

int main(){
    ll n;
    cin >> n;
    vl a(n);
    rep(i, n) cin >> a[i];

    vl b = a;
    sort(nall(b));
    vc_unique(b);
    rep(i, n){
        a[i] = lower_bound(nall(b), a[i]) - b.begin() + 1;
    }

    BIT<ll> bit(n);
    ll ans = 0;
    rep(j, n){
        ans += j - bit.sum(a[j]);
        bit.add(a[j]-1, 1);
    }

    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 7832
final_rank: 13

code_snippet: #include<iostream>
#define SENTINEL 2000000000
#define MAX 200000

int L[MAX/2+1], R[MAX/2+1];
long long cnt;

void merge(int left, int right, int A[]) {
    int mid = (left + right)/2;
    for (int i = 0; i < mid-left; i++) L[i] = A[left+i];
    for (int i = 0; i < right-mid; i++) R[i] = A[mid+i];
    L[mid-left] = R[right-mid] = SENTINEL;
    int i = 0, j= 0;
    for (int k = left; k < right; k++) {
        if (L[i] < R[j]) {
            A[k] = L[i];
            i++;
        }
        else {
            A[k] = R[j];
            j++;
            cnt += (mid-left)-i;
        }
    }
}

void mergeSort(int left, int right, int A[]) {
    int mid = (left+right)/2;
    if (left+1 < right) {
        mergeSort(left, mid, A);
        mergeSort(mid, right, A);
        merge(left, right, A);
    }
}

int main() {
    int n;
    std::cin >> n;
    int A[n];
    for (int i = 0; i < n; i++) std::cin >> A[i];

    cnt = 0;
    mergeSort(0, n, A);
    std::cout << cnt << std::endl;
}
accuracy: 1
time_ms: 50
memory_kb: 5000
final_rank: 7

code_snippet: #include<iostream>
#define SENTINEL 2000000000
#define MAX 200000

int L[MAX/2+1], R[MAX/2+1];
int cnt;

void merge(int left, int right, int A[]) {
    int mid = (left + right)/2;
    for (int i = 0; i < mid-left; i++) L[i] = A[left+i];
    for (int i = 0; i < right-mid; i++) R[i] = A[mid+i];
    L[mid-left] = R[right-mid] = SENTINEL;
    int i = 0, j= 0;
    for (int k = left; k < right; k++) {
        if (L[i] < R[j]) {
            A[k] = L[i];
            i++;
        }
        else {
            A[k] = R[j];
            j++;
            cnt += (mid-left)-i;
        }
    }
}

void mergeSort(int left, int right, int A[]) {
    int mid = (left+right)/2;
    if (left+1 < right) {
        mergeSort(left, mid, A);
        mergeSort(mid, right, A);
        merge(left, right, A);
    }
}

int main() {
    int n;
    std::cin >> n;
    int A[n];
    for (int i = 0; i < n; i++) std::cin >> A[i];

    cnt = 0;
    mergeSort(0, n, A);
    std::cout << cnt << std::endl;
}
accuracy: 0.6
time_ms: 10
memory_kb: 4132
final_rank: 19

code_snippet: #include<iostream>
#define SENTINEL 2000000000
#define MAX 200000

int L[MAX/2+1], R[MAX/2+1];
int cnt;

void merge(int left, int right, int A[]) {
    int mid = (left + right)/2;
    for (int i = 0; i < mid-left; i++) L[i] = A[left+i];
    for (int i = 0; i < right-mid; i++) R[i] = A[mid+i];
    L[mid-left] = R[right-mid] = SENTINEL;
    int i = 0, j= 0;
    for (int k = left; k < right; k++) {
        if (L[i] < R[j]) {
            A[k] = L[i];
            i++;
        }
        else {
            A[k] = R[j];
            j++;
            if (i != mid-left) cnt += (mid-left)-i;
        }
    }
}

void mergeSort(int left, int right, int A[]) {
    int mid = (left+right)/2;
    if (left+1 < right) {
        mergeSort(left, mid, A);
        mergeSort(mid, right, A);
        merge(left, right, A);
    }
}

int main() {
    int n;
    std::cin >> n;
    int A[n];
    for (int i = 0; i < n; i++) std::cin >> A[i];

    cnt = 0;
    mergeSort(0, n, A);
    std::cout << cnt << std::endl;
}
accuracy: 0.6
time_ms: 10
memory_kb: 4116
final_rank: 18