精灵魔法
题目描述
输入格式
输出格式
一行一个正整数\(K\),表示「反超」的个数。
样例
样例输入
3
1 2 3
2 1 3
样例输出
1
code
/*
3
1 2 3
2 1 3
*/
/*
1
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
#define int long long
struct edge {
int x, v;
} a[maxn];
int lowbit(int x) { return x & (-x); }
int n, b[maxn], tot[maxn];
void add(int x) {
while (x <= n) tot[x]++, x += lowbit(x);
}
int query(int x) {
int cnt = 0;
while (x) cnt += tot[x], x -= lowbit(x);
return cnt;
}
bool cmp(edge a, edge b) { return a.x < b.x; }
signed main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].x;
for (int i = 1; i <= n; i++) cin >> a[i].v, b[i] = a[i].v;
stable_sort(a + 1, a + n + 1, cmp);
stable_sort(b + 1, b + n + 1);
for (int i = 1; i <= n; i++) a[i].v = lower_bound(b + 1, b + n + 1, a[i].v) - b;
int cnt = 0;
for (int i = n; i >= 1; i--) cnt += query(a[i].v - 1), add(a[i].v);
return cout << cnt << endl, 0;
}