[AGC023E] Inversions

题解

对于 \(i, j\),考虑 \(P_i > P_j\) 的方案数。只需考虑 \(A_i \le A_j\) 的情况即可。这相当于是把 \(A_j\) 改为 \(A_i\) 之后满足 \(\forall i, P_i \le A_i\) 的限制的排列数除以 \(2\)

\(1 \ldots n\)\(A_i\) 从小到大排序,设排序后第 \(i\) 个数是 \(p_i\)。显然满足 \(\forall i, P_i \le A_i\) 的排列总数为 \(\prod_i A_{p_i}-i+1\)。设总数为 \(C\)

\(B_i = \frac{A_{p_i}-i}{A_{p_i}-i+1}\)

对所有满足 \(p_i < p_j\)\((i,j)\) 计算 \(P_{p_i} > P_{p_j}\) 的排列数。对于每一对 \(i < j\),如果 \(p_i < p_j\),那么它的贡献是 \(\frac 1 2 C\frac{A_{p_i}-i}{A_{p_j}-j+1}\prod_{k=i+1}^{j-1} B_k\)\(p_i > p_j\) 的情况没有很大区别,具体式子就不写出来了。枚举 \(j\),用线段树对每个 \(p_i\) 维护下这个式子,每次移动 \(j\) 的时候区间乘更新,统计答案时区间求和即可。

代码

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pi;

const int mod = 1e9+7;
const int maxn = 200010;

int n, C = 1, ans = 0;
pi a[maxn];
int b[maxn], sum[maxn<<2], cnt[maxn<<2], K[maxn<<2];

int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1LL * ret * x % mod;
x = 1LL * x * x % mod;
y >>= 1;
}
return ret;
}

void pushUp(int rt) {
sum[rt] = (sum[rt<<1] + sum[rt<<1|1]) % mod;
cnt[rt] = cnt[rt<<1] + cnt[rt<<1|1];
}

void modify(int rt, int k) {
sum[rt] = 1LL * sum[rt] * k % mod;
K[rt] = 1LL * K[rt] * k % mod;
}

void pushDown(int rt) {
if (K[rt] != 1) {
modify(rt<<1, K[rt]);
modify(rt<<1|1, K[rt]);
K[rt] = 1;
}
}

void update(int p, int v, int l, int r, int rt) {
if (l == r) {
sum[rt] = (sum[rt] + v) % mod;
cnt[rt] ++;
return;
}
pushDown(rt);
int m = (l + r) >> 1;
if (p <= m) update(p, v, l, m, rt<<1);
else update(p, v, m+1, r, rt<<1|1);
pushUp(rt);
}

int query(int L, int R, int l, int r, int rt) {
if (L > R) return 0;
if (L <= l && r <= R) return sum[rt];
int ret = 0;
pushDown(rt);
int m = (l + r) >> 1;
if (L <= m) ret = (ret + query(L, R, l, m, rt<<1)) % mod;
if (R > m) ret = (ret + query(L, R, m+1, r, rt<<1|1)) % mod;
return ret;
}

int query_cnt(int L, int R, int l, int r, int rt) {
if (L > R) return 0;
if (L <= l && r <= R) return cnt[rt];
int ret = 0;
int m = (l + r) >> 1;
if (L <= m) ret = ret + query_cnt(L, R, l, m, rt<<1);
if (R > m) ret = ret + query_cnt(L, R, m+1, r, rt<<1|1);
return ret;
}

int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].first);
a[i].second = i;
}
sort(a+1, a+n+1);
for (int i = 1; i <= n; i++) {
if (a[i].first-i+1 <= 0) {
puts("0");
return 0;
}
b[i] = 1LL*(a[i].first-i)*qpow(a[i].first-i+1, mod-2)%mod;
}
for (int i = 1; i <= n; i++) C = 1LL * C * (a[i].first-i+1) % mod;
for (int i = 1; i <= n; i++) {
int v = 1LL*(mod+1)/2*C%mod*qpow(a[i].first-i+1, mod-2)%mod;
ans = (ans + 1LL*v*query(1, a[i].second-1, 1, n, 1)%mod)%mod;
ans = ((ans + 1LL*C*query_cnt(a[i].second+1, n, 1, n, 1)%mod)%mod+mod-1LL*v*query(a[i].second+1, n, 1, n, 1)%mod)%mod;
K[1] = 1LL * K[1] * b[i] % mod;
sum[1] = 1LL * sum[1] * b[i] % mod;
update(a[i].second, a[i].first-i, 1, n, 1);
}
printf("%d\n", ans);
return 0;
}