#include <bits/stdc++.h>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define inf (1987654321)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
int n, m, q;
int mxr[100005], mxc[100005];
ll a[100005], b[100005];
double da[100005], db[100005];
set<pair<double, ll> > st;
set<int> r, c;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= m; ++i) cin >> b[i];
for(int i = 2; i <= n; ++i) st.insert({a[i] - a[i - 1], i}), da[i] = a[i] - a[i - 1];
for(int i = 2; i <= m; ++i) st.insert({b[i] - b[i - 1], i + n}), db[i] = b[i] - b[i - 1];
for(int i = 1; i <= n; ++i) r.insert(i);
for(int i = 1; i <= m; ++i) c.insert(i);
while(!st.empty()) {
auto mn = *st.rbegin(); st.erase(st.find(mn));
if(mn.second <= n) {
mxc[*c.rbegin()] = max((ll)mxc[*c.rbegin()], mn.second);
if(*r.rbegin() != mn.second) {
ll nxtr = *next(r.find(mn.second)), prvr = *prev(r.find(mn.second));
st.erase({da[nxtr], nxtr});
da[nxtr] = (double)(a[nxtr] - a[prvr])/(nxtr - prvr);
st.insert({da[nxtr], nxtr});
}
r.erase(r.find(mn.second));
}
else {
mn.second -= n;
mxr[*r.rbegin()] = max((ll)mxr[*r.rbegin()], mn.second);
if(*c.rbegin() != mn.second) {
ll nxtc = *next(c.find(mn.second)), prvc = *prev(c.find(mn.second));
st.erase({db[nxtc], nxtc + n});
db[nxtc] = (double)(b[nxtc] - b[prvc])/(nxtc - prvc);
st.insert({db[nxtc], nxtc + n});
}
c.erase(c.find(mn.second));
}
}
for(int i = 2; i <= n; ++i) mxr[i] = max(mxr[i], mxr[i - 1]);
for(int i = 2; i <= m; ++i) mxc[i] = max(mxc[i], mxc[i - 1]);
ll ans = 0;
int r = 1, c = 1, t = mxr[r] != 0 ? 1 : 0;
while(r != n || c != m) {
if(t) {
ans += a[r] * abs(mxr[r] - c);
c = mxr[r];
}
else {
ans += b[c] * abs(mxc[c] - r);
r = mxc[c];
}
t = !t;
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
2 ms |
520 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
137 ms |
11160 KB |
Output is correct |
5 |
Correct |
63 ms |
10408 KB |
Output is correct |
6 |
Correct |
44 ms |
4700 KB |
Output is correct |
7 |
Correct |
569 ms |
26884 KB |
Output is correct |
8 |
Correct |
490 ms |
26852 KB |
Output is correct |
9 |
Correct |
523 ms |
26908 KB |
Output is correct |
10 |
Correct |
535 ms |
26908 KB |
Output is correct |
11 |
Correct |
130 ms |
26828 KB |
Output is correct |
12 |
Correct |
557 ms |
26904 KB |
Output is correct |
13 |
Correct |
533 ms |
26892 KB |
Output is correct |
14 |
Correct |
209 ms |
26892 KB |
Output is correct |
15 |
Correct |
106 ms |
27084 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
328 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
2 ms |
520 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
137 ms |
11160 KB |
Output is correct |
33 |
Correct |
63 ms |
10408 KB |
Output is correct |
34 |
Correct |
44 ms |
4700 KB |
Output is correct |
35 |
Correct |
569 ms |
26884 KB |
Output is correct |
36 |
Correct |
490 ms |
26852 KB |
Output is correct |
37 |
Correct |
523 ms |
26908 KB |
Output is correct |
38 |
Correct |
535 ms |
26908 KB |
Output is correct |
39 |
Correct |
130 ms |
26828 KB |
Output is correct |
40 |
Correct |
557 ms |
26904 KB |
Output is correct |
41 |
Correct |
533 ms |
26892 KB |
Output is correct |
42 |
Correct |
209 ms |
26892 KB |
Output is correct |
43 |
Correct |
106 ms |
27084 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
324 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
328 KB |
Output is correct |
53 |
Correct |
1 ms |
328 KB |
Output is correct |
54 |
Correct |
1 ms |
280 KB |
Output is correct |
55 |
Correct |
145 ms |
12488 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
10 ms |
1492 KB |
Output is correct |
58 |
Correct |
33 ms |
3956 KB |
Output is correct |
59 |
Correct |
549 ms |
28056 KB |
Output is correct |
60 |
Correct |
538 ms |
28060 KB |
Output is correct |
61 |
Correct |
514 ms |
28080 KB |
Output is correct |
62 |
Correct |
494 ms |
28040 KB |
Output is correct |
63 |
Correct |
99 ms |
27880 KB |
Output is correct |
64 |
Correct |
499 ms |
28036 KB |
Output is correct |
65 |
Correct |
569 ms |
28188 KB |
Output is correct |
66 |
Correct |
172 ms |
28064 KB |
Output is correct |
67 |
Correct |
107 ms |
28276 KB |
Output is correct |