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
| #include <bits/stdc++.h> #define MAX_N 100000 #define INF 0x3f3f3f3f #define mid ((s+t)>>1) using namespace std; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, ru[MAX_N+5], rd[MAX_N+5], c[MAX_N+5]; struct node {int s, t, mi[2][2];} tr[MAX_N<<2]; node init(int p) { node ret; ret.s = ret.t = p; ret.mi[0][0] = INF, ret.mi[1][1] = min(ru[p], rd[p])+c[p]+c[p+1]; ret.mi[1][0] = ru[p]+rd[p]+c[p], ret.mi[0][1] = ru[p]+rd[p]+c[p+1]; return ret; } node operator + (const node &x, const node &y) { node ret; ret.s = x.s, ret.t = y.t; memset(ret.mi, INF, sizeof ret.mi); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) ret.mi[i][j] = min(ret.mi[i][j], x.mi[i][0]+y.mi[1][j]-c[x.t+1]), ret.mi[i][j] = min(ret.mi[i][j], x.mi[i][1]+y.mi[0][j]-c[x.t+1]), ret.mi[i][j] = min(ret.mi[i][j], x.mi[i][1]+y.mi[1][j]-c[x.t+1]); return ret; } void update(int v) {tr[v] = tr[v<<1]+tr[v<<1|1];} void build(int v, int s, int t) { if (s == t) {tr[v] = init(s); return;} build(v<<1, s, mid), build(v<<1|1, mid+1, t); tr[v] = tr[v<<1]+tr[v<<1|1]; } void modifyr(int v, int s, int t, int p) { if (s == t) {tr[v] = init(s); return;} if (p <= mid) modifyr(v<<1, s, mid, p); if (p >= mid+1) modifyr(v<<1|1, mid+1, t, p); tr[v] = tr[v<<1]+tr[v<<1|1]; } void modifyc(int v, int s, int t, int p) { if (s == t) {tr[v] = init(s); return;} if (p <= mid+1) modifyc(v<<1, s, mid, p); if (p >= mid+1) modifyc(v<<1|1, mid+1, t, p); tr[v] = tr[v<<1]+tr[v<<1|1]; } node query(int v, int s, int t, int l, int r) { if (s >= l && t <= r) return tr[v]; if (r <= mid) return query(v<<1, s, mid, l, r); if (l >= mid+1) return query(v<<1|1, mid+1, t, l, r); return query(v<<1, s, mid, l, r)+query(v<<1|1, mid+1, t, l, r); } int main() { read(n), read(m); for (int i = 1; i < n; i++) read(ru[i]); for (int i = 1; i < n; i++) read(rd[i]); for (int i = 1; i <= n; i++) read(c[i]); n--, build(1, 1, n); while (m--) { int opt; read(opt); if (opt == 1) { int l, r; read(l), read(r); if (l == r) printf("%d\n", c[l]); else { node res = query(1, 1, n, l, r-1); int ans = min(res.mi[0][0], res.mi[0][1]); ans = min(ans, min(res.mi[1][0], res.mi[1][1])); printf("%d\n", ans); } } else { int p, x; read(p), read(x); if (opt == 2) ru[p] = x, modifyr(1, 1, n, p); if (opt == 3) rd[p] = x, modifyr(1, 1, n, p); if (opt == 4) c[p] = x, modifyc(1, 1, n, p); } } return 0; }
|