이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mid ((l + r) >> 1)
#define tl (node + 1)
#define tr (node + 2 * (mid - l + 1))
const int MAX = 1e6 + 25;
struct line {
int m = 1e10, b = 1e12;
int operator() (int x) { return m * x + b; }
};
vector <line> tree(MAX * 2);
void insert (int l, int r, line a, int node) {
if (l == r) {
if (tree[node](l) > a(l)) {
tree[node] = a;
}
return;
}
if (tree[node].m > a.m) swap(tree[node], a);
if (tree[node](mid) > a(mid)) {
swap(tree[node], a);
insert(mid + 1, r, a, tr);
} else {
insert(l, mid, a, tl);
}
}
int get (int l, int r, int a, int node) {
if (l == r) {
return tree[node](a);
}
if (a <= mid) return min(tree[node](a), get(l, mid, a, tl));
else return min(tree[node](a), get(mid + 1, r, a, tr));
}
int n;
const int MAXN = 1e5 + 25;
int a[MAXN], b[MAXN];
int dp[MAXN];
signed main () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
cin >> b[i];
b[i] += b[i - 1];
}
insert(0, MAX, {-2 * a[1], dp[1] + a[1] * a[1] - b[1]}, 1);
for (int i = 2; i <= n; i++) {
int val = get(0, MAX, a[i], 1);
dp[i] += val + a[i] * a[i] + b[i - 1];
insert(0, MAX, {-2 * a[i], dp[i] + a[i] * a[i] - b[i]}, 1);
}
cout << dp[n] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |