跳到正文

题解

题解:P3113 [USACO14DEC] Marathon G

qiliu约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年9月28日
收录
2026年9月30日
更新
2026年11月14日
篇幅
约 8 分钟
获赞
2
题目
P3113

版本

  1. v1当前

P1

首先奶牛走的最短距离等于总距离减去奶牛最大能省去的距离(跳过某一个点少走的路的最大值),思考如何维护这两个东西。

tips:下文中用 len⁡(i,j)\operatorname{len}(i,j) 表示标号为 ii 和 jj 的点间的距离。

当我们修改一个点的坐标(假设为 ii)的时候,会发生什么呢?第一会直接影响 len⁡(i−1,i)\operatorname{len}(i-1,i) 与 len⁡(i,i+1)\operatorname{len}(i,i+1)。然后就是对于我们的“奶牛最大能省去的距离”,这个值可能被更新成 len⁡(i−1,i)+len⁡(i,i+1)−len⁡(i−1,i)\operatorname{len}(i-1,i)+\operatorname{len}(i,i+1)-\operatorname{len}(i-1,i)。于是我们明确了要维护的量,现在只需找一个合适的结构。

要求动态维护一个区间和与一个奇怪的区间最值,联想到线段树。为了方便考量,我们将边为线段树的元素(边 ii 代表点 ii 与 i+1i+1 间的边)。于是区间和就很好求了,但是这个最值涉及两条边(端点即上文中的点 i−1i-1、ii 和 i+1i+1),所以当我们合并两棵子树的时候,需要将下面这种情况额外算上。

然后就没什么好说的了,其他的就是些神秘的细节问题。

Code

C++
//注释的是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;
}
原文
存档评论1 条抓取于