提交时间:2023-08-14 13:58:44
运行 ID: 98270
#include<bits/stdc++.h> using namespace std; #ifdef IAKIOI #define cin fin ifstream cin("in.txt"); #endif #define ll long long constexpr int N = 2e5 + 5; int n, x[N], y[N]; ll f1[N], f2[N]; inline int f(int x, int y) { return abs(x) + abs(y); } struct BIT { ll t[N]; inline void add(int u, int v) { while (u <= n) { t[u] += v; u += u & -u; } } inline ll query(int u) { ll res = 0; while (u) { res += t[u]; u -= u & -u; } return res; } ll query(int l, int r) { return query(r) - query(l - 1); } }T1, T2; 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]; } for (int i = 1; i <= n; ++i) { T1.add(i, min(abs(x[i] - x[i - 1]), f(i - y[i - 1], x[i] - i)) + 1); T2.add(i, min(f(y[i] - i, i - x[i - 1]), abs(y[i] - y[i - 1])) + 1); } while (m--) { int op, a, b, c, d; cin >> op >> a >> b >> c; if (op == 1) { for (int i = a; i <= a + 1; ++i) { T1.add(i, -min(abs(x[i] - x[i - 1]), f(i - y[i - 1], x[i] - i)) + 1); T2.add(i, -min(f(y[i] - i, i - x[i - 1]), abs(y[i] - y[i - 1])) + 1); } x[a] = b, y[a] = c; for (int i = a; i <= a + 1; ++i) { T1.add(i, min(abs(x[i] - x[i - 1]), f(i - y[i - 1], x[i] - i)) + 1); T2.add(i, min(f(y[i] - i, i - x[i - 1]), abs(y[i] - y[i - 1])) + 1); } } else { cin >> d; if (max(a, b) > max(c, d)) swap(a, c), swap(b, d); if (max(a, b) == max(c, d)) { cout << f(a - c, b - d) << '\n'; } else { int t = max(a, b); ll s1 = min(f(t - a, x[t] - b), f(t - c, x[t] - d)) + 1, s2 = min(f(y[t] - a, t - b), f(y[t] - c, t - d)) + 1; s1 += T1.query(max(a, b) + 1, max(c, d) - 1), s2 += T2.query(max(a, b) + 1, max(c, d) - 1); int p = max(c, d); cout << min(s1 + f(p - c, x[p - 1] - d), s2 + f(y[p - 1] - c, p - d)) << '\n'; } } } return 0; }