結果

問題 No.2743 Twisted Lattice
ユーザー mealymealy
提出日時 2024-04-21 04:54:05
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 835 ms / 3,000 ms
コード長 3,331 bytes
コンパイル時間 6,907 ms
コンパイル使用メモリ 339,728 KB
実行使用メモリ 91,764 KB
最終ジャッジ日時 2024-04-21 04:54:21
合計ジャッジ時間 14,827 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 755 ms
91,764 KB
testcase_01 AC 280 ms
26,356 KB
testcase_02 AC 262 ms
26,024 KB
testcase_03 AC 793 ms
91,756 KB
testcase_04 AC 835 ms
91,760 KB
testcase_05 AC 824 ms
91,756 KB
testcase_06 AC 823 ms
91,760 KB
testcase_07 AC 797 ms
91,636 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using mint = atcoder::modint998244353;
//using mint = atcoder::modint1000000007;
using namespace atcoder;
using namespace std;

using ll = long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vector<int>;
using vm = vector<mint>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vpi = vector<pair<int,int>>;
using vpl = vector<pair<ll,ll>>;
#define rep(i,n) for (ll i = 0; i < n; i++)
#define replr(i,l,r) for (ll i = l; i < r; i++)
set<char> abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
const int inf =1e9;
const ll infl = 4e18;

template <typename T>
bool chmax(T &a, const T& b) {if (a < b) {a = b;  return true;}return false;}
template <typename T>
bool chmin(T &a, const T& b) {if (a > b) {a = b;  return true;}return false;}
template <typename T>
T max(vector<T> &v) {T m = 0;for (auto x : v) chmax(m, x);return m;}
template <typename T>
T min(vector<T> &v) {T m = inf;for (auto x : v) chmin(m, x);return m;}
template <typename T>
void print(vector<T> &v){for(auto x : v){cout<<x<<' ';}cout<<'\n';}
/*memo

*/
ll op(ll a, ll b){
    return min(a,b);
}
ll e(){
    return infl;
}
int main(){
    ll H,W,N; cin >> H >> W >> N;
    vpl ab(N);
    vector<tuple<ll,ll,int>> cd(N);
    rep(i,N){
        cin >> ab[i].first >> ab[i].second;
        get<0>(cd[i])=ab[i].second;
        get<1>(cd[i])=ab[i].first+ab[i].second;
        get<2>(cd[i])=i;
    }
    sort(cd.begin(),cd.end());
    vl dist(N,infl);
    segtree<ll,op,e> seg(N);
    rep(i,N){
        seg.set(i,get<1>(cd[i]));
    }
    rep(i,N){
        int ind=get<2>(cd[i]);
        ll a=ab[ind].first;
        ll b=ab[ind].second;
        chmin(dist[ind],seg.prod(i+1,N)+a-b-2);
    }
    rep(i,N){
        get<0>(cd[i])=ab[i].second;
        get<1>(cd[i])=ab[i].first-ab[i].second;
        get<2>(cd[i])=i;
    }
    sort(cd.begin(),cd.end(),greater<>());
    rep(i,N){
        seg.set(i,get<1>(cd[i]));
    }
    rep(i,N){
        int ind=get<2>(cd[i]);
        ll a=ab[ind].first;
        ll b=ab[ind].second;
        chmin(dist[ind],seg.prod(i+1,N)+a+b-2);
    }
    map<ll,vpl> mp;
    rep(i,N){
        mp[ab[i].second].push_back({ab[i].first,i});
    }
    for (auto [key,x] : mp){
        sort(mp[key].begin(),mp[key].end());
    }
    for(auto [key,x] : mp){
        rep(i,x.size()-1){
            ll a=x[i].first;
            ll b=x[i+1].first;
            ll ind1=x[i].second;
            ll ind2=x[i+1].second;
            chmin(dist[ind1],b-a);
            chmin(dist[ind2],b-a);
        }
    }
    map<ll,vpl> mp2=mp;
    for(auto [key,x] : mp2){
        for(auto [a,ind] : x){
            ll b=key+1;
            ll c=lower_bound(mp[b].begin(),mp[b].end(),make_pair(a,0LL))-mp[b].begin();
            if (c<mp[b].size()){
                chmin(dist[ind],a+mp[b][c].first-a);
                chmin(dist[mp[b][c].second],a+mp[b][c].first-a);
            }
            b=key-1;
            c=lower_bound(mp[b].begin(),mp[b].end(),make_pair(a,0LL))-mp[b].begin();
            if (c<mp[b].size()){
                chmin(dist[ind],a+mp[b][c].first-a);
                chmin(dist[mp[b][c].second],a+mp[b][c].first-a);
            }
        }
    }
    rep(i,N){
        cout<<dist[i]<<'\n';
    }
}
0