結果

問題 No.2743 Twisted Lattice
ユーザー kenken714kenken714
提出日時 2024-04-18 23:25:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,196 ms / 3,000 ms
コード長 1,880 bytes
コンパイル時間 3,171 ms
コンパイル使用メモリ 228,988 KB
実行使用メモリ 66,064 KB
最終ジャッジ日時 2024-04-18 23:25:37
合計ジャッジ時間 14,849 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,110 ms
66,060 KB
testcase_01 AC 756 ms
38,000 KB
testcase_02 AC 678 ms
38,512 KB
testcase_03 AC 1,163 ms
65,936 KB
testcase_04 AC 1,196 ms
65,932 KB
testcase_05 AC 1,129 ms
66,064 KB
testcase_06 AC 1,148 ms
66,060 KB
testcase_07 AC 1,162 ms
65,932 KB
testcase_08 AC 5 ms
9,728 KB
testcase_09 AC 5 ms
9,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define INF (ll)1ll << 60
#define ALL(n) n.begin(),n.end()
#define P pair<ll, ll>

vector<P> v;
map<ll, ll> to, fr;
map<P, ll> pos;
vector<ll> g[210000], ans(210000, INF);
priority_queue<P, vector<P>, greater<P>> qf, qb;
int main(){
    ll h, w, n;
    cin >> h >> w >> n;
    REP(i, n){
        ll a, b;
        cin >> a >> b;
        v.push_back(P(b, a));
        fr[b]++;
        pos[P(b, a)] = i;
    }
    ll itr = 0;
    for (auto& i : fr) {
        to[itr] = i.first;
        i.second = itr;
        itr++;
    }
    sort(ALL(v));
    REP(i, n) g[fr[v[i].first]].push_back(v[i].second);
    REP(i, 210000) sort(ALL(g[i]));
    REP(i, n) qb.push(P(v[i].second - 1 + v[i].first, fr[v[i].first]));
    REP(i, n){
        ll row = fr[v[i].first];
        //おなじれつ
        auto it = lower_bound(ALL(g[row]), v[i].second);
        if (it != g[row].begin()) ans[pos[v[i]]] = min(ans[pos[v[i]]], abs(*(it - 1) - v[i].second));
        if (it + 1 != g[row].end()) ans[pos[v[i]]] = min(ans[pos[v[i]]], abs(*(it + 1) - v[i].second));
        //となりれつ
        if (row >= 0 and to[row - 1] == v[i].first - 1 and g[row - 1].size()) ans[pos[v[i]]] = min(ans[pos[v[i]]], max(g[row - 1][0], v[i].second));
        if (to[row + 1] == v[i].first + 1 and g[row + 1].size()) ans[pos[v[i]]] = min(ans[pos[v[i]]], max(g[row + 1][0], v[i].second));
        //とおいれつ
        while(!qb.empty() and qb.top().second <= row) qb.pop();
        if (!qb.empty()) ans[pos[v[i]]] = min(ans[pos[v[i]]], qb.top().first - v[i].first + v[i].second - 1);
        if (!qf.empty()) ans[pos[v[i]]] = min(ans[pos[v[i]]], qf.top().first + v[i].first + v[i].second - 1);
        qf.push(P(v[i].second - 1 - v[i].first, fr[v[i].first]));
    }
    REP(i, n)cout << ans[i] << endl;
}
0