#include const int sup = 2000000001, inf = -sup; void chmin(int *a, int b) { if (*a > b) *a = b; } void chmax(int *a, int b) { if (*a < b) *a = b; } typedef struct { int left, right, max, la, lb; } lazy_seg_node; // Initialize a lazy-update segment tree between l (>= 0) to r (k = 1 for use) void init_node(lazy_seg_node v[], int k, int l, int r) { v[k].left = l; v[k].right = r; v[k].max = 0; v[k].la = 1; v[k].lb = 0; if (l < r) { init_node(v, k << 1, l, (l + r) / 2); init_node(v, (k << 1) ^ 1, (l + r) / 2 + 1, r); } } // Update the max values from v[k] to the root void update_max_to_root(lazy_seg_node v[], int k) { int j; for (j = k >> 1; j > 0; k = j, j >>= 1) v[j].max = (v[k].max > v[k^1].max)? v[k].max: v[k^1].max; } // Push the lazy update from v[k] to just below (if exists) void push_update_below(lazy_seg_node v[], int k) { int j; if (v[k].left == v[k].right || (v[k].la == 1 && v[k].lb == 0)) return; j = k << 1; v[j].max = v[j].max * v[k].la + v[k].lb; v[j].la *= v[k].la; v[j].lb = v[j].lb * v[k].la + v[k].lb; j ^= 1; v[j].max = v[j].max * v[k].la + v[k].lb; v[j].la *= v[k].la; v[j].lb = v[j].lb * v[k].la + v[k].lb; v[k].la = 1; v[k].lb = 0; } // Update the max values x between l and r to x * a + b (k = 1 for use) void update_segment(lazy_seg_node v[], int k, int l, int r, int a, int b) { if (r < v[k].left || v[k].right < l) return; else if (l <= v[k].left && v[k].right <= r) { v[k].max = v[k].max * a + b; v[k].la *= a; v[k].lb = v[k].lb * a + b; update_max_to_root(v, k); } else { push_update_below(v, k); update_segment(v, k << 1, l, r, a, b); update_segment(v, (k << 1) ^ 1, l, r, a, b); } } // Get the max value between l and r (k = 1 for use) int get_max(lazy_seg_node v[], int k, int l, int r) { int tmp[2]; if (r < v[k].left || v[k].right < l) return inf; else if (l <= v[k].left && v[k].right <= r) return v[k].max; else { push_update_below(v, k); tmp[0] = get_max(v, k << 1, l, r); tmp[1] = get_max(v, (k << 1) ^ 1, l, r); return (tmp[0] > tmp[1])? tmp[0]: tmp[1]; } } // Find the smallest index between l and r whose max value is at least x (k = 1 for use) int BS_left(lazy_seg_node v[], int k, int l, int r, int x) { int tmp; if (v[k].max < x || r < v[k].left || v[k].right < l) return r + 1; else if (v[k].left == v[k].right) return v[k].left; else { push_update_below(v, k); tmp = BS_left(v, k << 1, l, r, x); if (tmp <= r) return tmp; else return BS_left(v, (k << 1) ^ 1, l, r, x); } } // Find the largest index between l and r whose max value is at least x (k = 1 for use) int BS_right(lazy_seg_node v[], int k, int l, int r, int x) { int tmp; if (v[k].max < x || r < v[k].left || v[k].right < l) return l - 1; else if (v[k].left == v[k].right) return v[k].left; else { push_update_below(v, k); tmp = BS_right(v, (k << 1) ^ 1, l, r, x); if (tmp >= l) return tmp; else return BS_right(v, k << 1, l, r, x); } } // B should convex // min[l3-r3] will be min-plus convolution of (A[l1-r1], B[l2-r2]) void min_plus_convolution(int l1, int r1, int l2, int r2, int l3, int r3, int A[], int B[], int min[], int argmin[]) { if (l3 > r3) return; chmax(&l2, l3 - r1); chmin(&r2, r3 - l1); int i, j, k = (l3 + r3) / 2; for (i = l1, j = k - i, min[k] = sup; i <= r1 && j >= l2; i++, j--) { if (j > r2) continue; if (min[k] > A[i] + B[j]) { min[k] = A[i] + B[j]; argmin[k] = i; } } min_plus_convolution(l1, argmin[k], l2, r2, l3, k - 1, A, B, min, argmin); min_plus_convolution(argmin[k], r1, l2, r2, k + 1, r3, A, B, min, argmin); } void min_plus_convolution_naive(int n1, int n2, int A[], int B[], int min[], int argmin[]) { int i, j, k; for (k = 2; k <= n1 + n2; k++) min[k] = sup; for (i = 1; i <= n1; i++) { for (j = 1; j <= n2; j++) { k = i + j; if (min[k] > A[i] + B[j]) { min[k] = A[i] + B[j]; argmin[k] = i; } } } } int fix_argmin(int N, int A[], int B[], lazy_seg_node v[], int k) { int i = get_max(v, 1, k, k); if (i > 0) return i; int NN = N * 2 + 1, j, l, r, min, argmin; l = BS_right(v, 1, 1, k - 1, 1); r = BS_left(v, 1, k + 1, NN, 1); l = get_max(v, 1, l, l); r = get_max(v, 1, r, r); for (i = l, j = k - i, min = sup; i <= r && j >= 1; i++, j--) { if (j <= N && min > A[i] + B[j]) { min = A[i] + B[j]; argmin = i; } } update_segment(v, 1, k, k, 0, argmin); return argmin; } int main() { int i, N, Q, A[200001], B[200001]; scanf("%d %d", &N, &Q); for (i = 1; i <= N; i++) scanf("%d", &(A[i])); for (i = 1; i <= N; i++) scanf("%d", &(B[i])); int min[400001], argmin[400001]; min_plus_convolution(1, N, 1, N, 2, N * 2, A, B, min, argmin); int NN = N * 2 + 1; lazy_seg_node v[2][1048576]; init_node(v[0], 1, 1, NN); init_node(v[1], 1, 1, NN); for (i = 2; i <= NN - 1; i++) { update_segment(v[0], 1, i, i, 0, argmin[i]); update_segment(v[1], 1, i, i, 0, -argmin[i]); } update_segment(v[0], 1, 1, 1, 0, 1); update_segment(v[0], 1, NN, NN, 0, N); update_segment(v[1], 1, 1, 1, 0, -1); update_segment(v[1], 1, NN, NN, 0, -N); int p, x, k, l, r, m, ll, rr; while (Q--) { scanf("%d %d %d", &p, &x, &k); if (x > A[p]) { l = BS_right(v[1], 1, 1, NN, -(p - 1)); r = BS_left(v[0], 1, 1, NN, p + 1); if (l == 0) l++; if (r == NN + 1) r--; update_segment(v[0], 1, l + 1, r - 1, 0, inf); update_segment(v[1], 1, l + 1, r - 1, 0, sup); } else if (x < A[p]) { l = 2; r = NN - 1; while (l < r) { m = (l + r + 1) / 2; i = fix_argmin(N, A, B, v[0], m); if (i >= p || (i < p && 1 <= m - p && m - p <= N && x + B[m-p] < A[i] + B[m-i])) r = m - 1; else l = m; } ll = l; l = 2; r = NN - 1; while (l < r) { m = (l + r) / 2; i = fix_argmin(N, A, B, v[0], m); if (i <= p || (i > p && 1 <= m - p && m - p <= N && x + B[m-p] < A[i] + B[m-i])) l = m + 1; else r = m; } rr = r; update_segment(v[0], 1, ll + 1, rr - 1, 0, p); update_segment(v[1], 1, ll + 1, rr - 1, 0, -p); } A[p] = x; i = fix_argmin(N, A, B, v[0], k); update_segment(v[1], 1, k, k, 0, -i); printf("%d\n", A[i] + B[k-i]); fflush(stdout); } fflush(stdout); return 0; }