#include const int sup = 2000000001; void chmin(int *a, int b) { if (*a > b) *a = b; } void chmax(int *a, int b) { if (*a < b) *a = b; } void min_plus_convolution2(int l0, int r0, int l1, int r1, int l2, int r2, int l3, int r3, int A[], int B[], int end_argmin[]) { /* fprintf(stderr, "[%d %d %d %d %d %d]\n", l1, r1, l2, r2, l3, r3); for (int i = 0; i <= r0 - l0 + 1; i++) fprintf(stderr, "%d ", end_argmin[i]); fprintf(stderr, "\n"); */ if (l3 > r3) return; if (l1 + l2 > r3) { chmin(&(end_argmin[l1-l0]), l3); return; } if (r1 + r2 < l3) { chmin(&(end_argmin[r1-l0]), r3); return; } if (l1 == r1) { chmin(&(end_argmin[l1-l0]), l3); return; } chmax(&l2, l3 - r1); chmin(&r2, r3 - l1); int i, j, k = (l3 + r3) / 2, min, argmin; i = l1; j = k - i; if (j > r2) { i += j - r2; j -= j - r2; } for (min = sup; i <= r1 && j >= l2; i++, j--) { if (min > A[i] + B[j]) { min = A[i] + B[j]; argmin = i; } } if (min == sup) { if (l1 + l2 <= r3) argmin = l1; else argmin = r1; } chmin(&(end_argmin[argmin-l0]), k); min_plus_convolution2(l0, r0, l1, argmin, l2, r2, l3, k - 1, A, B, end_argmin); min_plus_convolution2(l0, r0, argmin, r1, l2, r2, k + 1, r3, A, B, end_argmin); } #define B_SIZE 200 #define B_NUM 1000 void solve2(int N, int A[], int B[], int Q, int p[], int x[], int k[], int ans[]) { int i, j, l, r, n; static int l_block[B_NUM + 1], r_block[B_NUM + 1], end_argmin[B_NUM + 1][B_SIZE + 1]; for (i = 1, l = 1, r = B_SIZE; l <= N; i++, l += B_SIZE, r += B_SIZE) { chmin(&r, N); l_block[i] = l; r_block[i] = r; for (j = 1, end_argmin[i][0] = 2; j <= r - l + 1; j++) end_argmin[i][j] = N * 2 + 1; min_plus_convolution2(l, r, l, r, 1, N, 2, N * 2, A, B, end_argmin[i]); for (j = r - l; j >= 1; j--) chmin(&(end_argmin[i][j]), end_argmin[i][j+1]); // for (j = 0; j <= r - l + 1; j++) fprintf(stderr, "%d ", end_argmin[i][j]); // fprintf(stderr, "\n"); } n = i - 1; /* for (l = 2; l <= N * 2; l++) { fprintf(stderr, "%2d: ", l); for (i = 1, j = l - i; i <= N; i++, j--) { if (1 <= j && j <= N) fprintf(stderr, "%3d ", A[i] + B[j]); else fprintf(stderr, "100 "); } fprintf(stderr, "\n"); } */ int q, m; for (q = 1; q <= Q; q++) { A[p[q]] = x[q]; for (i = 1; r_block[i] < p[q]; i++); l = l_block[i]; r = r_block[i]; for (j = 1, end_argmin[i][0] = 2; j <= r - l + 1; j++) end_argmin[i][j] = N * 2 + 1; min_plus_convolution2(l, r, l, r, 1, N, 2, N * 2, A, B, end_argmin[i]); for (j = r - l; j >= 1; j--) chmin(&(end_argmin[i][j]), end_argmin[i][j+1]); // fprintf(stderr, "%d: ", i); // for (j = 0; j <= r - l + 1; j++) fprintf(stderr, "%d ", end_argmin[i][j]); // fprintf(stderr, "\n"); ans[q] = sup; l = 0; r = r_block[i] - l_block[i] + 1; while (l < r) { m = (l + r) / 2; if (end_argmin[i][m] <= k[q]) l = m + 1; else r = m; } l += l_block[i] - 1; if (1 <= k[q] - l && k[q] - l <= N) chmin(&(ans[q]), A[l] + B[k[q] - l]); for (i = 1, ans[q] = sup; i <= n; i++) { /* l = 0; r = r_block[i] - l_block[i] + 1; while (l < r) { m = (l + r) / 2; if (end_argmin[i][m] <= k[q]) l = m + 1; else r = m; } */ l = l_block[i]; if (1 <= k[q] - l && k[q] - l <= N) chmin(&(ans[q]), A[l] + B[k[q] - l]); } } } #define MT_N 624 #define MT_M 397 #define MT_MATRIX_A 0x9908b0dfUL #define MT_UPPER_MASK 0x80000000UL #define MT_LOWER_MASK 0x7fffffffUL static unsigned int mt[MT_N]; static int mti = MT_N + 1; void init_genrand(unsigned int s) { mt[0] = s & 0xffffffffUL; for (mti = 1; mti < MT_N; mti++) { mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); mt[mti] &= 0xffffffffUL; } } unsigned int genrand() { unsigned int y; static unsigned int mag01[2] = {0x0UL, MT_MATRIX_A}; if (mti >= MT_N) { int kk; if (mti == MT_N + 1) init_genrand(5489UL); for (kk = 0; kk < MT_N - MT_M; kk++) { y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK); mt[kk] = mt[kk+MT_M] ^ (y >> 1) ^ mag01[y&0x1UL]; } for (; kk < MT_N - 1; kk++) { y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK); mt[kk] = mt[kk+(MT_M-MT_N)] ^ (y >> 1) ^ mag01[y&0x1UL]; } y = (mt[MT_N-1] & MT_UPPER_MASK) | (mt[0] & MT_LOWER_MASK); mt[MT_N-1] = mt[MT_M-1] ^ (y >> 1) ^ mag01[y&0x1UL]; mti = 0; } y = mt[mti++]; y ^= (y >> 11); y ^= (y << 7) & 0x9d2c5680UL; y ^= (y << 15) & 0xefc60000UL; y ^= (y >> 18); return y; } int main() { int i, N, Q; static int A[200001], B[200001], p[200001], x[200001], k[200001], ans[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])); for (i = 1; i <= Q; i++) scanf("%d %d %d", &(p[i]), &(x[i]), &(k[i])); solve2(N, A, B, Q, p, x, k, ans); for (i = 1; i <= Q; i++) printf("%d\n", ans[i]); /* for (i = 1; i <= N; i++) A[i] = genrand() % 10; for (i = 1; i <= N; i++) B[i] = 0; for (i = 1; i <= Q; i++) { p[i] = genrand() % N + 1; x[i] = genrand() % 10; k[i] = genrand() % (N * 2 - 1) + 2; } solve2(N, A, B, Q, p, x, k, ans); for (i = 1; i <= Q; i++) printf("%d\n", ans[i]); */ fflush(stdout); return 0; }