#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; } // 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 argmin[]) { if (l3 > r3) return; chmax(&l2, l3 - r1); chmin(&r2, r3 - l1); int i, j, k = (l3 + r3) / 2, min = sup; for (i = l1, j = k - i; i <= r1 && j >= l2; i++, j--) { if (j > r2) continue; if (min > A[i] + B[j]) { min = A[i] + B[j]; argmin[k] = i; } } min_plus_convolution(l1, argmin[k], l2, r2, l3, k - 1, A, B, argmin); min_plus_convolution(argmin[k], r1, l2, r2, k + 1, r3, A, B, argmin); } void solve(int N, int A[], int B[], int Q, int p[], int x[], int k[], int ans[]) { static int argmin[400001]; min_plus_convolution(1, N, 1, N, 2, N * 2, A, B, argmin); int h, i, j, NN = N * 2 + 1, head, tail; static int appear[200001], q[2501]; for (i = 1; i <= N; i++) appear[i] = 0; for (h = 1, tail = 0; h <= Q; h++) { A[p[h]] = x[h]; if (appear[p[h]] == 0) { appear[p[h]] = 1; q[tail++] = p[h]; } ans[h] = A[argmin[k[h]]] + B[k[h] - argmin[k[h]]]; for (head = 0; head < tail; head++) { i = q[head]; j = k[h] - i; if (1 <= j && j <= N) chmin(&(ans[h]), A[i] + B[j]); } if (tail == 5) { for (head = 0; head < tail; head++) appear[q[head]] = 0; tail = 0; min_plus_convolution(1, N, 1, N, 2, N * 2, A, B, argmin); } } } void naive(int N, int A[], int B[], int Q, int p[], int x[], int k[], int ans[]) { int q; static int min[400001], argmin[400001]; for (q = 1; q <= Q; q++) { A[p[q]] = x[q]; min_plus_convolution(1, N, 1, N, 2, N * 2, A, B, argmin); ans[q] = A[argmin[k[q]]] + B[k[q] - argmin[k[q]]]; } } #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])); int num[200001] = {}; for (i = 1; i <= Q; i++) num[p[i]]++; for (i = 1; i <= N; i++) if (num[i] >= 100) exit(-1); /* solve(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; } solve(N, A, B, Q, p, x, k, ans); for (i = 1; i <= Q; i++) printf("%d\n", ans[i]); */ /* static int AA[200001], BB[200001], anss[200001]; while (1) { for (i = 1; i <= N; i++) AA[i] = genrand() % 10; for (i = 1; i <= N; i++) BB[i] = (i - (N + 1) / 2) * (i - (N + 1) / 2); for (i = 1; i <= Q; i++) { p[i] = genrand() % N + 1; x[i] = genrand() % 10; k[i] = genrand() % (N * 2 - 1) + 2; } for (i = 1; i <= N; i++) { A[i] = AA[i]; B[i] = BB[i]; } solve(N, A, B, Q, p, x, k, ans); for (i = 1; i <= N; i++) { A[i] = AA[i]; B[i] = BB[i]; } naive(N, A, B, Q, p, x, k, anss); for (i = 1; i <= Q; i++) if (ans[i] != anss[i]) break; if (i <= Q) { for (i = 1; i <= N; i++) printf("%d ", AA[i]); printf("\n"); for (i = 1; i <= N; i++) printf("%d ", BB[i]); printf("\n"); for (i = 1; i <= Q; i++) printf("%d %d %d\n", p[i], x[i], k[i]); printf("\n"); for (i = 1; i <= Q; i++) printf("%d ", ans[i]); printf("\n"); for (i = 1; i <= Q; i++) printf("%d ", anss[i]); printf("\n"); break; } } */ fflush(stdout); return 0; }