1 solutions
-
0
荷塘
我们注意到错解不优:
$$\begin{align*} &\max\{|x_i - x_j| + |y_i - y_j|\} \\ =& \max\{(x_i - x_j) + (y_i - y_j), (x_i - x_j) - (y_i - y_j), -(x_i - x_j) + (y_i - y_j), -(x_i - x_j) - (y_i - y_j)\} \\ =& \max\{(x_i + y_i) - (x_j + y_j), (x_i - y_i) - (x_j - y_j), (-x_i + y_i) - (-x_j + y_j), (-x_i - y_i) - (-x_j - y_j)\} \end{align*} $$于是我们分别维护区间内 的 即可。
对于旋转,相当于 ,相当于上述四者调换顺序,使用线段树,维护方式与算法 2 相同。
故时间复杂度为 。
算法 2
将曼哈顿距离转为切比雪夫距离:。
这样就是两个维度分别取 ,各自维护区间 。
旋转只有 0/1/2/3 次这 4 种状态,容易预处理,修改时可以打旋转标记,将线段树结点的信息改为 “再转 0/1/2/3 次”的 。
时间复杂度为 。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define ls x<<1 #define rs x<<1|1 #define lson x<<1,l,mid #define rson x<<1|1,mid+1,r #define ll long long #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) using namespace std; const int N = 2e5+5,inf = -1e9; int tag[N<<2],a[N][2],n,m; struct node { int x1,x2,x3,x4; friend node operator + (const node &x,const node &y) {return {max(x.x1,y.x1),max(x.x2,y.x2),max(x.x3,y.x3),max(x.x4,y.x4)};} }t[N<<2]; // x+y x-y -x+y -x-y (x,y)->(-y,x) inline void pushtag(int x) {node s = t[x];t[x] = {s.x2,s.x4,s.x1,s.x3};(tag[x] += 1) %= 4;} inline void pushdown(int x) {for(;tag[x];tag[x]--)pushtag(ls),pushtag(rs);} void build(int x,int l,int r) { if(l == r) { int y = a[l][0],z = a[l][1]; t[x] = {y+z,y-z,-y+z,-y-z}; return ; } int mid = l+r>>1; build(lson);build(rson); t[x] = t[ls]+t[rs]; } void modify(int x,int l,int r,int L,int R) { if(L <= l&&r <= R)return pushtag(x); int mid = l+r>>1;pushdown(x); if(L <= mid)modify(lson,L,R); if(mid < R)modify(rson,L,R); t[x] = t[ls]+t[rs]; } node query(int x,int l,int r,int L,int R) { if(L <= l&&r <= R)return t[x]; int mid = l+r>>1;pushdown(x); node s = {inf,inf,inf,inf}; if(L <= mid)s = query(lson,L,R); if(mid < R)s = s+query(rson,L,R); return s; } char buf[1<<21],*p1,*p2; inline int rd() { char c;int f = 1; while(!isdigit(c = getchar()))if(c=='-')f = -1; int x = c-'0'; while(isdigit(c = getchar()))x = x*10+(c^48); return x*f; } int main() { freopen("fish.in","r",stdin); freopen("fish.out","w",stdout); n = rd();m = rd(); for(int i = 1;i <= n;i++)a[i][0] = rd(),a[i][1] = rd(); build(1,1,n); while(m--) { int op = rd(),l = rd(),r = rd(); if(op == 1)modify(1,1,n,l,r); else { node s = query(1,1,n,l,r); printf("%d\n",max(s.x1+s.x4,s.x2+s.x3)); } } return 0; }
- 1
Information
- ID
- 168
- Time
- 1000ms
- Memory
- 512MiB
- Difficulty
- 9
- Tags
- (None)
- # Submissions
- 24
- Accepted
- 3
- Uploaded By