0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

P5094 [USACO04OPEN] MooFest G 加强版

https://www.luogu.com.cn/record/77636702
This commit is contained in:
Baoshuo Ren 2022-06-19 19:01:27 +08:00
parent 574de078bc
commit 1e7407922a
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

54
Luogu/P5094/P5094.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <iostream>
#include <algorithm>
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<int, int> 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;
}