結果
問題 | No.2988 Min-Plus Convolution Query |
ユーザー | ygussany |
提出日時 | 2024-12-13 23:14:53 |
言語 | C (gcc 12.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 12,067 bytes |
コンパイル時間 | 768 ms |
コンパイル使用メモリ | 37,504 KB |
実行使用メモリ | 12,148 KB |
最終ジャッジ日時 | 2024-12-13 23:15:00 |
合計ジャッジ時間 | 6,793 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
ソースコード
#include <stdio.h> 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, 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].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].lb == 0) return; j = k << 1; v[j].max = v[k].lb; v[j].lb = v[k].lb; j ^= 1; v[j].max = v[k].lb; v[j].lb = v[k].lb; v[k].lb = 0; } // Update the max values x between l and r to b (k = 1 for use) void update_segment(lazy_seg_node v[], int k, int l, int r, 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 = b; v[k].lb = b; update_max_to_root(v, k); } else { push_update_below(v, k); update_segment(v, k << 1, l, r, b); update_segment(v, (k << 1) ^ 1, l, r, 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); i = l; j = k - i; if (j > N) { i += j - N; j -= j - N; } for (min = sup; i <= r && j >= 1; i++, j--) { if (min > A[i] + B[j]) { min = A[i] + B[j]; argmin = i; } } update_segment(v, 1, k, k, argmin); return argmin; } void fix_argmin_recursive(int N, int A[], int B[], lazy_seg_node v[][1048576], int l, int r) { if (l >= r || get_max(v[0], 1, r, r) - get_max(v[0], 1, l, l) < 1000) return; int i, m = (l + r) / 2; i = fix_argmin(N, A, B, v[0], m); update_segment(v[1], 1, m, m, -i); fix_argmin_recursive(N, A, B, v, l, m); fix_argmin_recursive(N, A, B, v, m, r); } void solve(int N, int A[], int B[], int Q, int p[], int x[], int k[], int ans[]) { static int min[400001], argmin[400001]; min_plus_convolution(1, N, 1, N, 2, N * 2, A, B, min, argmin); int i, NN = N * 2 + 1; static 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, argmin[i]); update_segment(v[1], 1, i, i, -argmin[i]); } update_segment(v[0], 1, 1, 1, 1); update_segment(v[0], 1, NN, NN, N); update_segment(v[1], 1, 1, 1, -1); update_segment(v[1], 1, NN, NN, -N); int q, l, r, m, ll, rr; for (q = 1; q <= Q; q++) { if (x[q] > A[p[q]]) { A[p[q]] = x[q]; l = BS_right(v[1], 1, 1, NN, -(p[q] - 1)); r = BS_left(v[0], 1, 1, NN, p[q] + 1); if (l == 0) l++; if (r == NN + 1) r--; update_segment(v[0], 1, l + 1, r - 1, inf); update_segment(v[1], 1, l + 1, r - 1, inf); fix_argmin_recursive(N, A, B, v, l, r); } else if (x[q] < A[p[q]]) { 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[q] || (i < p[q] && 1 <= m - p[q] && m - p[q] <= N && x[q] + B[m-p[q]] < 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[q] || (i > p[q] && 1 <= m - p[q] && m - p[q] <= N && x[q] + B[m-p[q]] < A[i] + B[m-i])) l = m + 1; else r = m; } rr = r; update_segment(v[0], 1, ll + 1, rr - 1, p[q]); update_segment(v[1], 1, ll + 1, rr - 1, -p[q]); A[p[q]] = x[q]; } i = fix_argmin(N, A, B, v[0], k[q]); update_segment(v[1], 1, k[q], k[q], -i); ans[q] = A[i] + B[k[q]-i]; } } 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 argargmin[][2]) { if (l3 > r3 || l1 + l2 > r3 || r1 + r2 < l3) return; if (l1 == r1) { chmin(&(argargmin[l1-l0+1][0]), l3); chmax(&(argargmin[l1-l0+1][1]), r3 + 1); return; } chmax(&l2, l3 - r1); chmin(&r2, r3 - l1); int i, j, k = (l3 + r3) / 2, min, argmin; for (i = l1, j = k - i, min = sup; i <= r1 && j >= l2; i++, j--) { if (j > r2) continue; 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(&(argargmin[argmin-l0+1][0]), k); chmax(&(argargmin[argmin-l0+1][1]), k + 1); min_plus_convolution2(l0, r0, l1, argmin, l2, r2, l3, k - 1, A, B, argargmin); min_plus_convolution2(l0, r0, argmin, r1, l2, r2, k + 1, r3, A, B, argargmin); } #define B_SIZE 100 #define B_NUM 2000 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], argargmin[B_NUM + 1][B_SIZE + 1][2]; 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; j <= r - l + 1; j++) { argargmin[i][j][0] = N * 2 + 1; argargmin[i][j][1] = 2; } argargmin[i][1][0] = 2; argargmin[i][r-l+1][1] = N * 2 + 1; min_plus_convolution2(l, r, l, r, 1, N, 2, N * 2, A, B, argargmin[i]); for (j = r - l; j >= 1; j--) chmin(&(argargmin[i][j][0]), argargmin[i][j+1][0]); for (j = 2; j <= r - l + 1; j++) chmax(&(argargmin[i][j][1]), argargmin[i][j-1][1]); /* for (j = 1; j <= r - l + 1; j++) printf("(%d %d) ", argargmin[i][j][0], argargmin[i][j][1]); printf("\n"); */ } n = i - 1; /* for (l = 2; l <= N * 2; l++) { for (i = 1, j = l - i; i <= N; i++, j--) { if (1 <= j && j <= N) printf("%11d ", A[i] + B[j]); else printf("%11d ", sup); } printf("\n"); } */ return; 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; j <= r - l + 1; j++) { argargmin[i][j][0] = N * 2 + 1; argargmin[i][j][1] = 2; } argargmin[i][1][0] = 2; argargmin[i][r-l+1][1] = N * 2 + 1; min_plus_convolution2(l, r, l, r, 1, N, 2, N * 2, A, B, argargmin[i]); for (j = r - l; j >= 1; j--) chmin(&(argargmin[i][j][0]), argargmin[i][j+1][0]); for (j = 2; j <= r - l + 1; j++) chmax(&(argargmin[i][j][1]), argargmin[i][j-1][1]); for (i = 1, ans[q] = sup; i <= n; i++) { l = 1; r = r_block[i] - l_block[i] + 1; while (l < r) { m = (l + r) / 2; if (argargmin[i][m][1] <= 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]); } } } 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, min, argmin); ans[q] = min[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])); solve2(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; }