求逆序对

题意

求区间逆序对

分析

权值树状数组 + 离散化

倒着插,每次查询比当前数字小的数的数量的即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 1e3 + 6;
vector<int> a, b;
int BIT[maxn];

inline int getid(int x) {
return lower_bound(b.begin(), b.end(), x) - b.begin() + 1;
}
inline int lowb(int x) { return x & (-x); }
void add(int x) {
while (x < b.size()) {
BIT[x]++;
x += lowb(x);
}
}
int sum(int x) {
int ret = 0;
while (x > 0) {
ret += BIT[x];
x -= lowb(x);
}
return ret;
}
int main() {
// /*
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
// */
std::ios::sync_with_stdio(false);
int n;
while (cin >> n) {
a.resize(n + 5);
b.resize(n + 5);
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
b.resize(distance(b.begin(), unique(b.begin(), b.end())));
sort(b.begin(), b.end());
int ans = 0;
clr(BIT, 0);
for (int i = n - 1; i >= 0; i--) {
ans += sum(getid(a[i]) - 1);
add(getid(a[i]));
}
cout << ans << endl;
}
}
Thank you for your support!