Loading... ``` #include <iostream> using namespace std; using ll = long long; const int N = 1e5 + 10; ll res; int n, arr[N]; void eval(int left, int right) { if (left >= right) return ; int mid = ((right - left) >> 1) + left; eval(left, mid), eval(mid + 1, right); int l = left, r = mid + 1, temp[right - left + 1], idx = 0; while(l <= mid && r <= right) { if (arr[l] > arr[r]) { res += mid - l + 1; temp[idx++] = arr[r++]; } else temp[idx++] = arr[l++]; } while(l <= mid) temp[idx++] = arr[l++]; while(r <= right) temp[idx++] = arr[r++]; for (int i = left, idx = 0; i <= right; ++i) arr[i] = temp[idx++]; } int main () { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; ++i) cin >> arr[i]; eval(0, n - 1); cout << res; return 0; } ``` 最后修改:2024 年 08 月 08 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