#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
pll operator-(pll a, pll b){
return mp(a.F - b.F, a.S - b.S);
}
ll cross(pll a, pll b){
return a.F * b.S - a.S * b.F;
}
struct info{
ll y, from, v;
};
ostream& operator<<(ostream& o, info i){
return o << '(' << i.y << ',' << i.from << ',' << i.v << ')';
}
int main(){
StarBurstStream
int n, m;
cin >> n >> m;
vector<pll> a(1), b(1);
for(int i = 1; i <= n; i++){
pll p; p.F = i;
cin >> p.S;
while(a.size() >= 3 && cross(a.back() - a[a.size() - 2], p - a[a.size() - 2]) <= 0){
a.pob;
}
a.eb(p);
}
for(int i = 1; i <= m; i++){
pll p; p.F = i;
cin >> p.S;
while(b.size() >= 3 && cross(b.back() - b[b.size() - 2], p - b[b.size() - 2]) <= 0){
b.pob;
}
b.eb(p);
}
//printv(a, cerr);
//printv(b, cerr);
vector<info> d;
n = a.size(); m = b.size();
n--; m--;
d.eb(info({2, 1, a[1].S}));
/*vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, MAX));
dp[1][1] = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(i == 1 && j == 1) continue;
dp[i][j] = min(dp[i - 1][j] + b[j].S * (a[i].F - a[i - 1].F), dp[i][j - 1] + a[i].S * (b[j].F - b[j - 1].F));
}
printv(dp[i], cerr);
}*/
auto calc = [&](int x, int y){
int l = 0, r = (int)d.size() - 1;
while(l < r){
int mid = (l + r + 1) / 2;
if(d[mid].y > y) r = mid - 1;
else l = mid;
}
info t = d[l];
ll p = t.v * (b[y].F - b[y - 1].F) + (b[y].S - b[y - 1].S) * (a[x].F - a[t.from].F);
return mp(p, b[y].F - b[y - 1].F);
};
for(int i = 2; i <= n; i++){
/*cerr << "before " << i << "\n";
for(int j = 2; j <= m; j++) cerr << calc(i, j) << " ";
cerr << "\n";*/
int l = 2, r = m;
while(l < r){
int mid = (l + r) / 2;
pll t = calc(i, mid);
if(t.F >= a[i].S * t.S) r = mid;
else l = mid + 1;
}
pll t = calc(i, l);
if(t.F >= a[i].S * t.S){
while(!d.empty() && d.back().y >= l) d.pob;
d.eb(info({l, i, a[i].S}));
}
/*cerr << "ok " << i << "\n";
for(int j = 2; j <= m; j++) cerr << calc(i, j) << " ";
cerr << "\n";
printv(d, cerr);*/
}
ll ans = b[1].S * (a[n].F - 1);
for(int i = 2; i <= m; i++){
pll t = calc(n, i);
ans += t.F;
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 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 |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
6 ms |
596 KB |
Output is correct |
5 |
Correct |
7 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
18 ms |
932 KB |
Output is correct |
8 |
Correct |
17 ms |
1108 KB |
Output is correct |
9 |
Correct |
16 ms |
980 KB |
Output is correct |
10 |
Correct |
16 ms |
900 KB |
Output is correct |
11 |
Correct |
14 ms |
980 KB |
Output is correct |
12 |
Correct |
16 ms |
964 KB |
Output is correct |
13 |
Correct |
15 ms |
1048 KB |
Output is correct |
14 |
Correct |
15 ms |
1016 KB |
Output is correct |
15 |
Correct |
16 ms |
1236 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
380 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
320 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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 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 |
328 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
6 ms |
596 KB |
Output is correct |
33 |
Correct |
7 ms |
588 KB |
Output is correct |
34 |
Correct |
3 ms |
340 KB |
Output is correct |
35 |
Correct |
18 ms |
932 KB |
Output is correct |
36 |
Correct |
17 ms |
1108 KB |
Output is correct |
37 |
Correct |
16 ms |
980 KB |
Output is correct |
38 |
Correct |
16 ms |
900 KB |
Output is correct |
39 |
Correct |
14 ms |
980 KB |
Output is correct |
40 |
Correct |
16 ms |
964 KB |
Output is correct |
41 |
Correct |
15 ms |
1048 KB |
Output is correct |
42 |
Correct |
15 ms |
1016 KB |
Output is correct |
43 |
Correct |
16 ms |
1236 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
316 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
316 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
320 KB |
Output is correct |
52 |
Correct |
1 ms |
380 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
320 KB |
Output is correct |
55 |
Correct |
11 ms |
1096 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
2 ms |
328 KB |
Output is correct |
58 |
Correct |
4 ms |
468 KB |
Output is correct |
59 |
Correct |
25 ms |
2116 KB |
Output is correct |
60 |
Correct |
24 ms |
1944 KB |
Output is correct |
61 |
Correct |
27 ms |
1748 KB |
Output is correct |
62 |
Correct |
25 ms |
1952 KB |
Output is correct |
63 |
Correct |
52 ms |
4680 KB |
Output is correct |
64 |
Correct |
24 ms |
2008 KB |
Output is correct |
65 |
Correct |
24 ms |
2156 KB |
Output is correct |
66 |
Correct |
23 ms |
2120 KB |
Output is correct |
67 |
Correct |
27 ms |
1432 KB |
Output is correct |