提交时间:2023-08-14 17:41:25

运行 ID: 98399

#include<bits/stdc++.h> #define int long long #define ls (now<<1) #define rs (now<<1|1) #define mid ((l+r)>>1) using namespace std; const int N = 2e5+2; struct mat { int a[3][3],n; mat() {a[1][1] = a[1][2] = a[2][1] = a[2][2] = 1e18,n = 2; } }; mat mul(mat A,mat B) { mat ret; for(int i = 1;i <= 2;i++) for(int j = 1;j <= 2;j++) for(int k = 1;k <= 2;k++) ret.a[i][j] = min(A.a[i][k]+B.a[k][j],ret.a[i][j]); return ret; } mat t[N<<2]; int x[N],y[N]; void build(int now,int l,int r) { if(l == r) { mat A; A.a[1][1] = 1+abs(x[l]-x[l+1]); A.a[2][1] = 1+abs(y[l]-l-1)+abs(l+1-x[l+1]); A.a[1][2] = 1+abs(l+1-x[l])+abs(l+1-y[l+1]); A.a[2][2] = 1+abs(y[l]-y[l+1]); t[now] = A; return; } build(ls,l,mid); build(rs,mid+1,r); t[now] = mul(t[ls],t[rs]); } mat ask(int now,int l,int r,int x,int y) { if(l >= x && r <= y) return t[now]; if(mid < x) return ask(rs,mid+1,r,x,y); if(mid >= y) return ask(ls,l,mid,x,y); else return mul(ask(ls,l,mid,x,y),ask(rs,mid+1,r,x,y)); } void add(int now,int l,int r,int X) { if(l == r) { mat A; A.a[1][1] = 1+abs(x[l]-x[l+1]); A.a[2][1] = 1+abs(y[l]-l-1)+abs(l+1-x[l+1]); A.a[1][2] = 1+abs(l+1-x[l])+abs(l+1-y[l+1]); A.a[2][2] = 1+abs(y[l]-y[l+1]); t[now] = A; return; } if(mid >= X) add(ls,l,mid,X); else add(rs,mid+1,r,X); t[now] = mul(t[ls],t[rs]); } int f[N][2],n,m; int len(int x,int y,int xx,int yy) { return abs(x-xx)+abs(y-yy); } signed main() { scanf("%lld%lld",&n,&m); for(int i = 1;i < n;i++) scanf("%lld%lld",&x[i],&y[i]); if(n > 2) build(1,1,n-2); for(;m;m--) { int opt,a,b,c,d; scanf("%lld",&opt); if(opt == 1) { scanf("%lld%lld%lld",&a,&b,&c); x[a] = b; y[a]=c; if(a != n-1) add(1,1,n-2,a); add(1,1,n-2,a-1); continue; } scanf("%lld%lld%lld%lld",&a,&b,&c,&d); int s = max(a,b); int t = max(c,d); if(s > t) { swap(s,t); swap(a,c); swap(b,d); } if(s == t) { printf("%lld\n",len(a,b,c,d)); continue; } else if(s+1 == t) { printf("%lld\n",min(len(a,b,s,x[s])+1+len(t,x[t-1],c,d),len(a,b,y[s],s)+1+len(y[t-1],t,c,d))); continue; } mat A; A.a[1][1] = len(a,b,s,x[s]); A.a[1][2] = len(a,b,y[s],s); A = mul(A,ask(1,1,n-2,s,t-2)); int f0 = A.a[1][1],f1=A.a[1][2]; int ans = 1e9; ans = min(f0+1+len(t,x[t-1],c,d),f1+1+len(y[t-1],t,c,d)); printf("%lld\n",ans); } }