本来想水题的
开始以为就是一个区间维护最大值和最小值
结果开开心心的打了200行程序,一测样例,过不去
为什么呢?因为他要先买到宝石才能卖掉啊
所以白打了200行吗qwq
树剖的做法是显然的,线段树维护也是显然的
但是不能光维护最大值和最小值了
我们需要找一个
a
a
a和
b
b
b使
a
a
a在
b
b
b左边(形象的解释),让
v
a
l
a
−
v
a
l
b
val_a-val_b
vala−valb最大
怎么办呢?暴力找吗
我们需要维护这些东西
1.区间最小值
2.区间最大值
3.区间从左边走到右边能够获得的最大利润
4.区间从右边走到左边能够获得的最大利润
为什么要分开维护3,4呢?因为我们能够发现,我们从不同方向走同一段路得到的答案显然是不一样的
对于这种返回值比较多的东西,我们可以通过结构体返回来实现
简单贴下合并答案的代码(这里我图省事没有新建结构体,直接用的线段树的结构体)
segment_tree merge(segment_tree l,segment_tree r){
segment_tree res;
res._max=max(l._max,r._max);
res._min=min(l._min,r._min);
res.lmax=max(l._max-r._min,max(l.lmax,r.lmax));
res.rmax=max(r._max-l._min,max(l.rmax,r.rmax));
return res;
}
然后就很简单啦,我为了图省事,就在原来写的代码上改了改,于是成功的让我花了两个小时去debug…
如果当时重构代码的话估计还快一点qwq
树剖这种大码量的题还是要注意细节问题,比如说l,r有没有写反之类的
我就是最后询问的时候一个地方把r写成了l样例死活过不去
记得最后把l的左右swap一下
话说我感觉我左右写反了,为啥我要输出rmax呢qaq
code(我知道你们只看这个):
# include <bits/stdc++.h>
using namespace std;
# define Rep(i,a,b) for(int i=a;i<=b;i++)
# define _Rep(i,a,b) for(int i=a;i>=b;i--)
# define RepG(i,u) for(int i=head[u];~i;i=e[i].next)
# define debug puts("QAQ");
typedef long long ll;
const int N=1e5+5;
const int mod=1e9+7;
const double eps=1e-7;
template <typename T> void read(T &x){
x=0;int f=1;
char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-'0';
x*=f;
}
int n,q;
int head[N],cnt;
int faz[N],son[N],dep[N],siz[N],top[N],dfn[N],tot;
int a[N],_a[N];
struct Edge{
int to,next;
}e[N<<1];
void add(int x,int y){
e[++cnt]=(Edge){y,head[x]},head[x]=cnt;
}
struct segment_tree{
int l,r;
int _max,_min;
int lmax,rmax;
int tag;
segment_tree(){l=r=_max=_min=tag=0;lmax=rmax=0;}
bool operator == (const segment_tree &cmp)const{
return l==cmp.l&&r==cmp.r&&_max==cmp._max&&_min==cmp._min&&lmax==cmp.lmax&&rmax==cmp.rmax&&tag==cmp.tag;
}
}seg[N<<2];
# define lc (u<<1)
# define rc (u<<1|1)
void pushup(int u){
seg[u]._max=max(seg[lc]._max,seg[rc]._max);
seg[u]._min=min(seg[lc]._min,seg[rc]._min);
seg[u].lmax=max(seg[lc]._max-seg[rc]._min,max(seg[lc].lmax,seg[rc].lmax));
seg[u].rmax=max(seg[rc]._max-seg[lc]._min,max(seg[lc].rmax,seg[rc].rmax));
}
void pushdown(int u){
seg[lc]._max+=seg[u].tag,seg[lc]._min+=seg[u].tag;
seg[rc]._max+=seg[u].tag,seg[rc]._min+=seg[u].tag;
seg[lc].tag+=seg[u].tag,seg[rc].tag+=seg[u].tag;
seg[u].tag=0;
}
segment_tree merge(segment_tree l,segment_tree r){
segment_tree res;
res._max=max(l._max,r._max);
res._min=min(l._min,r._min);
res.lmax=max(l._max-r._min,max(l.lmax,r.lmax));
res.rmax=max(r._max-l._min,max(l.rmax,r.rmax));
return res;
}
void build(int u,int l,int r){
seg[u].l=l,seg[u].r=r;
if(l==r){
seg[u]._max=_a[l],seg[u]._min=_a[l];
return;
}
int mid=l+r>>1;
build(lc,l,mid);
build(rc,mid+1,r);
pushup(u);
}
void update(int u,int l,int r,int k){
if(seg[u].l>=l&&seg[u].r<=r){
seg[u]._max+=k,seg[u]._min+=k;
seg[u].tag+=k;
return;
}
if(seg[u].tag)pushdown(u);
int mid=seg[u].l+seg[u].r>>1;
if(l<=mid)update(lc,l,r,k);
if(r>mid)update(rc,l,r,k);
pushup(u);
}
segment_tree query(int u,int l,int r){
if(seg[u].l>=l&&seg[u].r<=r)return seg[u];
if(seg[u].tag)pushdown(u);
int mid=seg[u].l+seg[u].r>>1;
if(r<=mid)return query(lc,l,r);
if(l>mid)return query(rc,l,r);
return merge(query(lc,l,r),query(rc,l,r));
}
void RouteModify(int x,int y,int k){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
update(1,dfn[top[x]],dfn[x],k);
x=faz[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
update(1,dfn[x],dfn[y],k);
}
int RouteQuery(int x,int y){
segment_tree l,r;
l._min=r._min=2e9;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]){
r=merge(query(1,dfn[top[y]],dfn[y]),r);
y=faz[top[y]];
}
else{
l=merge(query(1,dfn[top[x]],dfn[x]),l);
x=faz[top[x]];
}
}
if(dep[x]>dep[y])l=merge(query(1,dfn[y],dfn[x]),l);
else r=merge(query(1,dfn[x],dfn[y]),r);
swap(l.lmax,l.rmax);
// printf("%d %d %d %d\n",r._max,r._min,r.lmax,r.rmax);
return merge(l,r).rmax;
}
void dfs1(int u,int fa){
faz[u]=fa;
siz[u]=1;
dep[u]=dep[fa]+1;
RepG(i,u){
int v=e[i].to;
if(v==fa)continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])son[u]=v;
}
}
void dfs2(int u,int _top){
top[u]=_top;
dfn[u]=++tot;
_a[tot]=a[u];
if(!son[u])return;
dfs2(son[u],_top);
RepG(i,u){
int v=e[i].to;
if(v==faz[u]||v==son[u])continue;
dfs2(v,v);
}
}
int main()
{
memset(head,-1,sizeof(head));
read(n);
Rep(i,1,n)read(a[i]);
Rep(i,1,n-1){
int x,y;
read(x),read(y);
add(x,y),add(y,x);
}
dfs1(1,0),dfs2(1,1);
build(1,1,n);
read(q);
while(q--){
int x,y,c;
read(x),read(y),read(c);
printf("%d\n",RouteQuery(x,y));
RouteModify(x,y,c);
}
return 0;
}