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 92 93 94 95 96 97 98 99 100 101 102
| #include <bits/stdc++.h> #define pb push_back const int N = 2e4 + 100, K = 100 + 100, INF = 0x3f3f3f3f; int n, num; int d[K][N], ans = INF; int c[N], dis[N], s[N], w[N], lp[N]; std::vector<int> cg[N]; namespace LT { #define lc (u << 1) #define rc ((u << 1) | 1) #define mid ((l + r) >> 1) int v[N << 2], tg[N << 2]; void up(int u){ v[u] = std::min(v[lc], v[rc]); } void adt(int u, int d){ tg[u] += d, v[u] += d; } void dw(int u) { if (tg[u]) { adt(lc, tg[u]), adt(rc, tg[u]); tg[u] = 0; } } void bd(int x[], int u = 1, int l = 0, int r = n) { v[u] = INF, tg[u] = 0; if (l == r) return void(v[u] = x[l]); bd(x, lc, l, mid), bd(x, rc, mid + 1, r); up(u); } void mdf(int ql, int qr, int d, int u = 1, int l = 0, int r = n) { if (l >= ql && r <= qr) return adt(u, d); dw(u); if (ql <= mid) mdf(ql, qr, d, lc, l, mid); if (qr > mid) mdf(ql, qr, d, rc, mid + 1, r); up(u); } int qry(int ql, int qr, int u = 1, int l = 0, int r = n) { if (l >= ql && r <= qr) return v[u]; dw(u); int res = INF; if (ql <= mid) res = qry(ql, qr, lc, l, mid); if (qr > mid) res = std::min(qry(ql, qr, rc, mid + 1, r), res); return res; } #undef lc #undef rc #undef mid } int findr(int x, int st) { int l = st, r = n, mid, res = n; while (l <= r) { mid = (l + r) >> 1; if (dis[mid] <= x) res = mid, l = mid + 1; else r = mid - 1; } return res; } int findl(int x, int st) { int l = 1, r = st, mid, res = st; while (l <= r) { mid = (l + r) >> 1; if (dis[mid] >= x) res = mid, r = mid - 1; else l = mid + 1; } return res; } int main() { scanf("%d %d", &n, &num); for (int i = 2; i <= n; ++i) scanf("%d", &dis[i]); for (int i = 1; i <= n; ++i) scanf("%d", &c[i]); for (int i = 1; i <= n; ++i) { scanf("%d", &s[i]); cg[findr(dis[i] + s[i], i)].pb(i); lp[i] = findl(dis[i] - s[i], i); } for (int i = 1; i <= n; ++i) scanf("%d", &w[i]); ++n, ++num; dis[n] = w[n] = INF, c[n] = 0; memset(d, 0x3f, sizeof d); d[0][0] = 0; for (int j = 1; j <= num; ++j) { LT::bd(d[j - 1]); for (int i = 1; i <= n; ++i) { d[j][i] = LT::qry(0, i - 1) + c[i]; for (int t : cg[i]) LT::mdf(0, lp[t] - 1, w[t]); } } for (int i = 0; i <= num; ++i) ans = std::min(ans, d[i][n]); printf("%d\n", ans); return 0; }
|