#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; // https://judge.yosupo.jp/submission/247405 constexpr int msb(unsigned int n){return n==0?-1:31-__builtin_clz(n);} constexpr int msb(int n){return n==0?-1:31-__builtin_clz(n);} constexpr int msb(unsigned long long n){return n==0?-1:63-__builtin_clzll(n);} constexpr int msb(long long n){return n==0?-1:63-__builtin_clzll(n);} template std::vectormonotone_minima(int n,int m,const Func&f){ std::vectorres(n); std::vector>st(msb(n)*2+5); int p=0; st[p++]=std::make_tuple(0,n,0,m); while(p>0){ auto [lx,rx,ly,ry]=st[--p]; if(lx==rx)continue; if(lx+1==rx){ res[lx]=ly; for(int i=ly+1;i>1; res[mid]=ly; for(int i=ly+1;i std::vectormin_plus_convolution(const std::vector&a,const std::vector&b){ int n=a.size(),m=b.size(); auto f=[&](int i,int j,int k)->bool { if(i=n)return true; if constexpr(MIN)return a[i-j]+b[j]>a[i-k]+b[k]; else return a[i-j]+b[j]argmin=monotone_minima(n+m-1,m,f); std::vectorres(n+m-1); for(int i=0;isync_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(); }