結果

問題 No.2743 Twisted Lattice
ユーザー otoshigootoshigo
提出日時 2024-04-20 19:36:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,368 bytes
コンパイル時間 2,844 ms
コンパイル使用メモリ 221,456 KB
実行使用メモリ 45,596 KB
最終ジャッジ日時 2024-04-20 19:36:50
合計ジャッジ時間 7,747 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 501 ms
45,588 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/math>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

template <typename M>
struct segment_tree {
    using T = typename M::T;

public:
    explicit segment_tree() {}
    explicit segment_tree(int N) : segment_tree(std::vector<T>(N, M::e())) {}

    explicit segment_tree(const std::vector<T>& v) {
        int N = (int)v.size();
        n = N;
        size = 1;
        while (size < N) size <<= 1;
        data.resize(2 * size, M::e());
        for (int i = 0; i < N; i++) data[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) update(i);
    };

    void set(int p, T x) {
        assert(0 <= p && p < n);
        p += size;
        data[p] = x;
        while (p > 1) {
            p >>= 1;
            update(p);
        }
    }

    T get(int p) {
        assert(0 <= p && p < n);
        p += size;
        return data[p];
    }

    T prod(int l, int r) {
        assert(0 <= l && l <= r && r <= n);
        T vl = M::e(), vr = M::e();
        l += size;
        r += size;
        while (l < r) {
            if (l & 1) vl = M::op(vl, data[l++]);
            if (r & 1) vr = M::op(data[--r], vr);
            l >>= 1;
            r >>= 1;
        }
        return M::op(vl, vr);
    }

    T all_prod() { return data[1]; }

private:
    int n, size;
    std::vector<T> data;
    void update(int p) { data[p] = M::op(data[2 * p], data[2 * p + 1]); }
};

const ll INF=1LL<<60;

struct Monoid{
    using T=ll;
    static T op(T l,T r){return min(l,r);}
    static T e(){return INF;}
};

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll H,W;
    int N;
    cin>>H>>W>>N;
    vector<ll>A(N),B(N);
    unordered_map<ll,vector<ll>>V;
    unordered_map<ll,int>mp;
    vector<ll>I;
    for(int i=0;i<N;i++){
        cin>>A[i]>>B[i];
        A[i]--;
        B[i]--;
        V[B[i]].push_back(A[i]);
        I.push_back(B[i]);
    }
    sort(all(I));
    I.erase(unique(all(I)),I.end());
    int L=I.size();
    for(int i=0;i<L;i++)mp[I[i]]=i;
    segment_tree<Monoid>seg1(L),seg2(L);
    for(int i=0;i<N;i++){
        if(A[i]+B[i]<seg1.get(mp[B[i]]))seg1.set(mp[B[i]],A[i]+B[i]);
        if(A[i]-B[i]<seg2.get(mp[B[i]]))seg2.set(mp[B[i]],A[i]-B[i]);
    }
    for(int i=0;i<N;i++){
        ll ans=INF;
        for(int t=-1;t<=1;t++){
            if(!V.count(B[i]+t))continue;
            auto id=lower_bound(all(V[B[i]+t]),A[i]+1-abs(t));
            if(id!=V[B[i]+t].end())chmin(ans,abs(A[i]-*id)+abs(t)*(min(A[i],*id)+1));
            if(id!=V[B[i]+t].begin()){
                id--;
                if(t!=0){
                    chmin(ans,abs(A[i]-*id)+abs(t)*(min(A[i],*id)+1));
                }else{
                    if(id!=V[B[i]+t].begin()){
                        id--;
                        chmin(ans,abs(A[i]-*id)+abs(t)*(min(A[i],*id)+1));
                    }
                }
            }
        }
        chmin(ans,A[i]+seg1.prod(mp[B[i]]+1,L)-B[i]);
        chmin(ans,A[i]+seg2.prod(0,mp[B[i]])+B[i]);
        cout<<ans<<"\n";
    }
}
0