F - Manhattan Christmas Tree 2

F - Manhattan Christmas Tree 2

Problem Statement

There are $N$ Christmas trees on a two-dimensional plane. The $i$-th $(1\le i\le N)$ Christmas tree is located at coordinates $(X_i,Y_i)$.

You are given $Q$ queries. Process the queries in order. Each query is one of the following:

  • Type $1$ : Given in the form 1 i x y. Change the coordinates of the $i$-th Christmas tree to $(x,y)$.
  • Type $2$ : Given in the form 2 L R x y. Output the Manhattan distance from the coordinates $(x,y)$ to the farthest Christmas tree among the $L,L+1,\ldots,R$-th Christmas trees.

Here, the Manhattan distance between coordinates $(x_1,y_1)$ and coordinates $(x_2,y_2)$ is defined as $|x_1-x_2|+|y_1-y_2|$.

Constraints

  • $1\le N,Q\le 2\times 10^5$
  • $-10^9\le X_i,Y_i\le 10^9$
  • $1\le i\le N$
  • $1\le L\le R\le N$
  • $-10^9\le x,y\le 10^9$
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

$N$ $Q$
$X_1$ $Y_1$
$X_2$ $Y_2$
$\vdots$
$X_N$ $Y_N$
$\text{query}_1$
$\text{query}_2$
$\vdots$
$\text{query}_Q$

Here, the $i$-th query $\text{query}_i$ is given in one of the following formats:

$1$ $i$ $x$ $y$

$2$ $L$ $R$ $x$ $y$

Output

Output the answers to the queries, separated by newlines, according to the instructions in the problem statement.


Sample Input 1

3 4
-1 -1
1 2
-2 1
2 1 2 0 0
2 1 3 -1 2
1 1 0 1
2 1 3 -1 2

Sample Output 1

3
3
2

Initially, the $1$st, $2$nd, $3$rd Christmas trees are located at coordinates $(-1,-1)$, $(1,2)$, $(-2,1)$, respectively.

Each query is processed as follows:

  • The Manhattan distances from the $1$st and $2$nd Christmas trees to coordinates $(0,0)$ are $2$ and $3$, respectively. Thus, output $3$, which is the maximum value among $2,3$.
  • The Manhattan distances from the $1$st, $2$nd, $3$rd Christmas trees to coordinates $(-1,2)$ are $3$, $2$, $2$, respectively. Thus, output $3$, which is the maximum value among $3,2,2$.
  • Change the coordinates of the $1$st Christmas tree to $(0,1)$. The coordinates of the $1$st, $2$nd, $3$rd Christmas trees become $(0,1),(1,2),(-2,1)$, respectively.
  • The Manhattan distances from the $1$st, $2$nd, $3$rd Christmas trees to coordinates $(-1,2)$ are $2$, $2$, $2$, respectively. Thus, output $2$, which is the maximum value among $2,2,2$.

Sample Input 2

5 7
-9 5
-2 -9
10 -6
9 8
2 9
1 3 -9 -6
2 3 4 2 7
1 4 -2 -10
2 1 2 0 -10
2 3 4 10 -9
2 3 4 8 7
2 5 5 0 2

Sample Output 2

24
24
22
30
9

 

解题思路

  赛时看到这题基本没什么思路,偶然间想到之前打 lc 的比赛时遇到过将曼哈顿距离转化为切比雪夫距离的技巧,于是去 OI Wiki 上回顾了一下,发现这题好像确实可以这样做。于是直接无脑抄了结论写代码,果然一发过了。此次记录主要是为了深入理解曼哈顿距离与切比雪夫距离相互转化的推导过程。

  首先回顾二维平面中两种距离的定义。对于二维空间中的两个点 $A \left( x_1, y_1 \right)$ 和 $B \left( x_2, y_2 \right)$,其曼哈顿距离距离为 $\parallel A-B \parallel_{1} = |x_1 - x_2| + |y_1 - y_2|$,切比雪夫距离为 $\parallel A-B \parallel_{\infty} = \max\left\{|x_1 - x_2|, |y_1 - y_2|\right\}$。下面证明通过坐标旋转变换,二者可以相互等价转换。

  对于曼哈顿距离 $|x_1 - x_2| + |y_1 - y_2|$,其等价于

