Submission details
Task:Sorting books
Sender:aalto26dw_002
Submission time:2026-09-23 17:24:39 +0300
Language:C++ (C++23)
Status:COMPILE ERROR

Compiler report

In file included from /usr/include/c++/13/string:43,
                 from /usr/include/c++/13/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:52,
                 from input/code.cpp:5:
/usr/include/c++/13/bits/allocator.h: In destructor 'constexpr std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl()':
/usr/include/c++/13/bits/allocator.h:184:7: error: inlining failed in call to 'always_inline' 'constexpr std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = int]': target specific option mismatch
  184 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/13/vector:66,
                 from /usr/include/c++/13/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:53:
/usr/include/c++/13/bits/stl_vector.h:133:14: note: called from here
  133 |       struct _Vector_impl
      |              ^~~~~~~~~~~~

Code

#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif
#include "bits/stdc++.h"
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fill(arr,val) memset(arr,val,sizeof(arr))
#define FOR(i, a, b) for(__typeof(b) i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for(__typeof(a) i = a, _b = b; i >= _b; --i)
#define ALL(a) (a).begin(), (a).end()
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ii pair<int,int>
#define iii pair<int,pair<int,int>>
#define dq deque<int>
#define nend '\n'
using namespace std;

const ll inf = 1e18;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};
const int MOD = 1e9 + 7;
const ll hashi = 2e9 + 11;

inline ll add(ll a, ll b) { return (a + b) % MOD; }
inline ll sub(ll a, ll b) { return ((a - b) % MOD + MOD) % MOD; }
inline ll mul(ll a, ll b) { return (a * b) % MOD; }

inline ll binpow(ll a, ll b) {
    ll res = 1;
    a %= MOD;
    while (b > 0) {
        if (b & 1) res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

inline ll modInverse(ll n) {
    return binpow(n, MOD - 2);
}

inline ll modDivide(ll a, ll b) {
    return (a % MOD * modInverse(b)) % MOD;
}

ll extended_gcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1; y = 0;
        return a;
    }
    ll x1, y1;
    ll d = extended_gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

void init_code() {
    fast;
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif
}
/// sorting books: one swap of adjacent "wrong" books fixes exactly 1 inversion
/// -> answer = number of inversions = pairs i<j with a[i]>a[j]
/// count with bit: go left to right, for a[i] count how many ALREADY inserted
/// values are bigger = (inserted so far) - (inserted values <= a[i])
/// values up to 1e9 -> compress to 1..n first. O(n log n)
/// answer can be n*(n-1)/2 ~ 2e10 -> ll
const int N=2e5+5;
int n,a[N],bit[N];
void upd(int i) {
    for (; i<=n; i+=i&-i) bit[i]++;
}
int get(int i) {
    int s=0;
    for (; i>0; i-=i&-i) s+=bit[i];
    return s;
}
void solve() {
    cin >> n;
    FOR(i,1,n) cin >> a[i];
    vector<int> v(a+1,a+n+1);
    sort(ALL(v));
    v.erase(unique(ALL(v)),v.end());
    FOR(i,1,n) a[i]=lower_bound(ALL(v),a[i])-v.begin()+1;
    ll res=0;
    FOR(i,1,n) {
        res+=(i-1)-get(a[i]); ///inserted before, but bigger than a[i]
        upd(a[i]);
    }
    cout << res << nend;
}
int main() {
    init_code();

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }

}