forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
50 lines (47 loc) · 1.15 KB
/
B.cpp
File metadata and controls
50 lines (47 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
int m, n, x;
vector < pair<int,int> > a;
cin >> m >> n;
for (int i = 1; i <= m; i++)
{
cin >> x;
a.push_back({x, i});
}
for (int i = 1; i <= n; i++)
{
cin >> x;
a.push_back({x, -i});
}
sort(a.begin(), a.end());
long long ans = 0;
long long sumIndexA = 0, sumIndexB = 0;
long long sumValueA = 0, sumValueB = 0;
long long sumProductA = 0, sumProductB = 0;
long long cntA = 0, cntB = 0;
for (auto u : a)
if (u.second > 0)
{
int value = u.first, index = u.second;
ans += value * (1LL * index * cntB - sumIndexB);
ans += sumProductB - sumValueB * index;
cntA++;
sumProductA += 1LL * value * index;
sumIndexA += index;
sumValueA += value;
}
else
{
int value = u.first, index = -u.second;
ans -= value * (1LL * index * cntA - sumIndexA);
ans -= sumProductA - sumValueA * index;
cntB++;
sumProductB += 1LL * value * index;
sumIndexB += index;
sumValueB += value;
}
cout << ans << endl;
}