結果

問題 No.2743 Twisted Lattice
ユーザー ponjuiceponjuice
提出日時 2024-04-18 00:27:59
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,074 ms / 3,000 ms
コード長 1,857 bytes
コンパイル時間 2,631 ms
コンパイル使用メモリ 225,712 KB
実行使用メモリ 36,992 KB
最終ジャッジ日時 2024-10-09 18:13:39
合計ジャッジ時間 13,492 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ll h, w, n;
    cin >> h >> w >> n;
    vector<pair<ll,ll>> a(n);
    map<ll, set<ll>> as;

    for (int i = 0; i < n; i++) {
        cin >> a[i].second >> a[i].first;
        as[a[i].first].insert(a[i].second);
    }
    vector<ll> ans(n, 1e10);

    // case a[i].first == a[j].first
    for (int i = 0; i < n; i++) {
        auto itr = as[a[i].first].upper_bound(a[i].second);
        if (itr != as[a[i].first].end()) {
            ans[i] = min(ans[i], *itr - a[i].second);
        }

        itr = as[a[i].first].find(a[i].second);
        if (itr != as[a[i].first].begin()) {
            itr = prev(itr);
            ans[i] = min(ans[i], a[i].second - *itr);
        }
    }

    // case abs(a[i].first - a[j].first) == 1
    for (int i = 0; i < n; i++) {
        if (as.find(a[i].first - 1) != as.end()) {
            ans[i] = min(ans[i], max(a[i].second, *as[a[i].first - 1].begin()));
        }

        if (as.find(a[i].first + 1) != as.end()) {
            ans[i] = min(ans[i], max(a[i].second, *as[a[i].first + 1].begin()));
        }
    }

    // case abs(a[i].first - a[j].first) > 1
    vector<int> iot(n);
    iota(iot.begin(), iot.end(), 0);
    sort(iot.begin(), iot.end(), [&](int i, int j){
        return a[i] < a[j];
    });
    {// l to r
        ll b = 1e10;
        for (auto i : iot) {
            ans[i] = min(ans[i], a[i].first + a[i].second - 1 + b);
            b = min(b, a[i].second - 1 - a[i].first);
        }
    }
    {// r to l
        ll b = 1e10;
        reverse(iot.begin(), iot.end());
        for (auto i : iot) {
            ans[i] = min(ans[i], -a[i].first + a[i].second - 1 + b);
            b = min(b, a[i].second - 1 + a[i].first);
        }
    }

    
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
}
0