This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5, H = 1e6 + 5;
class Line {
public:
int m, c;
Line() {
m = 0;
c = 1e18;
}
Line(int m_, int c_) {
m = m_;
c = c_;
}
int evaluate(int x) {
return m * x + c;
}
};
class LiChaoTree {
Line *t;
int n;
public:
LiChaoTree(int n_) {
n = n_;
t = new Line[n * 4];
}
int insert(int v, int l, int r, Line nl) {
if (l == r)
return t[v] = nl.evaluate(l) < t[v].evaluate(l) ? nl : t[v], 1;
int mid = (l + r) / 2;
if (t[v].m > nl.m) swap(t[v], nl);
if (t[v].evaluate(mid) > nl.evaluate(mid))
swap(t[v], nl), insert(v * 2 + 1, mid + 1, r, nl);
else
insert(v * 2, l, mid, nl);
}
int query(int v, int l, int r, int x) {
if (l == r)
return t[v].evaluate(x);
int mid = (l + r) / 2;
if (x < mid)
return min(t[v].evaluate(x), query(v * 2, l, mid, x));
else
return min(t[v].evaluate(x), query(v * 2 + 1, mid + 1, r, x));
}
};
int h[N], w[N], dp[N];
int n, s;
int solveTestCase() {
cin >> n;
for (int i = 0; i < n; i++) cin >> h[i];
for (int i = 0; i < n; i++) cin >> w[i], s += w[i];
LiChaoTree lct(H);
dp[0] = -w[0];
lct.insert(1, 0, H, Line(-2 * h[0], dp[0] + h[0] * h[0]));
for (int i = 1; i < n; i++)
dp[i] = lct.query(1, 0, H, h[i]) + h[i] * h[i] - w[i], lct.insert(1, 0, H, Line(-2 * h[i], dp[i] + h[i] * h[i]));
cout << dp[n - 1] + s;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
// cin >> test;
while (test--)
solveTestCase();
}
Compilation message (stderr)
building.cpp: In function 'long long int solveTestCase()':
building.cpp:76:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
building.cpp: In member function 'long long int LiChaoTree::insert(long long int, long long int, long long int, Line)':
building.cpp:46:5: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |