bzoj 3999 旅游

树链剖分 + 线段树.

先写一个树链剖分.

再用线段树维护信息,每个区间需要维护最大值,最小值,前面某个数减后面某个数的最大值,最小值.

修改操作比较简单,对于询问操作,先用分割出的线段树区间的最值更新答案,再将所有分割出的区间按照经过的顺序排序,暴力枚举两个区间,用前一个区间的最小值减去后一个区间的最大值更新答案.

在线段树上处理时,需要注意移动的方向.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//%std
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=1e9;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
const int MAXN=5e4+10;
int n,m,ecnt=0,head[MAXN],to[MAXN<<1],nx[MAXN<<1];
void addedge(int u,int v)
{
++ecnt;
to[ecnt]=v;
nx[ecnt]=head[u];
head[u]=ecnt;
}
int val[MAXN],dfn[MAXN],rnk[MAXN],idx=0;
int siz[MAXN],dep[MAXN],mxson[MAXN],fa[MAXN],top[MAXN];
void dfs1(int u,int f)
{
siz[u]=1;
fa[u]=f;
for(int i=head[u]; i; i=nx[i])
{
int v=to[i];
if(v==f)
continue;
dep[v]=dep[u]+1;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[mxson[u]])
mxson[u]=v;
}
}
void dfs2(int u,int tp)
{
top[u]=tp;
dfn[u]=++idx;
rnk[idx]=u;
if(mxson[u])
dfs2(mxson[u],tp);
for(int i=head[u]; i; i=nx[i])
{
int v=to[i];
if(v!=fa[u] && v!=mxson[u])
dfs2(v,v);
}
}
struct node
{
ll tag;
ll v[2],delta[2];
int tim;
bool operator < (const node &rhs) const
{
return tim<rhs.tim;
}
} Tree[MAXN<<2];
int lt,rt,tot;
ll ans;
node tmp[MAXN];
#define root Tree[o]
#define lson Tree[o<<1]
#define rson Tree[o<<1|1]
void pushup(int o)
{
root.v[0]=max(lson.v[0],rson.v[0]);
root.v[1]=min(lson.v[1],rson.v[1]);
root.delta[0]=max(lson.delta[0],rson.delta[0]);
root.delta[0]=max(root.delta[0],rson.v[0]-lson.v[1]);
root.delta[1]=max(lson.delta[1],rson.delta[1]);
root.delta[1]=max(root.delta[1],lson.v[0]-rson.v[1]);
}
void BuildTree(int o,int l,int r)
{
root.tag=0;
if(l==r)
{
root.v[0]=root.v[1]=val[rnk[l]];
root.delta[0]=root.delta[1]=0;
return;
}
int mid=(l+r)>>1;
BuildTree(o<<1,l,mid);
BuildTree(o<<1|1,mid+1,r);
pushup(o);
}
void modify(int o,int c)
{
root.tag+=c;
root.v[0]+=c;
root.v[1]+=c;
}
void pushdown(int o)
{
if(root.tag)
{
modify(o<<1,root.tag);
modify(o<<1|1,root.tag);
root.tag=0;
}
}
void query(int o,int l,int r,int L,int R,int dir,int c)
// dir=0 left->right dir=1 right->left
{
if(L<=l && r<=R)
{
ans=max(ans,root.delta[dir]);
tmp[++tot]=root;
if(!dir)
tmp[tot].tim=rt--;
else
tmp[tot].tim=lt++;
modify(o,c);
return;
}
pushdown(o);
int mid=(l+r)>>1;
if(R>mid)
query(o<<1|1,mid+1,r,L,R,dir,c);
if(L<=mid)
query(o<<1,l,mid,L,R,dir,c);
pushup(o);
}
ll solve(int x,int y,int c)
{
ans=tot=0;
lt=0,rt=inf;
while(top[x]!=top[y])
{
if(dep[top[x]]>dep[top[y]]) //jump x
{
query(1,1,n,dfn[top[x]],dfn[x],1,c);
x=fa[top[x]];
}
else //jump y
{
query(1,1,n,dfn[top[y]],dfn[y],0,c);
y=fa[top[y]];
}
}
if(dep[x]>dep[y]) //jump x
query(1,1,n,dfn[y],dfn[x],1,c);
else //jump y
query(1,1,n,dfn[x],dfn[y],0,c);
sort(tmp+1,tmp+1+tot);
for(int i=1; i<tot; ++i)
for(int j=i+1; j<=tot; ++j)
ans=max(ans,tmp[j].v[0]-tmp[i].v[1]);
return ans;
}
int main()
{
n=read();
for(int i=1; i<=n; ++i)
val[i]=read();
for(int i=1; i<n; ++i)
{
int u=read(),v=read();
addedge(u,v);
addedge(v,u);
}
dfs1(1,0);
dfs2(1,1);
BuildTree(1,1,n);
m=read();
for(int i=1; i<=m; ++i)
{
int x=read(),y=read(),c=read();
printf("%lld\n",solve(x,y,c));
}
return 0;
}