#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 100000; const int MOD = 99999997; struct Stick { int id, height; bool operator<(const Stick &other) const { return height < other.height; } } s1[MAXN + 1], s2[MAXN + 1]; int t[MAXN + 1], tmp[MAXN + 1]; int ans; void mergeSort (int *a, int l, int r) { if (l >= r) return; int mid = (l + r) / 2; mergeSort(a, l, mid); mergeSort(a, mid + 1, r); int i = l, j = mid + 1, k = l; while (i <= mid && j <= r) { if (a[i] > a[j]) { tmp[k++] = a[j++]; ans = (ans + (mid - i + 1) % MOD) % MOD; } else { tmp[k++] = a[i++]; } } while (i <= mid) tmp[k++] = a[i++]; while (j <= r) tmp[k++] = a[j++]; for (int i = l; i <= r; i++) a[i] = tmp[i]; return; } int main () { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &s1[i].height); s1[i].id = i; } for (int i = 0; i < n; i++) { scanf("%d", &s2[i].height); s2[i].id = i; } sort(s1, s1 + n); sort(s2, s2 + n); for (int i = 0; i < n; i++) t[s2[i].id] = s1[i].id; mergeSort(t, 0, n - 1); printf("%d\n", ans); return 0; }
|