#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
struct point {
long long x, y;
point(): x(0), y(0) {};
point(long long _x, long long _y): x(_x), y(_y) {};
};
point operator-(point p1, point p2) {
return point(p1.x - p2.x, p1.y - p2.y);
}
long long operator^(point p1, point p2) {
return p1.x * p2.y - p1.y * p2.x;
}
const int maxN = 1e5 + 20;
int A[maxN];
int B[maxN];
bool ccw(point p1, point p2, point p3) {
return ((p1 - p2) ^ (p3 - p2)) < 0;
}
vector<point> build(int A[], int N) {
point L(1, A[1]);
point R(N, A[N]);
vector<point> hull = {L};
for (int i = 2; i <= N; i++) {
point cur(i, A[i]);
if (i != N && !ccw(L, cur, R)) {
continue;
}
while ((int)hull.size() >= 2 && !ccw(hull.end()[-2], hull.back(), cur)) {
hull.pop_back();
}
hull.push_back(cur);
}
return hull;
}
void just_do_it() {
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];
}
vector<point> hullA = build(A, N);
vector<point> hullB = build(B, M);
int szA = hullA.size();
int szB = hullB.size();
int ptA = 0;
int ptB = 0;
int cx = 1;
int cy = 1;
long long res = 0;
while (ptA < szA - 1 || ptB < szB - 1) {
if (ptA < szA - 1 && (ptB == szB - 1 || ((hullA[ptA + 1] - hullA[ptA]) ^ (hullB[ptB + 1] - hullB[ptB])) > 0)) {
ptA++;
res += (hullA[ptA].x - cx) * B[cy];
cx = hullA[ptA].x;
}
else {
ptB++;
res += (hullB[ptB].x - cy) * A[cx];
cy = hullB[ptB].x;
}
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
960 KB |
Output is correct |
5 |
Correct |
5 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
580 KB |
Output is correct |
7 |
Correct |
12 ms |
1856 KB |
Output is correct |
8 |
Correct |
12 ms |
1756 KB |
Output is correct |
9 |
Correct |
12 ms |
1836 KB |
Output is correct |
10 |
Correct |
12 ms |
1832 KB |
Output is correct |
11 |
Correct |
11 ms |
1736 KB |
Output is correct |
12 |
Correct |
12 ms |
1892 KB |
Output is correct |
13 |
Correct |
13 ms |
1868 KB |
Output is correct |
14 |
Correct |
11 ms |
1748 KB |
Output is correct |
15 |
Correct |
11 ms |
1996 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 |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
332 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 |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
332 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 |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
5 ms |
960 KB |
Output is correct |
33 |
Correct |
5 ms |
852 KB |
Output is correct |
34 |
Correct |
2 ms |
580 KB |
Output is correct |
35 |
Correct |
12 ms |
1856 KB |
Output is correct |
36 |
Correct |
12 ms |
1756 KB |
Output is correct |
37 |
Correct |
12 ms |
1836 KB |
Output is correct |
38 |
Correct |
12 ms |
1832 KB |
Output is correct |
39 |
Correct |
11 ms |
1736 KB |
Output is correct |
40 |
Correct |
12 ms |
1892 KB |
Output is correct |
41 |
Correct |
13 ms |
1868 KB |
Output is correct |
42 |
Correct |
11 ms |
1748 KB |
Output is correct |
43 |
Correct |
11 ms |
1996 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 |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
332 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 |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
332 KB |
Output is correct |
55 |
Correct |
8 ms |
1512 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
468 KB |
Output is correct |
58 |
Correct |
3 ms |
704 KB |
Output is correct |
59 |
Correct |
18 ms |
2952 KB |
Output is correct |
60 |
Correct |
18 ms |
2912 KB |
Output is correct |
61 |
Correct |
17 ms |
3008 KB |
Output is correct |
62 |
Correct |
18 ms |
3048 KB |
Output is correct |
63 |
Correct |
18 ms |
4972 KB |
Output is correct |
64 |
Correct |
21 ms |
3064 KB |
Output is correct |
65 |
Correct |
23 ms |
2968 KB |
Output is correct |
66 |
Correct |
23 ms |
2900 KB |
Output is correct |
67 |
Correct |
17 ms |
3144 KB |
Output is correct |