提交时间:2024-01-25 14:23:51
运行 ID: 128189
#include<bits/stdc++.h> #define int long long using namespace std; int n,ans,a[514514],c[514514]; inline int fr(){ register int x = 0,f = 1; static char c = getchar(); while (c < '0' || c > '9'){ if (c == '-'){ f = -1; } c = getchar(); } while (c >= '0' && c <= '9'){ x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return x * f; } inline void fw(int x){ if (x < 0){ putchar('-'); x = -x; } if (x > 9){ fw(x / 10); } putchar(x % 10 + 48); } int lowbit(int x){ return (x & (-x)); } void add(int x,int val){ while (x <= n){ c[x] += val; x += lowbit(x); } } int sum(int x){ int ret = 0; while (x >= 1){ ret += c[x]; x -= lowbit(x); } return ret; } signed main(){ n = fr(); for (int i = 1;i <= n;i++){ a[i] = fr(); } for (int i = 1;i <= n;i++){ add(a[i],1); ans += i - sum(a[i]); } fw(ans); return 0; }