题解:CF1979E Manhattan Triangle

· · 题解

Solution

先转切比雪夫。

讨论取到 \max 的是 x 坐标还是 y 坐标,可以发现不可能三个都取同一个坐标。因此,一定是两个取 x 一个取 y,或者两个取 y 一个取 x。

考虑两个取 x 一个取 y,另一种情况交换 x,y 再跑一遍即可。

设 (A,B),(B,C) 两对取了 x 坐标,那么不可能 x_B=x_A-d,x_C=x_A+d, 不然 B,C 距离有 2d。因此有 x_B=x_C。

钦定 y_B<y_C,枚举 B,此时可以知道 C=(x_B,y_B+d)。那么合法的 A 条件就是 \lvert x_A-x_B\rvert =d\land y_A\in[x_B,x_C]。用 set 维护 s_i 表示 x_j=i 的点 j 的 y_j 的集合,枚举 x_A=x_B-d 还是 x_A=x_B+d,在 set 里二分看看有没有即可。

代码比较短。

Code

#include <bits/stdc++.h>
#define REP(i, l, r) for (int i = (l); i <= (r); ++ i)
#define DEP(i, r, l) for (int i = (r); i >= (l); -- i)
#define fi first
#define se second
#define pb emplace_back
#define mems(x, v) memset((x), (v), sizeof(x))
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
using namespace std;
namespace Milkcat {
    typedef long long LL;
    typedef pair<LL, LL> pii;
    const int N = 1e6 + 5;
    int n, d, x, y, A, B, C, a[N], b[N];
    map<int, set<int>> mp; map<pii, int> id;
    int main() {
        cin >> n >> d, A = B = C = 0;
        REP(i, 1, n) {
            cin >> x >> y;
            a[i] = x + y, b[i] = x - y;
        }
        REP(t, 0, 3) {
            int dx = (t & 1 ? -d : d);
            REP(i, 1, n)
                mp[a[i]].insert(b[i]), id[{a[i], b[i]}] = i;
            for (auto &[x, s] : mp) {
                if (!mp.count(x + dx)) continue;
                auto &t = mp[x + dx];
                for (int y : s) {
                    if (!s.count(y + d)) continue;
                    auto it = t.lower_bound(y);
                    if (it == t.end() || *it > y + d) continue;
                    A = id[{x, y}], B = id[{x, y + d}], C = id[{x + dx, *it}]; break;
                }
            }
            mp.clear(), id.clear();
            if (A > 0) break;
            if (t == 1) REP(i, 1, n) swap(a[i], b[i]);
        }
        cout << A << ' ' << B << ' ' << C << '\n'; 
        return 0;
    }
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int T = 1; cin >> T;
    while (T --) Milkcat::main();
    return 0;
}