結果

問題 No.2743 Twisted Lattice
ユーザー butsurizukibutsurizuki
提出日時 2024-04-21 13:18:44
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 863 ms / 3,000 ms
コード長 1,663 bytes
コンパイル時間 4,224 ms
コンパイル使用メモリ 295,520 KB
実行使用メモリ 82,944 KB
最終ジャッジ日時 2024-04-21 13:19:01
合計ジャッジ時間 12,701 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 764 ms
82,944 KB
testcase_01 AC 731 ms
26,624 KB
testcase_02 AC 527 ms
26,496 KB
testcase_03 AC 790 ms
82,816 KB
testcase_04 AC 863 ms
82,816 KB
testcase_05 AC 848 ms
82,944 KB
testcase_06 AC 781 ms
82,816 KB
testcase_07 AC 815 ms
82,944 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using pl=pair<long long,long long>;

long long ds(pl a,pl b){
  if(abs(a.second-b.second)<=1){
    return abs(a.first-b.first)+abs(a.second-b.second)*min(a.first,b.first);
  }
  return a.first+b.first+abs(a.second-b.second);
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long h,w,n;
  cin >> h >> w >> n;
  map<long long,set<pl>> mp;
  vector<long long> x(n),y(n);
  vector<pl> p(n);

  for(long long i=0;i<n;i++){
    cin >> x[i] >> y[i];
    p[i]={x[i],y[i]};
    mp[y[i]].insert({x[i],i});
  }

  vector<long long> res(n,8e18);
  for(long long i=0;i<n;i++){
    mp[y[i]].erase({x[i],i});
    for(long long d=-1;d<=1;d++){
      long long cy=y[i]+d;
      if(mp[cy].empty()){continue;}

      auto it=mp[cy].lower_bound({x[i],-8e18});
      if(it!=mp[cy].end()){
        res[i]=min(res[i],ds(p[i],p[(*it).second]));
      }

      it=mp[cy].lower_bound({x[i],8e18});
      if(it!=mp[cy].begin()){
        it--;
        res[i]=min(res[i],ds(p[i],p[(*it).second]));
      }
    }
    mp[y[i]].insert({x[i],i});
  }

  // for(auto &nx : res){
  //   cout << nx << "\n";
  // }

  vector<pl> vp(n);
  for(long long i=0;i<n;i++){
    vp[i]={y[i],i};
  }
  sort(vp.begin(),vp.end());

  long long md=4e18;
  for(auto &nx : vp){
    res[nx.second]=min(res[nx.second],md+x[nx.second]+y[nx.second]-2);
    md=min(md,x[nx.second]-y[nx.second]);
  }

  md=4e18;
  reverse(vp.begin(),vp.end());
  for(auto &nx : vp){
    res[nx.second]=min(res[nx.second],md+x[nx.second]-y[nx.second]-2);
    md=min(md,x[nx.second]+y[nx.second]);
  }

  for(auto &nx : res){
    cout << nx << "\n";
  }
  return 0;
}
0