\begin{aligned} 
&|x_1 - x_2| + |y_1 - y_2| \\
=&\max\{x_1 - x_2, \, x_2 - x_1\} + \max\{y_1 - y_2, \, y_2 - y_1\} \\
=&\max\{x_1 - x_2 + y_1 - y_2, \, x_1 - x_2 + y_2 - y_1, \, x_2 - x_1 + y_1 - y_2, \, x_2 - x_1 + y_2 - y_1 \} \\
=&\max\left\{\max\{x_1 - x_2 + y_1 - y_2, \, x_2 - x_1 + y_2 - y_1\}, \, \max\{x_1 - x_2 + y_2 - y_1, \, x_2 - x_1 + y_1 - y_2\}\right\} \\
=&\max\left\{\max\{(x_1 + y_1) - (x_2 + y_2), \, (x_2 + y_2) - (x_1 + y_1)\}, \, \max\{(x_1 - y_1) - (x_2 - y_2), \, (x_2 - y_2) - (x_1 - y_1)\}\right\} \\
=&\max\left\{|(x_1 + y_1) - (x_2 + y_2)|, \, |(x_1 - y_1) - (x_2 - y_2)|\right\} \\
\end{aligned}

  这正是点 $(x_1+y_1,x_1-y_1)$ 与 $(x_2+y_2,x_2-y_2)$ 在新坐标系下的切比雪夫距离。其几何意义为:将原坐标系绕原点顺时针旋转 $45^{\circ}$,再进行一次镜像翻转,最后拉伸 $\sqrt{2}$ 倍。对应的线性变换矩阵为 $\begin{pmatrix}1&1\\1&-1\end{pmatrix}$,即将点 $(x,y)$ 映射为 $(x+y,x-y)$。

  对于切比雪夫距离 $\max\left\{|x_1 - x_2|, |y_1 - y_2|\right\}$,利用结论 $\max\{|a|,|b|\}=\tfrac{|a+b|+|a-b|}{2}$(可通过对 $a,b$ 正负性分类讨论证明),可得

\begin{aligned} 
&\max\left\{|x_1 - x_2|, \, |y_1 - y_2|\right\} \\
=&\frac{|x_1 - x_2 + y_1 - y_2| + |x_1 - x_2 - y_1 + y_2|}{2} \\
=&\left|\frac{x_1 - x_2 + y_1 - y_2}{2}\right| + \left|\frac{x_1 - x_2 - y_1 + y_2}{2}\right| \\
=&\left|\frac{x_1 + y_1}{2} - \frac{x_2 + y_2}{2}\right| + \left|\frac{x_1 - y_1}{2} - \frac{x_2 - y_2}{2}\right| \\
\end{aligned}

  这正是点 $\left(\tfrac{x_1+y_1}{2}, \tfrac{x_1-y_1}{2}\right)$ 与 $\left(\tfrac{x_2+y_2}{2}, \tfrac{x_2-y_2}{2}\right)$ 的曼哈顿距离。变换矩阵为 $\begin{pmatrix}\tfrac{1}{2}&\tfrac{1}{2}\\\tfrac{1}{2}&-\tfrac{1}{2}\end{pmatrix}$。

  因此,将点 $(x,y)$ 映射为 $(x+y,x-y)$ 后,原曼哈顿距离等于新坐标系下的切比雪夫距离;反之,将点映射为 $\left(\frac{x+y}{2},\frac{x-y}{2}\right)$ 则原切比雪夫距离等于新坐标系下的曼哈顿距离

  本题要求区间内的圣诞树 $(x_i,y_i)$ 到查询点 $(x,y)$ 的最大曼哈顿距离,因此可以考虑将其转换为切比雪夫距离。将所有圣诞树坐标和查询点都变换为 $(x+y,x-y)$ 形式后,问题转化为求区间内点到查询点 $(x+y,x-y)$ 的最大切比雪夫距离,即

\begin{aligned}
&\max_{l\leq i \leq r}\{|x-x_i|+|y-y_i|\} \\
=&\max_{l\leq i \leq r}\{\max\{|(x+y)-(x_i+y_i)|, \, |(x-y)-(x_i-y_i)|\}\} \\
=&\max\left\{\max_{l\leq i \leq r}\{|(x+y)-(x_i+y_i)|\}, \, \max_{l\leq i \leq r}\{|(x-y)-(x_i-y_i)|\}\right\} \\
\end{aligned}

  对于 $\max_{l\leq i \leq r}\{|(x+y)-(x_i+y_i)|\}$,其最大值必然在 $x_i+y_i \left(l \leq i \leq r\right)$ 的最大值或最小值处取得(同理 $\max_{l\leq i \leq r}\{|(x-y)-(x_i-y_i)|\}$)。因此有

