#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int N_MAX = 1e5;
const int VALUE_MAX = 1e6;
int64 h[1 + N_MAX], w[1 + N_MAX];
int n; int64 pSum[1 + N_MAX];
void initInput (void) {
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> h[i];
for (int i = 1; i <= n; i ++)
cin >> w[i];
for (int i = 1; i <= n; i ++)
pSum[i] = pSum[i - 1] + w[i];
}
struct defLine {
int64 slope, h;
int64 getEval (int64 t) {return slope * t + h;}
};
const int64 myINF = 1e18;
struct LiChaoNode {
defLine line;
LiChaoNode* u;
LiChaoNode* v;
LiChaoNode () {
line = {0, +myINF};
u = NULL; v = NULL;
}
};
void update (LiChaoNode* root, int left, int right, defLine newLine) {
if (left == right) {
if (root -> line.getEval (left) > newLine.getEval (left))
swap (root -> line, newLine);
}
else {
int mid = left + (right - left) / 2;
if (root -> line.getEval (mid) > newLine.getEval (mid))
swap (root -> line, newLine);
if (root -> line.slope < newLine.slope) {
if (root -> u == NULL)
root -> u = new LiChaoNode;
update (root -> u, left, mid, newLine);
}
else {
if (root -> v == NULL)
root -> v = new LiChaoNode;
update (root -> v, mid + 1, right, newLine);
}
}
}
int64 query (LiChaoNode* root, int left, int right, int64 t) {
if (left == right)
return root -> line.getEval (t);
int mid = left + (right - left) / 2;
int64 answer = root -> line.getEval (t);
if (t <= mid && root -> u != NULL)
answer = min (answer, query (root -> u, left, mid, t));
else if (root -> v != NULL)
answer = min (answer, query (root -> v, mid + 1, right, t));
return answer;
}
int64 DP[1 + N_MAX];
void solve (void) {
LiChaoNode* CHT = new LiChaoNode;
DP[1] = 0; update (CHT, 0, VALUE_MAX, {-2 * h[1], DP[1] + h[1] * h[1] - w[1]});
for (int i = 2; i <= n; i ++) {
DP[i] = h[i] * h[i] + pSum[i - 1] + query (CHT, 0, VALUE_MAX, h[i]);
defLine newLine = {-2 * h[i], DP[i] + h[i] * h[i] - pSum[i]};
update (CHT, 0, VALUE_MAX, newLine);
///cout << DP[i] << "\n";
}
cout << DP[n];
}
int main()
{
initInput ();
solve ();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
5220 KB |
Output is correct |
2 |
Correct |
61 ms |
5188 KB |
Output is correct |
3 |
Correct |
61 ms |
5256 KB |
Output is correct |
4 |
Correct |
54 ms |
4588 KB |
Output is correct |
5 |
Correct |
57 ms |
11816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
59 ms |
5220 KB |
Output is correct |
7 |
Correct |
61 ms |
5188 KB |
Output is correct |
8 |
Correct |
61 ms |
5256 KB |
Output is correct |
9 |
Correct |
54 ms |
4588 KB |
Output is correct |
10 |
Correct |
57 ms |
11816 KB |
Output is correct |
11 |
Correct |
92 ms |
12800 KB |
Output is correct |
12 |
Correct |
78 ms |
12500 KB |
Output is correct |
13 |
Correct |
74 ms |
6836 KB |
Output is correct |
14 |
Correct |
79 ms |
12732 KB |
Output is correct |
15 |
Correct |
58 ms |
17944 KB |
Output is correct |
16 |
Correct |
62 ms |
11780 KB |
Output is correct |
17 |
Correct |
53 ms |
4468 KB |
Output is correct |
18 |
Correct |
48 ms |
4424 KB |
Output is correct |