题解:P3113 [USACO14DEC] Marathon G
P1
首先奶牛走的最短距离等于总距离减去奶牛最大能省去的距离(跳过某一个点少走的路的最大值),思考如何维护这两个东西。
tips:下文中用
当我们修改一个点的坐标(假设为
要求动态维护一个区间和与一个奇怪的区间最值,联想到线段树。为了方便考量,我们将边为线段树的元素(边
然后就没什么好说的了,其他的就是些神秘的细节问题。
Code
//注释的是debug,可以直接拿来用的[赞]
#include<bits/stdc++.h>
#define int long long
//#define DEBUG(x) cerr<<#x<<'='<<x<<endl
#define endl '\n'
using namespace std;
struct Throw_break{};
int rd(){
char t=getchar();int f=1,x=0;
while(t<'0'||t>'9'){ if(t=='-')f*=-1; t=getchar(); }
while(t>='0'&&t<='9') x=x*10+t-'0',t=getchar();
return x*f;
}void wt(int t){
if(t<0)putchar('-'),t=-t;
if(t>9)wt(t/10);
putchar(t%10+'0');
return;
}
const int N=1e5+100,INF=1e18;
int n,q;
struct node{int l,r,sum,maxx;};//不需要lazytag
struct XY{int x,y;}a[N];
//1-2-3-4-5-6.....n端点
// 1 2 3 4 5 6...m距离(node_i<=>len(i,i+1))
class line_tree{
private:
node tr[N<<2];
inline int len(int x,int y){return abs(a[x].y-a[y].y)+abs(a[x].x-a[y].x);}
node merge(node x,node y){ node res;
res.sum=x.sum+y.sum,res.maxx=max(x.maxx,y.maxx);
if(x.sum!=0&&y.sum!=0)res.maxx=max(res.maxx,len(x.r,y.l)+len(y.l,y.l+1)-len(x.r,y.l+1));
//上面图片中提到的那个贡献
//加if是为了防止query中的res调用时,res定义时自带的的两个端点产生贡献
res.l=min(x.l,y.l),res.r=max(x.r,y.r);
return res;}
void pushup(int rt){/*cerr<<"solving:"<<rt<<endl;*/return (void)(tr[rt]=merge(tr[rt<<1],tr[rt<<1|1]));}
public:
void build(int rt,int l,int r){ tr[rt].l=l,tr[rt].r=r;int mid=l+r>>1;
if(l==r) return (void)(tr[rt].sum=len(l,r+1),tr[rt].maxx=-INF);
build(rt<<1,l,mid),build(rt<<1|1,mid+1,r),pushup(rt);
return;}
void update(int rt,int l,int r){int mid=tr[rt].l+tr[rt].r>>1;
if(l<=tr[rt].l&&tr[rt].r<=r) return (void)(tr[rt].sum=len(tr[rt].l,tr[rt].r+1));
if(l<=mid)update(rt<<1,l,r); if(r>mid)update(rt<<1|1,l,r);
return pushup(rt);}
node query(int rt,int l,int r){ node res=(node){tr[rt].l,tr[rt].r,0,-INF};//如果直接把坐标设成特殊值就乱成一坨了(我太菜没想到好法子)
int mid=tr[rt].l+tr[rt].r>>1;
if(l<=tr[rt].l&&tr[rt].r<=r) {/*cerr<<"leaf:"<<rt<<' '<<tr[rt].l<<' '<<tr[rt].r<<' '<<tr[rt].sum<<' '<<tr[rt].maxx<<endl;*/return tr[rt];}
// cerr<<rt<<' '<<tr[rt].l<<' '<<tr[rt].r<<':'<<res.sum<<','<<res.maxx<<endl;
if(l<=mid&&r>mid) {res=merge(query(rt<<1,l,r),query(rt<<1|1,l,r));
/*cerr<<rt<<'|'<<tr[rt].l<<'|'<<tr[rt].r<<'|'<<res.sum<<','<<res.maxx<<endl;*/}
else if(l<=mid) {res=merge(res,query(rt<<1,l,r));
/*cerr<<rt<<'.'<<tr[rt].l<<'.'<<tr[rt].r<<':'<<res.sum<<','<<res.maxx<<endl;*/}
else if(r>mid) {res=merge(res,query(rt<<1|1,l,r));
/*cerr<<rt<<';'<<tr[rt].l<<';'<<tr[rt].r<<':'<<res.sum<<','<<res.maxx<<endl;*/}
return res;}
// void debug(){
// for(int i=1;i<=n;i++)cerr<<a[i].x<<' '<<a[i].y<<endl;cerr<<"--out--\n";
// for(int i=1;i<=n-1;i++)cerr<<i<<' '<<len(i,i+1)<<endl;
// cerr<<"---------\n";
// for(int i=1;i<=n-2;i++)cerr<<i<<' '<<len(i,i+2)<<endl;
// cerr<<"---------\n";
// return;}
}crr;
signed main(){
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
n=rd(),q=rd(); for(int i=1;i<=n;i++)a[i].x=rd(),a[i].y=rd();
crr.build(1,1,n-1);//crr.debug();
for(int i=1;i<=q;i++){char opt;scanf("%c",&opt);//cerr<<opt<<endl;
if(opt=='U'){ int u=rd(),x=rd(),y=rd();
a[u].x=x,a[u].y=y;
if(u!=n) crr.update(1,u,u);
if(u!=1) crr.update(1,u-1,u-1);
// cerr<<i<<endl;crr.debug();
}else{ int x=rd(),y=rd();//cerr<<"--begin query--\n";
node ans=crr.query(1,x,y-1);//cerr<<"--end query--\n";
// cerr<<ans.sum<<' '<<ans.maxx<<endl;
wt(ans.sum-ans.maxx),putchar(endl);
}
}
//fclose(stdin);
//fclose(stdout);
return "Ciallo~ (∠・ω< )⌒★",0;
}