結果
問題 | No.2988 Min-Plus Convolution Query |
ユーザー | 👑 Nachia |
提出日時 | 2024-12-13 01:05:38 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,435 ms / 2,000 ms |
コード長 | 5,260 bytes |
コンパイル時間 | 1,582 ms |
コンパイル使用メモリ | 101,600 KB |
実行使用メモリ | 105,484 KB |
最終ジャッジ日時 | 2024-12-13 01:06:11 |
合計ジャッジ時間 | 30,537 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 1,358 ms
104,988 KB |
testcase_03 | AC | 1,412 ms
104,980 KB |
testcase_04 | AC | 1,323 ms
104,244 KB |
testcase_05 | AC | 51 ms
13,944 KB |
testcase_06 | AC | 2 ms
6,816 KB |
testcase_07 | AC | 2 ms
6,816 KB |
testcase_08 | AC | 2 ms
6,816 KB |
testcase_09 | AC | 107 ms
16,828 KB |
testcase_10 | AC | 54 ms
10,768 KB |
testcase_11 | AC | 12 ms
6,816 KB |
testcase_12 | AC | 41 ms
7,424 KB |
testcase_13 | AC | 375 ms
35,440 KB |
testcase_14 | AC | 51 ms
8,064 KB |
testcase_15 | AC | 646 ms
58,088 KB |
testcase_16 | AC | 1,220 ms
96,900 KB |
testcase_17 | AC | 465 ms
46,604 KB |
testcase_18 | AC | 431 ms
44,724 KB |
testcase_19 | AC | 356 ms
38,560 KB |
testcase_20 | AC | 1,390 ms
104,384 KB |
testcase_21 | AC | 1,342 ms
104,660 KB |
testcase_22 | AC | 1,375 ms
104,636 KB |
testcase_23 | AC | 1,324 ms
104,824 KB |
testcase_24 | AC | 1,350 ms
105,028 KB |
testcase_25 | AC | 1,192 ms
104,948 KB |
testcase_26 | AC | 1,183 ms
104,724 KB |
testcase_27 | AC | 1,378 ms
104,400 KB |
testcase_28 | AC | 1,423 ms
105,012 KB |
testcase_29 | AC | 1,435 ms
104,748 KB |
testcase_30 | AC | 1,348 ms
105,220 KB |
testcase_31 | AC | 1,421 ms
105,484 KB |
testcase_32 | AC | 2 ms
6,816 KB |
testcase_33 | AC | 2 ms
6,816 KB |
testcase_34 | AC | 2 ms
6,820 KB |
testcase_35 | AC | 2 ms
6,820 KB |
testcase_36 | AC | 2 ms
6,816 KB |
testcase_37 | AC | 2 ms
6,816 KB |
testcase_38 | AC | 2 ms
6,820 KB |
testcase_39 | AC | 2 ms
6,816 KB |
testcase_40 | AC | 2 ms
6,816 KB |
testcase_41 | AC | 2 ms
6,816 KB |
ソースコード
#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include <iostream> #include <string> #include <vector> #include <algorithm> using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<int(n); i++) const i64 INF = 1001001001001001001; template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; } template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; #include <atcoder/modint> using Modint = atcoder::static_modint<998244353>; namespace nachia { template<class Elem> struct SmawkAlgorithm { template<class Gen, class Cmp = std::less<Elem>> static std::vector<std::pair<Elem, int>> Solve( int height, int width, Gen gen, Cmp cmp = Cmp() ){ if(height == 0) return std::vector<std::pair<Elem, int>>(0); auto reduce = [&](int yst, const std::vector<int>& cols) -> std::vector<int> { int w = int(cols.size()); std::vector<int> idx; int r = -1; for(int q=0; q<w; q++){ if(idx.empty()){ idx.push_back(q); r += yst; continue; } int a = cols[idx.back()]; int b = cols[q]; if(cmp(gen(r,a), gen(r,b))){ if(r+yst < height){ idx.push_back(q); r += yst; } } else { idx.pop_back(); q--; r -= yst; } } return idx; }; auto ysts = std::vector<int>(1,1); auto cols = std::vector<std::vector<int>>(1); for(int i=0; i<width; i++) cols[0].push_back(i); cols[0] = reduce(1, cols[0]); while(true){ int nxst = ysts.back() * 2; if(height < nxst) break; auto nxc = reduce(nxst, cols.back()); int w = nxc.size(); for(int i=0; i<w; i++) nxc[i] = cols.back()[nxc[i]]; cols.push_back(move(nxc)); ysts.push_back(nxst); } std::vector<std::pair<Elem,int>> 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<height; y+=st*2){ int r = y+st < height ? ans[y+st].second : width-1; ans[y] = std::make_pair(gen(y,x[p]), x[p]); while(p+1 < int(x.size()) && x[p+1] <= r){ int xp = x[++p]; auto fxp = gen(y,xp); if(!cmp(ans[y].first, fxp)) ans[y] = std::make_pair(fxp, xp); } } } return ans; } }; // bool Func(y, xl, xr) , xl < xr // returns if A[y][xl] < A[y][xr] template<class Func> std::vector<int> SmawkAlgorithmIndependent( int height, int width, Func f ){ auto tmp = SmawkAlgorithm<std::pair<int,int>>::Solve( height, width, [](int r, int c){ return std::make_pair(r,c); }, [&f](std::pair<int,int> l, std::pair<int,int> r) -> bool { return f(l.first, l.second, r.second); } ); std::vector<int> res(height); for(int i=0; i<height; i++) res[i] = tmp[i].first; return res; } } // namespace nachia void testcase(){ i64 N, Q; cin >> N >> Q; vector<i64> A(N); rep(i,N) cin >> A[i]; vector<int> AlastChange(N, 0); vector<i64> B(N*3, INF); rep(i,N) cin >> B[N+i]; rep(i,N) B[N-1-i] = (i64)10000000000 * i + 5000000000; rep(i,N) B[N*2+i] = (i64)10000000000 * i + 5000000000; vector<i64> ans(Q, INF); vector<i64> buf(N*2, INF); vector<int> K(Q); auto sub = [&](int ql, int qr, const vector<int>& J){ if(J.empty()) return; vector<int> V; int v_min = J.front(); int v_max = N - 1 + J.back(); for(int i=ql; i<qr; i++) if(v_min <= K[i] && K[i] <= v_max) V.push_back(K[i]); if(V.empty()) return; sort(V.begin(), V.end()); int n = V.size(); int m = J.size(); auto q = nachia::SmawkAlgorithm<i64>().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<qr; i++) if(v_min <= K[i] && K[i] <= v_max) chmin(ans[i], buf[K[i]]); }; int Q2 = 1; while(Q2<Q) Q2*=2; vector<vector<pair<i64,int>>> query(Q2*2); auto qadd = [&](int l, int r, int p, i64 x) -> void { 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<d; l++){ if(Q < st * (l+1)) continue; vector<int> 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); } } rep(i,Q) cout << ans[i] << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }