洛谷3976 【TJOI2015】旅游(树链剖分)

传送门

【题目分析】

考试3h30min,40min打完T2,就对着这道和T3发懵。。。。。。

讲道理昨天我看见这道题就丢了。。。。。

首先题意简直令人想打出题人,语文水平果然很重要。。。。

其实只能在这条路径上买一次宝石、卖一次宝石,所以就是求最大差值。

但这道题要考虑路径是有向的,所以将路径拆为u->lca和lca->v,query的时候直接传结构体就行了。。。

然后就是忘了这个加法有顺序的调了半天。。。qwq

还有。。。。注意最后与0取个max,不然会蹦负数。。。。。。

【代码~】

#include<bits/stdc++.h>
using namespace std;
const int MAXN=5e4+10;
const int MAXM=1e5+10;
const int INF=0x3f3f3f3f;

int n,q,cnt;
int a[MAXN];
int head[MAXN],depth[MAXN],siz[MAXN],fa[MAXN],son[MAXN],top[MAXN];
int nxt[MAXM],to[MAXM];
int dfn[MAXN],ys[MAXN],tot;
struct Tree{
	int l,r;
	int add;
	int maxx,minn;
	int lc,rc;
	Tree(int x=0){
		maxx=-INF,minn=INF;
		lc=rc=-INF;
	}
	friend inline Tree operator+(const Tree &a,const Tree &b){
		Tree c;
		c.l=a.l,c.r=b.r;
		c.maxx=max(a.maxx,b.maxx);
		c.minn=min(a.minn,b.minn);
		c.lc=max(a.lc,max(b.lc,a.maxx-b.minn));
		c.rc=max(a.rc,max(b.rc,b.maxx-a.minn));
		return c;
	}
}tr[MAXN<<2];

int Read(){
	int i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

void add(int x,int y){
	nxt[cnt]=head[x];
	head[x]=cnt;
	to[cnt]=y;
	cnt++;
}

void dfs1(int u,int f){
	siz[u]=1;
	for(int i=head[u];i!=-1;i=nxt[i]){
		int v=to[i];
		if(v==f)
		  continue;
		fa[v]=u;
		depth[v]=depth[u]+1;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])
		  son[u]=v;
	}
}

void dfs2(int u,int tp){
	top[u]=tp;
	dfn[u]=++tot;
	ys[tot]=u;
	if(!son[u])
	  return ;
	dfs2(son[u],tp);
	for(int i=head[u];i!=-1;i=nxt[i]){
		int v=to[i];
		if(v==son[u]||v==fa[u])
		  continue;
		dfs2(v,v);
	}
}

void push_up(int root){
	tr[root]=tr[root<<1]+tr[root<<1|1];
}

void push_now(int root,int key){
	tr[root].add+=key;
	tr[root].maxx+=key;
	tr[root].minn+=key;
}

void push_down(int root){
	if(tr[root].add){
		push_now(root<<1,tr[root].add);
		push_now(root<<1|1,tr[root].add);
		tr[root].add=0;
	}
}

void build(int root,int l,int r){
	tr[root].l=l,tr[root].r=r;
	if(l==r){
		tr[root].minn=tr[root].maxx=a[ys[l]];
		return ;
	}
	int mid=l+r>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	push_up(root);
}

void update(int root,int l,int r,int L,int R,int key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_now(root,key);
		return ;
	}
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  update(root<<1,l,mid,L,R,key);
	else{
		if(L>mid)
		  update(root<<1|1,mid+1,r,L,R,key);
		else
		  update(root<<1,l,mid,L,mid,key),update(root<<1|1,mid+1,r,mid+1,R,key);
	}
	push_up(root);
}

Tree query(int root,int l,int r,int L,int R){
	if(L<=l&&r<=R)
	  return tr[root];
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  return query(root<<1,l,mid,L,R);
	else{
		if(L>mid)
		  return query(root<<1|1,mid+1,r,L,R);
		else
		  return query(root<<1,l,mid,L,mid)+query(root<<1|1,mid+1,r,mid+1,R);
	}
}

int lca(int x,int y){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		x=fa[top[x]];
	}
	return depth[x]<depth[y]?x:y;
}

int querypath(int x,int y){
	Tree ans1,ans2;
	int lc=lca(x,y);
	while(top[x]!=top[lc]){
		ans1=query(1,1,n,dfn[top[x]],dfn[x])+ans1;
		x=fa[top[x]];
	}
	while(top[y]!=top[lc]){
		ans2=query(1,1,n,dfn[top[y]],dfn[y])+ans2;
		y=fa[top[y]];
	}
	if(y==lc)
	  ans1=query(1,1,n,dfn[lc],dfn[x])+ans1;
	else
	  ans2=query(1,1,n,dfn[lc],dfn[y])+ans2;
	return max(max(max(ans1.lc,ans2.rc),0),ans2.maxx-ans1.minn);
}

void updatepath(int x,int y,int key){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		update(1,1,n,dfn[top[x]],dfn[x],key);
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	update(1,1,n,dfn[y],dfn[x],key);
}

int main(){
	memset(head,-1,sizeof(head));
	n=Read();
	for(int i=1;i<=n;++i)
	  a[i]=Read();
	for(int i=1;i<n;++i){
		int x=Read(),y=Read();
		add(x,y),add(y,x);
	}
	dfs1(1,-1),dfs2(1,1);
	build(1,1,n);
	q=Read();
	while(q--){
		int x=Read(),y=Read(),k=Read();
		cout<<querypath(x,y)<<'\n';
		updatepath(x,y,k);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值