結果
問題 | No.2095 High Rise |
ユーザー |
![]() |
提出日時 | 2022-10-07 23:04:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 667 ms / 2,000 ms |
コード長 | 1,838 bytes |
コンパイル時間 | 2,568 ms |
コンパイル使用メモリ | 210,052 KB |
最終ジャッジ日時 | 2025-02-07 23:52:35 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
#define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) v.begin(), v.end() typedef long long ll; #include <bits/stdc++.h> using namespace std; template <typename X> struct SegTree{ using FX=function<X(X,X)>;// X•X -> X となる関数の型 int n; FX fx; const X ex; vector<X> dat; SegTree(int n_,FX fx_,X ex_) : n(),fx(fx_),ex(ex_),dat(n_*4,ex_){ int x=1; while (n_>x){ x *= 2; } n=x; } void set(int i,X x){ dat[i+n-1]=x;} void build(){ for (int k=n-2;k>=0;k--) dat[k]=fx(dat[2*k+1],dat[2*k+2]); } void update(int i,X x){ i += n-1; dat[i]=x; while(i>0){ i=(i-1)/2; // parent dat[i]=fx(dat[i*2+1],dat[i*2+2]); } } X get(int i){return dat[i+n-1];} X all(){return dat[0];} X query(int a,int b){ return query_sub(a,b,0,0,n);} X query_sub(int a,int b,int k,int l,int r){ if(r<=a || b<=l){ return ex; }else if(a<=l && r<=b){ return dat[k]; }else{ X vl=query_sub(a,b,k*2+1,l,(l+r)/2); X vr=query_sub(a,b,k*2+2,(l+r)/2,r); return fx(vl,vr); } } }; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int n,m; cin>>n>>m; vector<vector<ll>> A(n,vector<ll>(m)); rep(i,n) rep(j,m) cin>>A[i][j]; if(n==1){ cout<<0<<endl; return 0; } using X=ll; auto fx=[](X x1,X x2)->X{ return min(x1,x2);}; X ex=1e18; SegTree<X> seg(m,fx,ex); rep(j,m) seg.set(j,A[0][j]); seg.build(); for(int i=1;i<n;i++){ SegTree<X> seg1(m,fx,ex); rep(j,m){ ll mi=1e18; if(j>0) mi=min(mi,seg.query(0,j)+A[i][j]+A[i-1][j]); if(j<m-1) mi=min(mi,seg.query(j,m)+A[i][j]+A[i-1][j]); mi=min(mi,seg.query(j,j+1)+A[i][j]); seg1.update(j,mi); } rep(j,m) seg.update(j,seg1.query(j,j+1)); } cout<<seg.all()<<endl; return 0; }