From f587de10cff4d64e0c4fc02020e85c9ffde4d65e Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 11 Apr 2022 13:20:35 +0800 Subject: [PATCH] =?UTF-8?q?241.=20=E6=A5=BC=E5=85=B0=E5=9B=BE=E8=85=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/13302049/ --- AcWing/241/241.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 AcWing/241/241.cpp diff --git a/AcWing/241/241.cpp b/AcWing/241/241.cpp new file mode 100644 index 00000000..d7f5b0fa --- /dev/null +++ b/AcWing/241/241.cpp @@ -0,0 +1,52 @@ +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2e5 + 5; + +int n, y[N], l[N], r[N], c1[N], c2[N]; +long long ans1, ans2; + +inline int lowbit(int x) { + return x & -x; +} + +void add(int *c, int x, int y) { + for (; x <= n; x += lowbit(x)) c[x] += y; +} + +int sum(int *c, int x) { + int ans = 0; + for (; x; x -= lowbit(x)) ans += c[x]; + return ans; +} + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> y[i]; + } + + for (int i = 1; i <= n; i++) { + l[i] = sum(c1, y[i] - 1); + add(c1, y[i], 1); + } + + for (int i = n; i; i--) { + r[i] = sum(c2, y[i] - 1); + add(c2, y[i], 1); + } + + for (int i = 1; i <= n; i++) { + ans1 += 1ll * (i - 1 - l[i]) * (n - i - r[i]); + ans2 += 1ll * l[i] * r[i]; + } + + cout << ans1 << ' ' << ans2 << endl; + + return 0; +}