#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 1e5 + 10;
const ll inf = 1e18;
int h, w;
ll a[maxn], b[maxn], dp[maxn];
struct slope
{
int idx, type;
ll num, den;
slope(int _idx = 0, int _type = 0, ll _num = 0, ll _den = 0)
{
idx = _idx;
type = _type;
num = _num;
den = _den;
}
bool operator < (const slope &s) const
{
if (num * s.den == s.num * den)
{
if (type == s.type)
return idx < s.idx;
return type < s.type;
}
return num * s.den > s.num * den;
}
};
void solve()
{
cin >> h >> w;
for (int i = 1; i <= h; i ++)
cin >> a[i];
for (int i = 1; i <= w; i ++)
cin >> b[i];
set < int > active_row, active_col;
multiset < slope > dif;
for (int i = 2; i <= h; i ++)
{
active_row.insert(i);
slope cur(i, 0, a[i] - a[i - 1], 1);
dif.insert(cur);
}
for (int i = 2; i <= w; i ++)
{
active_col.insert(i);
slope cur(i, 1, b[i] - b[i - 1], 1);
dif.insert(cur);
}
ll ans = 0;
while(!dif.empty())
{
slope cur = *dif.begin();
dif.erase(dif.find(cur));
///cout << "step " << cur.idx << " " << cur.type << " " << cur.num << " " << cur.den << endl;
if (cur.type == 0)
{
set < int > :: iterator it = active_row.find(cur.idx);
if (next(it) == active_row.end())
{
int last = 1, bef = 1;
if (it != active_row.begin())
bef = *prev(it);
if (active_col.size() > 0)
last = *active_col.rbegin();
ans = ans + (ll)(*it - bef) * b[last];
///cout << "ans " << ans << " " << *it - bef << " " << endl;
}
else
{
int nxt = *next(it);
slope sl(nxt, 0, a[nxt] - a[cur.idx], nxt - cur.idx);
dif.erase(dif.find(sl));
int bef = 1;
if (it != active_row.begin())
bef = *prev(it);
sl = slope(nxt, 0, a[nxt] - a[bef], nxt - bef);
dif.insert(sl);
}
active_row.erase(it);
}
else
{
set < int > :: iterator it = active_col.find(cur.idx);
if (next(it) == active_col.end())
{
int last = 1, bef = 1;
if (it != active_col.begin())
bef = *prev(it);
if (active_row.size() > 0)
last = *active_row.rbegin();
ans = ans + (ll)(*it - bef) * a[last];
}
else
{
int nxt = *next(it);
slope sl(nxt, 1, b[nxt] - b[cur.idx], nxt - cur.idx);
///if (dif.find(sl) == dif.end())
/// while(true);
dif.erase(dif.find(sl));
int bef = 1;
if (it != active_col.begin())
bef = *prev(it);
sl = slope(nxt, 1, b[nxt] - b[bef], nxt - bef);
dif.insert(sl);
}
active_col.erase(it);
}
}
cout << ans << endl;
}
int main()
{
speed();
solve();
return 0;
}
/**
8 9
3 8 1 11 15 23 6 7
18 9 2 5 14 8 6 30 17
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
122 ms |
10160 KB |
Output is correct |
5 |
Correct |
69 ms |
9556 KB |
Output is correct |
6 |
Correct |
37 ms |
4308 KB |
Output is correct |
7 |
Correct |
493 ms |
24556 KB |
Output is correct |
8 |
Correct |
460 ms |
24560 KB |
Output is correct |
9 |
Correct |
461 ms |
24524 KB |
Output is correct |
10 |
Correct |
452 ms |
24696 KB |
Output is correct |
11 |
Correct |
143 ms |
24396 KB |
Output is correct |
12 |
Correct |
477 ms |
24576 KB |
Output is correct |
13 |
Correct |
473 ms |
24576 KB |
Output is correct |
14 |
Correct |
229 ms |
24568 KB |
Output is correct |
15 |
Correct |
152 ms |
24652 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
0 ms |
324 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
122 ms |
10160 KB |
Output is correct |
33 |
Correct |
69 ms |
9556 KB |
Output is correct |
34 |
Correct |
37 ms |
4308 KB |
Output is correct |
35 |
Correct |
493 ms |
24556 KB |
Output is correct |
36 |
Correct |
460 ms |
24560 KB |
Output is correct |
37 |
Correct |
461 ms |
24524 KB |
Output is correct |
38 |
Correct |
452 ms |
24696 KB |
Output is correct |
39 |
Correct |
143 ms |
24396 KB |
Output is correct |
40 |
Correct |
477 ms |
24576 KB |
Output is correct |
41 |
Correct |
473 ms |
24576 KB |
Output is correct |
42 |
Correct |
229 ms |
24568 KB |
Output is correct |
43 |
Correct |
152 ms |
24652 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
328 KB |
Output is correct |
49 |
Correct |
0 ms |
324 KB |
Output is correct |
50 |
Correct |
0 ms |
332 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
328 KB |
Output is correct |
55 |
Correct |
130 ms |
11588 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
8 ms |
1364 KB |
Output is correct |
58 |
Correct |
33 ms |
3736 KB |
Output is correct |
59 |
Correct |
428 ms |
25732 KB |
Output is correct |
60 |
Correct |
488 ms |
25676 KB |
Output is correct |
61 |
Correct |
472 ms |
25804 KB |
Output is correct |
62 |
Correct |
415 ms |
25744 KB |
Output is correct |
63 |
Correct |
155 ms |
25576 KB |
Output is correct |
64 |
Correct |
457 ms |
25676 KB |
Output is correct |
65 |
Correct |
458 ms |
25752 KB |
Output is correct |
66 |
Correct |
181 ms |
25676 KB |
Output is correct |
67 |
Correct |
156 ms |
25872 KB |
Output is correct |