From 1e7407922a9ab7a4b866deb631434165bf8e7071 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 19 Jun 2022 19:01:27 +0800 Subject: [PATCH] =?UTF-8?q?P5094=20[USACO04OPEN]=20MooFest=20G=20=E5=8A=A0?= =?UTF-8?q?=E5=BC=BA=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/77636702 --- Luogu/P5094/P5094.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Luogu/P5094/P5094.cpp diff --git a/Luogu/P5094/P5094.cpp b/Luogu/P5094/P5094.cpp new file mode 100644 index 00000000..b9eec31e --- /dev/null +++ b/Luogu/P5094/P5094.cpp @@ -0,0 +1,54 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 5e4 + 5; + +int n; +long long c1[N], c2[N], s, ans; +std::pair p[N]; + +inline int lowbit(int x) { + return x & -x; +} + +void add(long long* c, int x, int y) { + for (; x <= 5e4; x += lowbit(x)) c[x] += y; +} + +long long sum(long long* c, int x) { + long long res = 0; + for (; x; x -= lowbit(x)) res += c[x]; + return res; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> p[i].first >> p[i].second; + } + + std::sort(p + 1, p + 1 + n); + + for (int i = 1; i <= n; i++) { + int a = sum(c1, p[i].second), + b = sum(c2, p[i].second); + + ans += ((a * p[i].second - b) + ((s - b) - (i - a - 1) * p[i].second)) * p[i].first; + + add(c1, p[i].second, 1); + add(c2, p[i].second, p[i].second); + s += p[i].second; + } + + cout << ans << endl; + + return 0; +}