- 求逆序对
1
- @ 2026-7-23 11:55:58
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
struct Fenwick {
int n;
vector<ll> t;
Fenwick(int n_) {
n = n_; t.assign(n + 1, 0);
}
void change(int u, ll x) {
for (; u <= n; u += u & -u) t[u] += x;
}
ll sum(int u) {
ll sum = 0;
for (; u; u -= u & -u) sum += t[u];
return sum;
}
ll sum(int l, int r) {
return sum(r) - sum(l - 1);
}
};
void solve() {
int n;
cin >> n;
Fenwick t(1e5);
ll res = 0;
for (int i = 1; i <= n; i++) {
int x; cin >> x;
res += t.sum(x + 1, 1e5);
t.change(x, 1);
}
cout << res << '\n';
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
// cout << powl(1 - 1 / 1e9, 3e7) << '\n';
int t = 1;
while (t--)
solve();
return 0;
}
/*
g++ -std=c++20 1.cpp -o 1 && 1 < in.txt > out.txt
*/
0 条评论
目前还没有评论...
信息
- ID
- 504
- 时间
- ms
- 内存
- MiB
- 难度
- 4
- 标签
- 递交数
- 28
- 已通过
- 16
- 上传者