提交时间:2023-08-14 17:10:29

运行 ID: 98381

#include<bits/stdc++.h> using namespace std; #ifdef IAKIOI #define cin fin ifstream cin("in.txt"); #endif #define ll long long #define ls k << 1 #define rs k << 1 | 1 constexpr int N = 2e5 + 5; int n, x[N], y[N]; inline int f(int x, int y) { return abs(x) + abs(y); } struct Mat { ll a[3][3]; Mat() { memset(a, 0, sizeof a); } inline Mat operator * (const Mat &b) { Mat c; for (int i = 1; i <= 2; ++i) for (int j = 1; j <= 2; ++j) { ll res = LLONG_MAX; for (int k = 1; k <= 2; ++k) res = min(res, a[i][k] + b.a[k][j]); c.a[i][j] = res; } return c; } }mat[N], t[N << 2]; void build(int k = 1, int l = 1, int r = n - 1) { if (l == r) { t[k] = mat[l]; return; } int mid = l + r >> 1; build(ls, l, mid), build(rs, mid + 1, r); t[k] = t[ls] * t[rs]; } void update(int x, int k = 1, int l = 1, int r = n - 1) { if (l == r) { t[k] = mat[x]; return; } int mid = l + r >> 1; if (x <= mid) update(x, ls, l, mid); else update(x, rs, mid + 1, r); t[k] = t[ls] * t[rs]; } Mat query(int L, int R, int k = 1, int l = 1, int r = n - 1) { if (L <= l && r <= R) return t[k]; int mid = l + r >>1; if (!(R > mid)) return query(L, R, ls, l, mid); if (!(L <= mid)) return query(L, R, rs, mid + 1, r); return query(L, R, ls, l, mid) * query(L, R, rs, mid + 1, r); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int m; cin >> n >> m; for (int i = 1; i < n; ++i) { cin >> x[i] >> y[i]; mat[i].a[1][1] = abs(x[i] - x[i - 1]) + 1, mat[i].a[2][1] = f(i - y[i - 1], x[i] - i) + 1; mat[i].a[1][2] = f(y[i] - i, i - x[i - 1]) + 1, mat[i].a[2][2] = abs(y[i] - y[i - 1]) + 1; } build(); while (m--) { int op, a, b, c, d; cin >> op >> a >> b >> c; if (op == 1) { x[a] = b, y[a] = c; for (int i = a; i <= min(a + 1, n - 1); ++i) { mat[i].a[1][1] = abs(x[i] - x[i - 1]) + 1, mat[i].a[2][1] = f(i - y[i - 1], x[i] - i) + 1; mat[i].a[1][2] = f(y[i] - i, i - x[i - 1]) + 1, mat[i].a[2][2] = abs(y[i] - y[i - 1]) + 1; update(i); } } else { cin >> d; if (max(a, b) == max(c, d)) { cout << f(a - c, b - d) << '\n'; } else { if (max(a, b) > max(c, d)) swap(a, c), swap(b, d); int t = max(a, b); Mat m; m.a[1][1] = min(f(t - a, x[t] - b), f(t - a, x[t] - b)) + 1, m.a[1][2] = min(f(y[t] - a, t - b), f(y[t] - a, t - b)) + 1; m.a[2][1] = LLONG_MIN, m.a[2][2] = LLONG_MIN; if (max(c, d) - 1 >= max(a, b) + 1) m = m * query(max(a, b) + 1, max(c, d) - 1); int p = max(c, d); cout << min(m.a[1][1] + f(p - c, x[p - 1] - d), m.a[1][2] + f(y[p - 1] - c, p - d)) << '\n'; } } } return 0; }