#include using namespace std; #define all(x) begin(x), end(x) #ifdef CKISEKI #define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n" #define debug(a...) debug_(#a, a) #define orange(a...) orange_(#a, a) #include void debug_(auto s, auto ...a) { cerr << "\e[1;32m(" << s << ") = ("; int f = 0; (..., (cerr << (f++ ? ", " : "") << a)); cerr << ")\e[0m\n"; } void orange_(auto s, auto L, auto R) { cerr << "\e[1;33m[ " << s << " ] = [ "; using namespace experimental; copy(L, R, make_ostream_joiner(cerr, ", ")); cerr << " ]\e[0m\n"; } #else #define safe ((void)0) #define debug(...) safe #define orange(...) safe #endif using lld = int64_t; // a is convex a[i+1]-a[i] <= a[i+2]-a[i+1] vector min_plus_convolution(auto &a, auto &b) { const int n = (int)a.size(), m = (int)b.size(); vector c(n + m - 1, numeric_limits::max()); auto dc = [&](auto Y, int l, int r, int jl, int jr) { if (l > r) return; int mid = (l + r) / 2, from = -1; lld &best = c[mid]; for (int j = jl; j <= jr; j++) if (int i = mid - j; i >= 0 && i < n) if (best > a[i]+b[j]) best = a[i]+b[j], from = j; Y(Y, l, mid-1, jl, from); Y(Y, mid+1, r, from, jr); }; return dc(dc, 0, n-1+m-1, 0, m-1), c; } int main() { cin.tie(nullptr)->sync_with_stdio(false); int N, Q; cin >> N >> Q; vector A(N + 1), B(N + 1); A[0] = B[0] = 4e9; for (int i = 1; i <= N; i++) cin >> A[i]; for (int i = 1; i <= N; i++) cin >> B[i]; vector> qs; auto calc = [&] { vector masked = A; vector ps; ps.reserve(qs.size()); for (const auto &[p, x, k] : qs) { masked[p] = 4e9; ps.emplace_back(p); } auto c = min_plus_convolution(B, masked); sort(all(ps)); ps.erase(unique(all(ps)), ps.end()); for (const auto &[p, x, k] : qs) { A[p] = x; lld ans = c[k]; debug(ans); orange(all(ps)); for (int i : ps) if (k - i >= 1 && k - i <= N) ans = min(ans, A[i] + B[k - i]); cout << ans << '\n'; } }; constexpr int BS = 600; for (int i = 0; i < Q; i++) { int p, x, k; cin >> p >> x >> k; qs.emplace_back(p, x, k); if (qs.size() > BS) { calc(); qs.clear(); } } calc(); }