求逆序对(lowbit版)
#include<bits/stdc++.h> typedef long long int ll; using namespace std; const int N = 2e5 + 10; #define lowbit(x) x & -x ll c[N], a[N]; void modify(ll x, ll d) { for (int i = x; i <= n; i += lowbit(i)) c[i] += d; } ll getsum(ll x) { ll sum = 0; for (int i = x; i >= 1; i -= lowbit(i)) sum += c[i]; return sum; } int main() { ll n, ans = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { ans += getsum(n) - getsum(a[i]); modify(a[i], 1); } cout << ans << endl; return 0; }
浅浅记录一下~

浙公网安备 33010602011771号