- 求逆序对
code
- @ 2026-2-8 10:59:42
struct Fenwick {
vector<ll> t;
int n;
Fenwick(int n_) {
n = n_;
t.assign(n + 1, 0);
}
void change(int x, int y) {
for (; x <= n; x += x & -x) {
t[x] += y;
}
}
ll query(int x) {
ll res = 0;
for (; x > 0; x -= x & -x) {
res += t[x];
}
return res;
}
ll query(int l, int r) {
return query(r) - query(l - 1);
}
};
void solve() {
int n; cin >> n;
const int N = 1e5;
Fenwick t(N + 1);
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
ll res = 0;
for (int i = 1; i <= n; i++) {
res += t.query(a[i] + 1, N);
t.change(a[i], 1);
}
cout << res << '\n';
}
0 条评论
目前还没有评论...
信息
- ID
- 431
- 时间
- ms
- 内存
- MiB
- 难度
- 5
- 标签
- 递交数
- 22
- 已通过
- 13
- 上传者