#include using namespace std; #define all(x) begin(x), end(x) #ifdef local #define safe cerr << __LINE__ << " line " << __LINE__ << " safe\n" #define debug(a...) debug_(#a, a) #define orange(a...) orange_(#a, a) template void debug_(const char *s, T ...a) { cerr << "\e[1;32m(" << s << ") = ("; int cnt = sizeof...(T); (..., (cerr << a << (--cnt ? ", " : ")\e[0m\n"))); } template void orange_(const char *s, I L, I R) { cerr << "\e[1;32m[ " << s << " ] = [ "; for (int f = 0; L != R; ++L) cerr << (f++ ? ", " : "") << *L; cerr << " ]\e[0m\n"; } #else #define safe ((void)0) #define debug(...) safe #define orange(...) safe #endif using lld = int64_t; namespace nachia { template struct SmawkAlgorithm { template> std::vector> Solve( int height, int width, Gen gen, Cmp cmp = Cmp() ){ if(height == 0) return std::vector>(0); auto reduce = [&](int yst, const std::vector& cols) -> std::vector { int w = int(cols.size()); std::vector idx; int r = -1; for(int q=0; q(1,1); auto cols = std::vector>(1); for(int i=0; i> ans(height, std::make_pair(gen(0,0), 0)); while(cols.size()){ auto x = std::move(cols.back()); cols.pop_back(); int st = ysts.back(); ysts.pop_back(); int p = 0; for(int y=st-1; y std::vector> ConvexMinPlusConvolution( const std::vector& a, const std::vector& b, Elem Inf ){ int n = a.size(); int m = b.size(); return nachia::SmawkAlgorithm().Solve( n+m-1, m, [&](int y, int x) -> Elem { return (y < x || n <= y-x) ? Inf : a[y-x] + b[x]; } ); } } // namespace nachia signed 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 = nachia::ConvexMinPlusConvolution(B, masked, ((lld)8e9) + 1); sort(all(ps)); ps.erase(unique(all(ps)), ps.end()); for (const auto &[p, x, k] : qs) { A[p] = x; lld ans = c[k].first; 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(); }