http://acm.hdu.edu.cn/showproblem.php?pid=5052
和之前做得求链上连续段数很像 但是这道题难在要考虑方向问题 写起来很恶心
从u和v两边往上爬 在某一边每爬一小段(线段树上一个区间 需要区分方向) 就看这一小段上的利润最大值 再和两边已经走过部分的最大最小值做差取最优
#include <bits/stdc++.h>
using namespace std;
const int N=0x3f3f3f3f;
struct node1
{
int v;
int next;
};
struct node2
{
int l;
int r;
int laz;
int minn;
int maxx;
int val0;//left
int val1;//right
};
node1 edge[100010];
node2 tree[200010];
int val[50010],first[50010],fa[50010],deep[50010],sum[50010],son[50010],top[50010],mp1[50010],mp2[50010];
int n,q,num;
void addedge(int u,int v)
{
edge[num].v=v;
edge[num].next=first[u];
first[u]=num++;
}
void dfsI(int cur)
{
int i,v;
sum[cur]=1,son[cur]=-1;
for(i=first[cur];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa[cur])
{
fa[v]=cur,deep[v]=deep[cur]+1;
dfsI(v);
sum[cur]+=sum[v];
if(son[cur]==-1||sum[son[cur]]<sum[v])
{
son[cur]=v;
}
}
}
}
void dfsII(int cur,int tp)
{
int i,v;
num++;
top[cur]=tp,mp1[cur]=num,mp2[num]=cur;
if(son[cur]==-1) return;
dfsII(son[cur],tp);
for(i=first[cur];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa[cur]&&v!=son[cur])
{
dfsII(v,v);
}
}
}
void pushup(int cur)
{
tree[cur].minn=min(tree[2*cur].minn,tree[2*cur+1].minn);
tree[cur].maxx=max(tree[2*cur].maxx,tree[2*cur+1].maxx);
tree[cur].val0=max(max(0,tree[2*cur].maxx-tree[2*cur+1].minn),max(tree[2*cur].val0,tree[2*cur+1].val0));
tree[cur].val1=max(max(0,tree[2*cur+1].maxx-tree[2*cur].minn),max(tree[2*cur].val1,tree[2*cur+1].val1));
}
void pushdown(int cur)
{
if(tree[cur].laz!=0)
{
tree[2*cur].minn+=tree[cur].laz;
tree[2*cur].maxx+=tree[cur].laz;
tree[2*cur].laz+=tree[cur].laz;
tree[2*cur+1].minn+=tree[cur].laz;
tree[2*cur+1].maxx+=tree[cur].laz;
tree[2*cur+1].laz+=tree[cur].laz;
tree[cur].laz=0;
}
}
void build(int l,int r,int cur)
{
int m;
tree[cur].l=l;
tree[cur].r=r;
tree[cur].laz=0;
if(l==r)
{
tree[cur].minn=val[mp2[l]];
tree[cur].maxx=val[mp2[l]];
tree[cur].val0=0;
tree[cur].val1=0;
return;
}
m=(l+r)/2;
build(l,m,2*cur);
build(m+1,r,2*cur+1);
pushup(cur);
}
int queryII(int pl,int pr,int op,int &tminn,int &tmaxx,int cur)
{
int res,lminn,lmaxx,rminn,rmaxx;
if(pl<=tree[cur].l&&tree[cur].r<=pr)
{
tminn=tree[cur].minn;
tmaxx=tree[cur].maxx;
if(op==0) return tree[cur].val0;
else return tree[cur].val1;
}
pushdown(cur);
if(pr<=tree[2*cur].r) return queryII(pl,pr,op,tminn,tmaxx,2*cur);
else if(pl>=tree[2*cur+1].l) return queryII(pl,pr,op,tminn,tmaxx,2*cur+1);
else
{
res=0;
res=max(res,queryII(pl,pr,op,lminn,lmaxx,2*cur));
res=max(res,queryII(pl,pr,op,rminn,rmaxx,2*cur+1));
tminn=min(lminn,rminn);
tmaxx=max(lmaxx,rmaxx);
if(op==0) res=max(res,lmaxx-rminn);
else res=max(res,rmaxx-lminn);
return res;
}
}
int queryI(int u,int v)
{
int res,minnu,maxxu,minnv,maxxv,minnt,maxxt;
res=0,maxxu=0,minnu=N,maxxv=0,minnv=N;
while(top[u]!=top[v])
{
if(deep[top[u]]>deep[top[v]])
{
res=max(res,queryII(mp1[top[u]],mp1[u],0,minnt,maxxt,1));
res=max(res,maxxt-minnu);
res=max(res,maxxv-minnt);
minnu=min(minnu,minnt);
maxxu=max(maxxu,maxxt);
u=fa[top[u]];
}
else
{
res=max(res,queryII(mp1[top[v]],mp1[v],1,minnt,maxxt,1));
res=max(res,maxxv-minnt);
res=max(res,maxxt-minnu);
minnv=min(minnv,minnt);
maxxv=max(maxxv,maxxt);
v=fa[top[v]];
}
}
if(deep[u]>deep[v])
{
res=max(res,queryII(mp1[v],mp1[u],0,minnt,maxxt,1));
res=max(res,maxxt-minnu);
res=max(res,maxxv-minnt);
}
else
{
res=max(res,queryII(mp1[u],mp1[v],1,minnt,maxxt,1));
res=max(res,maxxv-minnt);
res=max(res,maxxt-minnu);
}
return res;
}
void updateII(int pl,int pr,int val,int cur)
{
if(pl<=tree[cur].l&&tree[cur].r<=pr)
{
tree[cur].minn+=val;
tree[cur].maxx+=val;
tree[cur].laz+=val;
return;
}
pushdown(cur);
if(pl<=tree[2*cur].r) updateII(pl,pr,val,2*cur);
if(pr>=tree[2*cur+1].l) updateII(pl,pr,val,2*cur+1);
pushup(cur);
}
void updateI(int u,int v,int w)
{
while(top[u]!=top[v])
{
if(deep[top[u]]<deep[top[v]]) swap(u,v);
updateII(mp1[top[u]],mp1[u],w,1);
u=fa[top[u]];
}
if(deep[u]<deep[v]) swap(u,v);
updateII(mp1[v],mp1[u],w,1);
}
int main()
{
int t,u,v,w,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&val[i]);
}
memset(first,-1,sizeof(first));
num=0;
for(i=1;i<=n-1;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
fa[1]=-1,deep[1]=1;
dfsI(1);
num=0;
dfsII(1,1);
build(1,n,1);
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d",&u,&v,&w);
printf("%d\n",queryI(u,v));
updateI(u,v,w);
}
}
return 0;
}