//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef DEBUG
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
const int oo = 1e9+7;
struct T{
int first, second, idx;
bool vertical;
T(int _f, int _s, int _i, int _v): first(_f), second(_s), idx(_i), vertical(_v) {}
bool operator < (const T &y) const {return first * y.second == second * y.first ? (idx == y.idx ? vertical < y.vertical : idx < y.idx) : first * y.second < second * y.first;};
};
void __print(T a){
cerr << "{" << a.first << ", " << a.second << "}, " << a.idx << ", " << a.vertical;
}
void solve(){
int n, m; cin >> n >> m;
vector<int>a(n+2, -oo), b(m+2, -oo);
for (int i = 1; i<=n; i++) cin >> a[i];
for (int i = 1; i<=m; i++) cin >> b[i];
multiset<T>s;
set<int>aliveA = {-oo, 1, oo}, aliveB = {-oo, 1, oo};
for (int i = 1; i<n; i++) {
aliveA.insert(i+1);
s.insert({a[i+1]-a[i], 1LL, i+1, 0});
}
for (int i = 1; i<m; i++) {
aliveB.insert(i+1);
s.insert({b[i+1]-b[i], 1LL, i+1, 1});
}
// debug(s);
pair<int, int> now = {n, m};
int ans = 0;
while (s.size()){
auto it = *s.rbegin();
if (!it.vertical){
a.swap(b);
aliveA.swap(aliveB);
}
//B
auto it2 = aliveB.lower_bound(it.idx);
auto R = *(++it2);
--it2;
auto L = *(--it2);
if (L != -oo && R != oo) s.insert({b[R] - b[L], R-L, R, it.vertical});
if (R != oo) s.erase(s.find({b[R] - b[it.idx], R-it.idx, R, it.vertical}));
aliveB.erase(it.idx);
//
if (!it.vertical){
a.swap(b);
aliveA.swap(aliveB);
}
if (!it.vertical && now.first == it.idx){
ans += b[now.second] * it.second;
now.first -= it.second;
}
if (it.vertical && now.second == it.idx) {
ans += a[now.first] * it.second;
now.second -= it.second;
}
// debug(it, now);
s.erase(s.find(it));
}
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
4 ms |
608 KB |
Output is correct |
10 |
Correct |
3 ms |
480 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
6 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
320 KB |
Output is correct |
25 |
Correct |
1 ms |
324 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
456 KB |
Output is correct |
4 |
Correct |
237 ms |
11400 KB |
Output is correct |
5 |
Correct |
115 ms |
10588 KB |
Output is correct |
6 |
Correct |
96 ms |
4812 KB |
Output is correct |
7 |
Correct |
682 ms |
27600 KB |
Output is correct |
8 |
Correct |
747 ms |
27716 KB |
Output is correct |
9 |
Correct |
838 ms |
27700 KB |
Output is correct |
10 |
Correct |
820 ms |
27704 KB |
Output is correct |
11 |
Correct |
260 ms |
27632 KB |
Output is correct |
12 |
Correct |
768 ms |
27588 KB |
Output is correct |
13 |
Correct |
786 ms |
27716 KB |
Output is correct |
14 |
Correct |
361 ms |
27612 KB |
Output is correct |
15 |
Correct |
203 ms |
27916 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
224 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
4 ms |
608 KB |
Output is correct |
10 |
Correct |
3 ms |
480 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
6 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
320 KB |
Output is correct |
25 |
Correct |
1 ms |
324 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
456 KB |
Output is correct |
32 |
Correct |
237 ms |
11400 KB |
Output is correct |
33 |
Correct |
115 ms |
10588 KB |
Output is correct |
34 |
Correct |
96 ms |
4812 KB |
Output is correct |
35 |
Correct |
682 ms |
27600 KB |
Output is correct |
36 |
Correct |
747 ms |
27716 KB |
Output is correct |
37 |
Correct |
838 ms |
27700 KB |
Output is correct |
38 |
Correct |
820 ms |
27704 KB |
Output is correct |
39 |
Correct |
260 ms |
27632 KB |
Output is correct |
40 |
Correct |
768 ms |
27588 KB |
Output is correct |
41 |
Correct |
786 ms |
27716 KB |
Output is correct |
42 |
Correct |
361 ms |
27612 KB |
Output is correct |
43 |
Correct |
203 ms |
27916 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
320 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
224 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
316 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
316 KB |
Output is correct |
55 |
Correct |
278 ms |
12812 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
16 ms |
1492 KB |
Output is correct |
58 |
Correct |
40 ms |
4156 KB |
Output is correct |
59 |
Correct |
766 ms |
27956 KB |
Output is correct |
60 |
Correct |
892 ms |
27952 KB |
Output is correct |
61 |
Correct |
740 ms |
28080 KB |
Output is correct |
62 |
Correct |
1020 ms |
28080 KB |
Output is correct |
63 |
Correct |
190 ms |
28088 KB |
Output is correct |
64 |
Correct |
943 ms |
28080 KB |
Output is correct |
65 |
Correct |
1016 ms |
28084 KB |
Output is correct |
66 |
Correct |
322 ms |
28024 KB |
Output is correct |
67 |
Correct |
228 ms |
28080 KB |
Output is correct |