#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct choice{
int a, posl, posr;
choice(int a, int posl, int posr): a(a), posl(posl), posr(posr) {}
friend bool operator<(const choice &lhs, const choice &rhs){
ll l = 1LL * lhs.a * (rhs.posr - rhs.posl), r = 1LL * rhs.a * (lhs.posr - lhs.posl);
if (l != r) return l < r;
return lhs.posl < rhs.posl;
}
};
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n, m; cin >> n >> m;
vector<int> a(n), b(m);
for (int i=0; i<n; i++) cin >> a[i];
for (int j=0; j<m; j++) cin >> b[j];
const int MAXV = 1000000000;
set<choice> s;
vector<int> nxta(n), nxtb(m);
for (int i=1; i<n; i++){
nxta[i-1] = i;
s.emplace(a[i-1] - a[i], i-1, i);
}
nxta.back() = -1;
for (int i=1; i<m; i++){
nxtb[i-1] = i;
s.emplace(b[i-1] - b[i], i-1 + MAXV, i + MAXV);
}
nxtb.back() = -1;
ll result = 0;
int lasta = n-1, lastb = m-1;
while (s.size()){
choice c = *s.begin(); s.erase(s.begin());
if (c.posl < MAXV){
int nxtpos = nxta[c.posl] = nxta[c.posr];
nxta[c.posr] = -1;
if (nxtpos == -1){
result += 1LL * b[lastb] * (c.posr - c.posl);
// cout << "B: " << lastb << " " << b[lastb] << "\n";
// cout << s.size() << "\n";
lasta = c.posl;
}
else{
// cout << "SUS: " << s.size();
s.erase(s.find(choice(a[c.posr] - a[nxtpos], c.posr, nxtpos)));
s.emplace(a[c.posl] - a[nxtpos], c.posl, nxtpos);
// cout << " " << s.size() << "\n";
}
}
else{
c.posl -= MAXV, c.posr -= MAXV;
int nxtpos = nxtb[c.posl] = nxtb[c.posr];
nxtb[c.posr] = -1;
if (nxtpos == -1){
result += 1LL * a[lasta] * (c.posr - c.posl);
// cout << "A: " << lasta << " " << a[lasta] << "\n";
// cout << s.size() << "\n";
lastb = c.posl;
}
else{
// cout << "SUS: " << s.size();
s.erase(s.find(choice(b[c.posr] - b[nxtpos], c.posr + MAXV, nxtpos + MAXV)));
s.emplace(b[c.posl] - b[nxtpos], c.posl + MAXV, nxtpos + MAXV);
// cout << " " << s.size() << "\n";
}
}
}
cout << result << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
448 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
71 ms |
6192 KB |
Output is correct |
5 |
Correct |
37 ms |
5784 KB |
Output is correct |
6 |
Correct |
23 ms |
2608 KB |
Output is correct |
7 |
Correct |
279 ms |
14540 KB |
Output is correct |
8 |
Correct |
268 ms |
14456 KB |
Output is correct |
9 |
Correct |
283 ms |
14420 KB |
Output is correct |
10 |
Correct |
284 ms |
14596 KB |
Output is correct |
11 |
Correct |
77 ms |
14424 KB |
Output is correct |
12 |
Correct |
272 ms |
14424 KB |
Output is correct |
13 |
Correct |
272 ms |
14424 KB |
Output is correct |
14 |
Correct |
137 ms |
14444 KB |
Output is correct |
15 |
Correct |
114 ms |
14544 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
616 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
448 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
71 ms |
6192 KB |
Output is correct |
33 |
Correct |
37 ms |
5784 KB |
Output is correct |
34 |
Correct |
23 ms |
2608 KB |
Output is correct |
35 |
Correct |
279 ms |
14540 KB |
Output is correct |
36 |
Correct |
268 ms |
14456 KB |
Output is correct |
37 |
Correct |
283 ms |
14420 KB |
Output is correct |
38 |
Correct |
284 ms |
14596 KB |
Output is correct |
39 |
Correct |
77 ms |
14424 KB |
Output is correct |
40 |
Correct |
272 ms |
14424 KB |
Output is correct |
41 |
Correct |
272 ms |
14424 KB |
Output is correct |
42 |
Correct |
137 ms |
14444 KB |
Output is correct |
43 |
Correct |
114 ms |
14544 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
72 ms |
7524 KB |
Output is correct |
56 |
Correct |
1 ms |
472 KB |
Output is correct |
57 |
Correct |
5 ms |
1116 KB |
Output is correct |
58 |
Correct |
20 ms |
2396 KB |
Output is correct |
59 |
Correct |
298 ms |
16472 KB |
Output is correct |
60 |
Correct |
266 ms |
16464 KB |
Output is correct |
61 |
Correct |
255 ms |
16512 KB |
Output is correct |
62 |
Correct |
374 ms |
16488 KB |
Output is correct |
63 |
Correct |
79 ms |
16464 KB |
Output is correct |
64 |
Correct |
249 ms |
16432 KB |
Output is correct |
65 |
Correct |
273 ms |
16468 KB |
Output is correct |
66 |
Correct |
109 ms |
16468 KB |
Output is correct |
67 |
Correct |
102 ms |
16440 KB |
Output is correct |