You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100010;
const int mod = 99999997;
struct Node {
int x, pos;
const bool operator<(const Node &t) const {
return x < t.x;
}
} a[N], b[N];
int n;
int q[N];
LL ans;
// 树状数组模板
#define lowbit(x) (x & -x)
int c[N];
void add(int x, int d) {
for (int i = x; i < N; i += lowbit(i)) c[i] += d;
}
LL sum(int x) {
LL res = 0;
for (int i = x; i; i -= lowbit(i)) res += c[i];
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("P1966.in", "r", stdin);
#endif
scanf("%d", &n); // 火柴的数目
// 记录第一列火柴的高度,火柴的序号
for (int i = 1; i <= n; i++) scanf("%d", &a[i].x), a[i].pos = i;
// 记录第二列火柴的高度,火柴的序号
for (int i = 1; i <= n; i++) scanf("%d", &b[i].x), b[i].pos = i;
// 使用高度这个概念,进行由小到大排序
sort(a + 1, a + 1 + n);
sort(b + 1, b + 1 + n);
// 高度概念消费完毕,只能继续使用位置这个概念了
// 将一个二维的逆序对问题,转化为一维逆序对问题
// a[1].pos : 第一列中高度最小的火柴所在位置
// b[1].pos : 第二列中高度最小的火柴所在位置
// 两者之间为什么需要建立起关联?两者之间如何建立成关联?
// 可以理解为: 以前 Japan那道题出现了多条相交的边需要把它们梳理成互相不相交的边
// 以b为标准将a进行调整
for (int i = 1; i <= n; i++) q[a[i].pos] = b[i].pos; // 类似于离散化
for (int i = 1; i <= n; i++) {
ans = (ans + sum(n) - sum(q[i])) % mod;
add(q[i], 1);
}
// 输出结果
printf("%lld", ans);
return 0;
}