#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; #include using Modint = atcoder::static_modint<998244353>; namespace nachia { template struct SmawkAlgorithm { template> static 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 SmawkAlgorithmIndependent( int height, int width, Func f ){ auto tmp = SmawkAlgorithm>::Solve( height, width, [](int r, int c){ return std::make_pair(r,c); }, [&f](std::pair l, std::pair r) -> bool { return f(l.first, l.second, r.second); } ); std::vector res(height); for(int i=0; i> N >> Q; vector A(N); rep(i,N) cin >> A[i]; vector AlastChange(N, 0); vector B(N*3, INF); rep(i,N) cin >> B[N+i]; vector ans(Q, INF); vector buf(N*2, INF); vector K(Q); auto sub = [&](int ql, int qr, const vector& J){ if(J.empty()) return; vector V; for(int i=ql; i().Solve( n, m, [&](int yz, int xz) -> i64 { return B[V[yz]-J[xz]+N] + A[J[xz]]; } ); rep(i,n) buf[V[i]] = q[i].first; for(int i=ql; i>> query(Q2*2); auto qadd = [&](int l, int r, int p, i64 x) -> void { //cout << "l = " << l << " , r = " << r << " , p = " << p << " , x = " << x << endl; l += Q2; r += Q2; while(l < r){ if(l % 2 == 1) query[l++].push_back({ p, x }); if(r % 2 == 1) query[--r].push_back({ p, x }); l /= 2; r /= 2; } }; rep(qi,Q){ int p; cin >> p; p--; i64 x; cin >> x; int k; cin >> k; K[qi] = k - 2; qadd(AlastChange[p], qi, p, A[p]); AlastChange[p] = qi; A[p] = x; } rep(i,N) qadd(AlastChange[i], Q, i, A[i]); rep(i,N) A[i] = INF; for(int d=1; d<=Q2; d*=2){ int st = Q2 / d; for(int l=0; l J; for(auto [u,v] : query[d+l]){ A[u] = v; J.push_back(u); } sort(J.begin(), J.end()); sub(st * l, st * (l+1), J); for(auto [u,v] : query[d+l]) A[u] = INF; } } rep(i,Q) cout << ans[i] << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }