// 時間軸をセグ木状に分割統治 + monotone minima #include #include #include #include #include #include #include using namespace std; using ll = long long; const ll INF = LLONG_MAX / 4; using P = pair; bool chmin(ll& a, ll b) { return a > b ? a = b, 1 : 0; } char* buf; ll buf_ind; template struct small { typedef T value_type; small() {} template small(const U&) {} T* allocate(size_t n) { if (n & 7) { n &= -8; n += 8; } buf_ind -= n * sizeof(T); buf_ind &= 0 - alignof(T); assert(buf_ind >= 0); return (T*)(buf + buf_ind); } void deallocate(T*, size_t) {} }; using V = vector>; int main() { buf_ind = 3 << 28; buf = (char*)malloc(buf_ind); cin.tie(0)->sync_with_stdio(0); ll N, Q; cin >> N >> Q; vector A(N), B(N); for (ll& x : A) cin >> x; for (ll& x : B) cin >> x; vector> queries(Q); for (auto& [p, x, k] : queries) { cin >> p >> x >> k; p--; k -= 2; } // {L, R, i, a} vector> A_range; { vector L(N); for (ll t = 0; t < Q; t++) { auto [p, x, k] = queries[t]; A_range.push_back({L[p], t, p, A[p]}); L[p] = t; A[p] = x; } for (ll i = 0; i < N; i++) A_range.push_back({L[i], Q, i, A[i]}); } ranges::sort(A_range, {}, [](auto& x) { return x[2]; }); vector

k_query; // {k, t} for (ll t = 0; t < Q; t++) { auto [p, x, k] = queries[t]; k_query.push_back({k, t}); } ranges::sort(k_query); vector A_seg(Q * 2), C_seg(Q * 2); for (auto [L, R, i, a] : A_range) { L += Q; R += Q; for (; L < R; L >>= 1, R >>= 1) { if (L & 1) A_seg[L++].push_back({i, a}); if (R & 1) A_seg[--R].push_back({i, a}); } } for (auto [k, t] : k_query) { ll i = t + Q; do C_seg[i].push_back({k, t}); while (i >>= 1); } vector ans(Q, INF); auto solve = [&](const V& A, const V& C) { auto minima = [&](auto minima, ll x1, ll x2, ll y1, ll y2) { if (x1 > x2) return; const ll x3 = (x1 + x2) / 2; auto [k, t] = C[x3]; ll y3 = -1, mn = INF * 2; for (ll y = y1; y <= y2; y++) { auto [i, a] = A[y]; const ll j = k - i; if (chmin(mn, [&] { if (j < 0) return INF - j; if (j >= N) return INF + j; return a + B[j]; }())) y3 = y; } assert(y3 != -1); chmin(ans[t], mn); minima(minima, x1, x3 - 1, y1, y3); minima(minima, x3 + 1, x2, y3, y2); }; if (A.empty() || C.empty()) return; minima(minima, 0, size(C) - 1, 0, size(A) - 1); }; for (ll i = 1; i < Q * 2; i++) { solve(A_seg[i], C_seg[i]); } for (ll x : ans) cout << x << '\n'; }