結果

問題 No.2743 Twisted Lattice
ユーザー KudeKude
提出日時 2024-04-21 11:45:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 3,000 ms
コード長 1,940 bytes
コンパイル時間 4,095 ms
コンパイル使用メモリ 304,860 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2024-04-21 11:45:14
合計ジャッジ時間 6,308 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
7,040 KB
testcase_01 AC 75 ms
7,168 KB
testcase_02 AC 73 ms
7,168 KB
testcase_03 AC 73 ms
7,168 KB
testcase_04 AC 86 ms
7,168 KB
testcase_05 AC 85 ms
7,040 KB
testcase_06 AC 81 ms
7,040 KB
testcase_07 AC 83 ms
7,040 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

struct P { int i, x, y; };

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, n;
  cin >> h >> w >> n;
  vector<P> ps(n);
  rep(i, n) {
    ps[i].i = i;
    cin >> ps[i].x >> ps[i].y;
  }
  sort(all(ps), [](const auto& lhs, const auto& rhs) {
    return lhs.y < rhs.y || (lhs.y == rhs.y && lhs.x < rhs.x);
  });
  constexpr long long INF = 1002003004005006007;
  VL ans(n, INF);
  rep(_, 2) {
    ll mx = -INF;
    for (auto [i, x, y] : ps) {
      chmin(ans[i], x - 1 + y - mx);
      chmax(mx, 0LL + y - (x - 1));
    }
    for (auto& p : ps) p.y = w - p.y;
    reverse(all(ps));
  }
  for (int l = 0, r = 1; l < n; l = r++) {
    while (r < n && ps[r].y == ps[l].y) r++;
    for (int i = l; i < r; i++) {
      if (i > l) chmin(ans[ps[i].i], 0LL + ps[i].x - ps[i-1].x);
      if (i + 1 < r) chmin(ans[ps[i].i], 0LL + ps[i+1].x - ps[i].x);
    }
    for (int i = l, j = r; j < n && ps[j].y == ps[l].y + 1; j++) {
      while (i + 1 < r && ps[i+1].x <= ps[j].x) i++;
      for (int i : {i, i + 1}) if (i < r) {
        ll dist = (ll)abs(ps[i].x - ps[j].x) + min(ps[i].x, ps[j].x);
        chmin(ans[ps[i].i], dist);
        chmin(ans[ps[j].i], dist);
      }
    }
  }
  for (ll x : ans) cout << x << '\n';
}
0