結果

問題 No.2095 High Rise
ユーザー umezoumezo
提出日時 2022-10-07 23:04:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 582 ms / 2,000 ms
コード長 1,838 bytes
コンパイル時間 2,305 ms
コンパイル使用メモリ 212,588 KB
実行使用メモリ 10,972 KB
最終ジャッジ日時 2023-09-03 15:07:04
合計ジャッジ時間 7,214 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 63 ms
4,376 KB
testcase_18 AC 37 ms
4,376 KB
testcase_19 AC 8 ms
4,380 KB
testcase_20 AC 74 ms
4,384 KB
testcase_21 AC 138 ms
5,108 KB
testcase_22 AC 580 ms
10,972 KB
testcase_23 AC 579 ms
10,876 KB
testcase_24 AC 582 ms
10,932 KB
testcase_25 AC 571 ms
10,964 KB
testcase_26 AC 576 ms
10,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0