#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include #include #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(i64 i=0; i<(i64)(n); i++) #define repr(i,n) for(i64 i=(i64)(n)-1; i>=0; i--) const i64 INF = 1001001001001001001; const char* yn(bool x){ return x ? "Yes" : "No"; } template 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; } template using nega_queue = std::priority_queue,std::greater>; using Modint = atcoder::static_modint<998244353>; // #include "nachia/vec.hpp" using namespace std; namespace nachia{ template< class S, class F, S op(S l, S r), F composition(F f, F x), S mapping(F f, S x) > struct LazySegtree { private: struct Node { S s; F f; bool propagated; }; int N; int logN; int xN; std::vector A; void mapf(Node& a, F f){ a.propagated = false; a.f = composition(f, a.f); a.s = mapping(f, a.s); } void mergev(int i){ if(i int minLeft2(int r, E cmp, int a = 0, int b = 0, int i = -1){ static S x; if(i == -1){ a=0; b=N; i=1; x = A[0].s; } if(r <= a) return a; if(b <= r){ S nx = op(A[i].s, x); if(cmp(nx)){ x = nx; return a; } } if(b-a == 1) return b; spread(i); int q = minLeft2(r, cmp, (a+b)/2, b, i*2+1); if(q > (a+b)/2) return q; return minLeft2(r, cmp, a, (a+b)/2, i*2); } // bool cmp(S) template int maxRight2(int l, E cmp, int a = 0, int b = 0, int i = -1){ static S x; if(i == -1){ a=0; b=N; i=1; x = A[0].s; } if(b <= l) return b; if(l <= a){ S nx = op(x, A[i].s); if(cmp(nx)){ x = nx; return b; } } if(b - a == 1) return a; spread(i); int q = maxRight2(l, cmp, a, (a+b)/2, i*2); if(q < (a+b)/2) return q; return maxRight2(l, cmp, (a+b)/2, b, i*2+1); } public: LazySegtree() : N(0), logN(-1), xN(0){} LazySegtree(int n, S e, F id){ N=1; logN=0; xN=n; while(N& a, S e, F id) : LazySegtree(a.size(), std::move(e), std::move(id)){ for(std::size_t i=0; i=1; i--) mergev(i); } void set(int p, S x){ p += N; for(int d=logN; d; d--) spread(p >> d); A[p].s = x; for(int d=1; d<=logN; d++) mergev(p >> d); } S get(int p){ p += N; for(int d=logN; d; d--) spread(p >> d); return A[p].s; } void apply(int p, F f){ set(p, mapping(f, get(p))); } void apply(int l, int r, F f){ if(!(l < r)) return; if(l == 0 && r == N){ mapf(A[1], f); return; } l += N; r += N; for(int d=logN; d; d--){ if((l >> d) << d != l) spread(l >> d); if((r >> d) << d != r) spread(r >> d); } int lp = l, rp = r; while(l < r){ if(l&1){ mapf(A[l++], f); } l /= 2; if(r&1){ mapf(A[--r], f); } r /= 2; } for(int d=1 ; d<=logN; d++){ if((lp >> d) << d != lp) mergev(lp >> d); if((rp >> d) << d != rp) mergev(rp >> d); } } S prod(int l, int r){ if(!(l < r)) return A[0].s; l += N; r += N; for(int d=logN; d; d--){ if((l >> d) << d != l) spread(l >> d); if((r >> d) << d != r) spread(r >> d); } S q1 = A[0].s, q2 = A[0].s; while(l < r){ if(l&1){ q1 = op(q1, A[l++].s); } l /= 2; if(r&1){ q2 = op(A[--r].s, q2); } r /= 2; } return op(q1, q2); } S allProd() const { return A[1].s; } // bool cmp(S) template int minLeft(int r, E cmp){ return minLeft2(r, cmp); } // bool cmp(S) template int maxRight(int l, E cmp){ int x = maxRight2(l, cmp); return x > xN ? xN : x; } }; } // namespace nachia; i64 lst_chmin(i64 f, i64 x){ return min(f, x); } using RangeChminPointGet = nachia::LazySegtree; void testcase(){ int N, M; cin >> N >> M; RangeChminPointGet ds_ru(N, INF, INF); RangeChminPointGet ds_lu(N, INF, INF); auto getVal = [&](i64 i){ return min(ds_ru.get(i) + i, ds_lu.get(i) - i); }; auto applyVal = [&](i64 i, i64 x){ ds_ru.apply(i, N, x-i); ds_lu.apply(0, i+1, x+i); }; vector A(M); rep(i,M){ cin >> A[i]; A[i]--; } reverse(A.begin(), A.end()); applyVal(0, 0); for(int a : A){ auto qa = getVal(a); auto qb = getVal(a+1); ds_ru.set(a, INF); ds_lu.set(a, INF); ds_ru.set(a+1, INF); ds_lu.set(a+1, INF); if(a != 0) chmin(qb, getVal(a-1) + 1); if(a+2 < N) chmin(qa, getVal(a+2) + 1); applyVal(a, qb); applyVal(a+1, qa); } rep(i,N-1){ if(i) cout << ' '; cout << getVal(i+1); } cout << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); #ifdef NACHIA int T; cin >> T; for(int t=0; t