#include <bits/stdc++.h>
using namespace std;
#define taskname "mvu"
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int,int> pii;
#define all(v) v.begin(), v.end()
struct line {
ll a, b;
line() : a(0), b() {}
line (ll a, ll b) : a(a), b(b) {}
ll calc (ll x) { return a * x + b; }
ll slope() { return a; }
};
struct liChao {
vector<line> tr;
liChao() {}
liChao (int sz) : tr(sz + 1, line(0, LLONG_MAX)) {}
void update (line f, int l, int r) {
if (l > r) return;
int mid = (l + r) >> 1;
if (l == r) {
tr[mid] = (f.calc(l) < tr[mid].calc(l) ? f : tr[mid]);
return;
}
if (f.slope() < tr[mid].slope()) swap(tr[mid], f);
if (tr[mid].calc(mid) < f.calc(mid)) {
update(f, l, mid - 1);
}
else {
update(tr[mid], mid + 1, r);
tr[mid] = f;
}
}
ll query (int p, int l, int r) {
int mid = (l + r) >> 1;
ll cur = tr[mid].calc(p);
if (p == mid) return cur;
if (p < mid) return min(query(p, l, mid - 1), cur);
if (p > mid) return min(query(p, mid + 1, r), cur);
}
};
const int M = 1e6 + 1;
ll dp[M], h[M], w[M];
ll f1 (int k) {
return -2 * h[k];
}
ll f2 (int k) {
return dp[k] + h[k] * h[k] - w[k];
}
ll f3 (int k) {
return h[k] * h[k] + w[k - 1];
}
int main()
{
if(fopen(taskname".inp","r"))
{
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) {
cin >> w[i];
w[i] += w[i - 1];
}
liChao tree(M);
tree.update(line(f1(1), f2(1)), 0, M);
for (int i = 2; i <= n; i++) {
dp[i] = tree.query(h[i], 0, M) + f3(i);
tree.update(line(f1(i), f2(i)), 0, M);
}
cout << dp[n];
return 0;
}
Compilation message
building.cpp: In function 'int main()':
building.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:74:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp: In member function 'll liChao::query(int, int, int)':
building.cpp:51:5: warning: control reaches end of non-void function [-Wreturn-type]
51 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
18012 KB |
Output is correct |
2 |
Correct |
4 ms |
18012 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
6 ms |
18176 KB |
Output is correct |
5 |
Correct |
5 ms |
18012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
21592 KB |
Output is correct |
2 |
Correct |
32 ms |
21596 KB |
Output is correct |
3 |
Correct |
30 ms |
19892 KB |
Output is correct |
4 |
Correct |
29 ms |
20828 KB |
Output is correct |
5 |
Correct |
28 ms |
22488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
18012 KB |
Output is correct |
2 |
Correct |
4 ms |
18012 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
6 ms |
18176 KB |
Output is correct |
5 |
Correct |
5 ms |
18012 KB |
Output is correct |
6 |
Correct |
30 ms |
21592 KB |
Output is correct |
7 |
Correct |
32 ms |
21596 KB |
Output is correct |
8 |
Correct |
30 ms |
19892 KB |
Output is correct |
9 |
Correct |
29 ms |
20828 KB |
Output is correct |
10 |
Correct |
28 ms |
22488 KB |
Output is correct |
11 |
Correct |
36 ms |
21076 KB |
Output is correct |
12 |
Correct |
35 ms |
22560 KB |
Output is correct |
13 |
Correct |
29 ms |
21076 KB |
Output is correct |
14 |
Correct |
35 ms |
21076 KB |
Output is correct |
15 |
Correct |
31 ms |
20816 KB |
Output is correct |
16 |
Correct |
29 ms |
24148 KB |
Output is correct |
17 |
Correct |
27 ms |
20816 KB |
Output is correct |
18 |
Correct |
27 ms |
20836 KB |
Output is correct |