1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include<bits/stdc++.h> #define LL long long #define PII pair<int, int> using namespace std; template <class T> inline void read(T &res) { res = 0; bool flag = 0; char c = getchar(); while('0' > c || c > '9') { if(c == '-') flag = 1; c = getchar();} while('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar(); if(flag) res = -res; } template <class T, class ...Arg> inline void read(T &res, Arg &...com){ read(res), read(com...);} template <class T> void out(T res) { if(res > 9) out(res / 10); putchar(res % 10 + '0'); } template <class T> inline void write(T res) { if(res < 0) putchar('-'), res = -res; out(res), putchar(' '); } const int N = 1e5 + 5; const LL Inf = 1e18; const double esp = 1e-8; inline int com(double x, double y) { if(fabs(x - y) <= esp) return 0; if(x < y) return -1; return 1; } int n, m; struct Node{ LL h, sum, f, w, id; bool operator< (Node b) { return h < b.h; } }a[N]; int hh, tt, q[N]; inline double get_k(int x, int y) { LL x1 = 2 * a[x].h, x2 = 2 * a[y].h; LL y1 = a[x].f - a[x].sum + a[x].h * a[x].h, y2 = a[y].f - a[y].sum + a[y].h * a[y].h; if(x1 == x2) return y1 < y2 ? Inf : -Inf; return (double)(y2 - y1) / (double)(x2 - x1); } inline void add(int x) { while(hh < tt && com(get_k(q[tt], x), get_k(q[tt - 1], q[tt])) <= 0) tt --; q[++ tt] = x; } inline LL answer(int x, int y) { return (a[x].h - a[y].h) * (a[x].h - a[y].h) + a[x].sum - a[y].sum - a[x].w + a[y].f; } inline bool cmp_id(Node x, Node y) { return x.id < y.id; } void cdq(int l, int r) { if(l == r) return ; int mid = l + r >> 1; cdq(l, mid); sort(a + l, a + mid + 1), sort(a + mid + 1, a + r + 1); hh = 1, tt = 0; for(int i = l;i <= mid;i ++) add(i); for(int i = mid + 1;i <= r;i ++) { while(hh < tt && com(a[i].h, get_k(q[hh], q[hh + 1])) >= 0) hh ++; a[i].f = min(a[i].f, answer(i, q[hh])); } sort(a + mid + 1, a + r + 1, cmp_id); cdq(mid + 1, r); } int main() { read(n); for(int i = 1;i <= n;i ++) read(a[i].h), a[i].f = Inf, a[i].id = i; for(int i = 1;i <= n;i ++) read(a[i].w), a[i].sum = a[i - 1].sum + a[i].w; a[1].f = 0; cdq(1, n); write(a[n].f); return 0; }
|