\begin{aligned}
&\max\left\{\max_{l\leq i \leq r}\{|(x+y)-(x_i+y_i)|\}, \, \max_{l\leq i \leq r}\{|(x-y)-(x_i-y_i)|\}\right\} \\
=&\max\left\{\left|(x+y) - \min_{l\leq i \leq r}\{x_i+y_i\}\right|, \, \left|(x+y) - \max_{l\leq i \leq r}\{x_i+y_i\}\right|, \, \left|(x-y) - \min_{l\leq i \leq r}\{x_i-y_i\}\right|, \, \left|(x-y) - \max_{l\leq i \leq r}\{x_i-y_i\}\right| \right\}
\end{aligned}

  因此我们只需用线段树动态维护四个值:区间内 $x_i + y_i$ 的最小值与最大值,以及区间内 $x_i - y_i$ 的最小值与最大值。然后单点修改,区间查询即可完成所有操作。

  AC 代码如下,时间复杂度为 $O(n + q\log{n})$:

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

typedef long long LL;

const int N = 2e5 + 5, INF = 2e9;

int x[N], y[N];
struct Node {
    int l, r, mnx, mxx, mny, mxy;
}tr[N * 4];

Node pushup(Node l, Node r) {
    return {l.l, r.r, min(l.mnx, r.mnx), max(l.mxx, r.mxx), min(l.mny, r.mny), max(l.mxy, r.mxy)};
}

void build(int u, int l, int r) {
    tr[u] = {l, r, INF, -INF, INF, -INF};
    if (l == r) {
        tr[u].mnx = tr[u].mxx = x[l];
        tr[u].mny = tr[u].mxy = y[l];
    }
    else {
        int mid = l + r >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        tr[u] = pushup(tr[u << 1], tr[u << 1 | 1]);
    }
}

void modify(int u, int t) {
    if (tr[u].l == tr[u].r) {
        tr[u].mnx = tr[u].mxx = x[t];
        tr[u].mny = tr[u].mxy = y[t];
    }
    else {
        int mid = tr[u].l + tr[u].r >> 1;
        if (t <= mid) modify(u << 1, t);
        else modify(u << 1 | 1, t);
        tr[u] = pushup(tr[u << 1], tr[u << 1 | 1]);
    }
}

Node query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return tr[u];
    int mid = tr[u].l + tr[u].r >> 1;
    if (r <= mid) return query(u << 1, l, r);
    if (l >= mid + 1) return query(u << 1 | 1, l, r);
    return pushup(query(u << 1, l, r), query(u << 1 | 1, l, r));
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> x[i] >> y[i];
        tie(x[i], y[i]) = make_tuple(x[i] + y[i], x[i] - y[i]);
    }
    build(1, 1, n);
    while (m--) {
        int op;
        cin >> op;
        if (op == 1) {
            int t, u, v;
            cin >> t >> u >> v;
            x[t] = u + v;
            y[t] = u - v;
            modify(1, t);
        }
        else {
            int l, r, x, y;
            cin >> l >> r >> x >> y;
            Node t = query(1, l, r);
            tie(x, y) = make_tuple(x + y, x - y);
            cout << max({abs((LL)x - t.mnx), abs((LL)x - t.mxx), abs((LL)y - t.mny), abs((LL)y - t.mxy)}) << '\n';
        }
    }
    
    return 0;
}

  补充:E. Maximum Monogonosity 这道题也用到了本题的思想,印象很深刻。

 

参考资料

  曼哈顿距离与切比雪夫距离的相互转化:https://oi-wiki.org/geometry/distance/#%E6%9B%BC%E5%93%88%E9%A1%BF%E8%B7%9D%E7%A6%BB%E4%B8%8E%E5%88%87%E6%AF%94%E9%9B%AA%E5%A4%AB%E8%B7%9D%E7%A6%BB%E7%9A%84%E7%9B%B8%E4%BA%92%E8%BD%AC%E5%8C%96

  Editorial - UNIQUE VISION Programming Contest 2025 Christmas (AtCoder Beginner Contest 437):https://atcoder.jp/contests/abc437/editorial/14897

posted @ 2025-12-22 16:21  onlyblues  阅读(3)  评论(0)    收藏  举报
Web Analytics