#include <bits/stdc++.h>
using namespace std;
struct rc {
int type, ind, prv;
long double value;
bool operator < ( const rc &x ) const {
return value < x.value;
}
};
const int MAX_N = 1e5;
const int MAX_M = 1e5;
int a[MAX_N + 1], b[MAX_M + 1], prvA[MAX_N + 1], nxtA[MAX_N + 1], prvB[MAX_M + 1], nxtB[MAX_M + 1];
priority_queue<rc> pq;
int main() {
int n, m;
cin >> n >> m;
for ( int i = 1; i <= n; i++ )
cin >> a[i];
for ( int i = 1; i <= m; i++ )
cin >> b[i];
prvA[1] = 0;
for ( int i = 2; i <= n; i++ ) {
pq.push( { 0, i, i - 1, (double)a[i] - a[i - 1] } );
prvA[i] = i - 1;
nxtA[i - 1] = i;
}
nxtA[n] = -1;
prvB[1] = -1;
for ( int i = 2; i <= m; i++ ) {
pq.push( { 1, i, i - 1, (long double)b[i] - b[i - 1] } );
prvB[i] = i - 1;
nxtB[i - 1] = i;
}
nxtB[m] = -1;
long long ans = 0;
while ( !pq.empty() ) {
auto x = pq.top();
pq.pop();
if ( (x.type == 0 && prvA[x.ind] != x.prv) || (x.type == 1 && prvB[x.ind] != x.prv) )
continue;
if ( x.type == 0 ) {
if ( nxtA[x.ind] == -1 ) {
ans += (long long)b[m] * (x.ind - prvA[x.ind]);
n = prvA[x.ind];
} else {
pq.push( { 0, nxtA[x.ind], prvA[x.ind], (long double)(a[nxtA[x.ind]] - a[prvA[x.ind]]) / (nxtA[x.ind] - prvA[x.ind]) } );
prvA[nxtA[x.ind]] = prvA[x.ind];
}
nxtA[prvA[x.ind]] = nxtA[x.ind];
} else {
if ( nxtB[x.ind] == -1 ) {
ans += (long long)a[n] * (x.ind - prvB[x.ind]);
m = prvB[x.ind];
} else {
pq.push( { 1, nxtB[x.ind], prvB[x.ind], (long double)(b[nxtB[x.ind]] - b[prvB[x.ind]]) / (nxtB[x.ind] - prvB[x.ind]) } );
prvB[nxtB[x.ind]] = prvB[x.ind];
}
nxtB[prvB[x.ind]] = nxtB[x.ind];
}
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2488 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
2 ms |
2648 KB |
Output is correct |
10 |
Correct |
2 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2500 KB |
Output is correct |
14 |
Correct |
3 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2628 KB |
Output is correct |
18 |
Correct |
1 ms |
2500 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2488 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
51 ms |
7116 KB |
Output is correct |
5 |
Correct |
34 ms |
7884 KB |
Output is correct |
6 |
Correct |
18 ms |
4708 KB |
Output is correct |
7 |
Correct |
128 ms |
13312 KB |
Output is correct |
8 |
Correct |
128 ms |
11968 KB |
Output is correct |
9 |
Correct |
134 ms |
12488 KB |
Output is correct |
10 |
Correct |
128 ms |
12800 KB |
Output is correct |
11 |
Correct |
88 ms |
12988 KB |
Output is correct |
12 |
Correct |
127 ms |
11932 KB |
Output is correct |
13 |
Correct |
130 ms |
12420 KB |
Output is correct |
14 |
Correct |
100 ms |
12696 KB |
Output is correct |
15 |
Correct |
81 ms |
12480 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2496 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2488 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2488 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
2 ms |
2648 KB |
Output is correct |
10 |
Correct |
2 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2500 KB |
Output is correct |
14 |
Correct |
3 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2628 KB |
Output is correct |
18 |
Correct |
1 ms |
2500 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2488 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
0 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
51 ms |
7116 KB |
Output is correct |
33 |
Correct |
34 ms |
7884 KB |
Output is correct |
34 |
Correct |
18 ms |
4708 KB |
Output is correct |
35 |
Correct |
128 ms |
13312 KB |
Output is correct |
36 |
Correct |
128 ms |
11968 KB |
Output is correct |
37 |
Correct |
134 ms |
12488 KB |
Output is correct |
38 |
Correct |
128 ms |
12800 KB |
Output is correct |
39 |
Correct |
88 ms |
12988 KB |
Output is correct |
40 |
Correct |
127 ms |
11932 KB |
Output is correct |
41 |
Correct |
130 ms |
12420 KB |
Output is correct |
42 |
Correct |
100 ms |
12696 KB |
Output is correct |
43 |
Correct |
81 ms |
12480 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2496 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
0 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2488 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
71 ms |
9160 KB |
Output is correct |
56 |
Correct |
1 ms |
2396 KB |
Output is correct |
57 |
Correct |
6 ms |
3028 KB |
Output is correct |
58 |
Correct |
18 ms |
3896 KB |
Output is correct |
59 |
Correct |
156 ms |
14220 KB |
Output is correct |
60 |
Correct |
175 ms |
13688 KB |
Output is correct |
61 |
Correct |
158 ms |
14524 KB |
Output is correct |
62 |
Correct |
157 ms |
14648 KB |
Output is correct |
63 |
Correct |
112 ms |
13584 KB |
Output is correct |
64 |
Correct |
161 ms |
14012 KB |
Output is correct |
65 |
Correct |
164 ms |
14120 KB |
Output is correct |
66 |
Correct |
131 ms |
14780 KB |
Output is correct |
67 |
Correct |
102 ms |
13972 KB |
Output is correct |