#include using namespace std; int inf = 1e9; using SS = int; class DualSegmentTree{ //作用させるものの交換法則が成り立つ時&&lazy->lazyとlazy->datの作用が同じ時専用. //出来ないときは遅延セグ木 出来たとしても改良必須あり. public: int siz = 1; vector dat; void mapping(SS f,SS &x){x = min(f,x);} //FF composition(FF f, FF g){mappingと同じ?} チェック. SS e(){return inf;} void make(int N){ while(siz < N) siz *= 2; dat.resize(siz*2,e()); } void make2(int N,vector &A){ make(N); for(int i=0; i>= 1; r >>= 1; } } SS get(int pos){ pos = pos+siz; SS ret = dat.at(pos); while(pos != 1){ pos = pos/2; mapping(dat.at(pos),ret); } return ret; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int H,W; cin >> H >> W; vector S(H); for(auto &s : S) cin >> s; reverse(S.begin(),S.end()); vector> toL(H,vector(W)),toR(H,vector(W,W)); for(int i=0; i=0; k--){ if(S.at(i).at(k) == '#') toR.at(i).at(k) = k; else if(k != W-1) toR.at(i).at(k) = toR.at(i).at(k+1); } } vector dist(H); for(auto &d : dist) d.make(W); dist.at(0).update(0,W,0); for(int i=0; i next(W); priority_queue,vector>,greater>> Q; for(int k=0; k used(W); while(Q.size()){ auto [nowd,pos] = Q.top(); Q.pop(); if(used.at(pos)) continue; used.at(pos) = true; if(pos) if(S.at(i+1).at(pos-1) == '.' && next.at(pos-1) > nowd+1){ next.at(pos-1) = nowd+1; Q.push({nowd+1,pos-1}); } if(pos != W-1) if(S.at(i+1).at(pos+1) == '.' && next.at(pos+1) > nowd+1){ next.at(pos+1) = nowd+1; Q.push({nowd+1,pos+1}); } } DualSegmentTree change; change.make2(W,next); dist.at(i+1) = change; } for(int i=0; i