#include #include #include #include #include #include using namespace std; using ll = long long; const ll INF = LLONG_MAX / 4; bool chmin(ll& a, ll b) { return a > b ? a = b, 1 : 0; } struct A_type { int l, r, i, a; }; struct C_type { int k, t; }; void main2(const ll Q, vector A, vector B, vector C) { ranges::sort(A, {}, &A_type::i); ranges::sort(C, {}, &C_type::k); vector A_dfs[20]; vector C_dfs[20]; A_dfs[0] = move(A); C_dfs[0] = move(C); vector ans(Q, INF); auto update = [&](const vector& A, const vector& C) { auto minima = [&](auto minima, ll x1, ll x2, ll y1, ll y2) -> void { if (x1 > x2) return; const ll x3 = (x1 + x2) / 2; const auto [k, t] = C[x3]; ll mn = INF * 2, y3 = -1; for (ll y = y1; y <= y2; y++) { const auto [l, r, i, a] = A[y]; const ll j = k - i; if (chmin(mn, [&] { if (j < 0) return INF - j; if (j >= ssize(B)) 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); }; auto dfs = [&](auto dfs, int depth, int L, int R) -> void { auto& A = A_dfs[depth]; auto& C = C_dfs[depth]; if (A.empty() || C.empty()) return; // A から [L, R) subset [l, r) である要素を A_cover に移す static vector A_cover; A_cover.clear(); erase_if(A, [&](const A_type& a) { if (a.l > L || R > a.r) return 0; A_cover.push_back(a); return 1; }); assert(ranges::is_sorted(A_cover, {}, &A_type::i)); update(A_cover, C); if (R - L == 1) return; const int M = (L + R) / 2; auto& A2 = A_dfs[depth + 1]; auto& C2 = C_dfs[depth + 1]; A2.clear(); C2.clear(); ranges::copy_if(A, back_inserter(A2), [&](const A_type& a) { return a.l < M; }); erase_if(C, [&](const C_type& c) { if (M <= c.t) return 0; C2.push_back(c); return 1; }); dfs(dfs, depth + 1, L, M); A2.clear(); ranges::copy_if(A, back_inserter(A2), [&](const A_type& a) { return M < a.r; }); swap(C, C2); dfs(dfs, depth + 1, M, R); }; dfs(dfs, 0, 0, Q); for (ll x : ans) cout << x << '\n'; } int main() { 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; } vector A_range; vector L(N); for (ll t = 0; t < Q; t++) { auto [p, x, k] = queries[t]; A_range.emplace_back(L[p], t, p, A[p]); L[p] = t; A[p] = x; } for (ll i = 0; i < N; i++) A_range.emplace_back(L[i], Q, i, A[i]); vector C_query; for (ll t = 0; t < Q; t++) { auto [p, x, k] = queries[t]; C_query.emplace_back(k, t); } main2(Q, A_range, B, C_query); }