# hdu5052

http://www.elijahqi.win/archives/830
tjoi2015我写错了 导致这题wa半天，tjoi数据太弱，没跑出来是错的

Problem Description
Yaoge likes to eat chicken chops late at night. Yaoge has eaten too many chicken chops, so that Yaoge knows the pattern in the world of chicken chops. There are N cities in the world numbered from 1 to N . There are some roads between some cities, and there is one and only one simple path between each pair of cities, i.e. the cities are connected like a tree. When Yaoge moves along a path, Yaoge can choose one city to buy ONE chicken chop and sell it in a city after the city Yaoge buy it. So Yaoge can get profit if Yaoge sell the chicken chop with higher price. Yaoge is famous in the world. AFTER Yaoge has completed one travel, the price of the chicken chop in each city on that travel path will be increased by V .

Input
The first line contains an integer T (0 < T ≤ 10), the number of test cases you need to solve. For each test case, the first line contains an integer N (0 < N ≤ 50000), the number of cities. For each of the next N lines, the i-th line contains an integer Wi(0 < Wi ≤ 10000), the price of the chicken chop in city i. Each of the next N - 1 lines contains two integers X Y (1 ≤ X, Y ≤ N ), describing a road between city X and city Y . The next line contains an integer Q(0 ≤ Q ≤ 50000), the number of queries. Each of the next Q lines contains three integer X Y V(1 ≤ X, Y ≤ N ; 0 < V ≤ 10000), meaning that Yaoge moves along the path from city X to city Y , and the price of the chicken chop in each city on the path will be increased by V AFTER Yaoge has completed this travel.

Output
For each query, output the maximum profit Yaoge can get. If no positive profit can be earned, output 0 instead.

Sample Input
1 5 1 2 3 4 5 1 2 2 3 3 4 4 5 5 1 5 1 5 1 1 1 1 2 5 1 1 1 2 1

Sample Output
4 0 0 1 0

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 55000
#define inf 0x7fffffff
using namespace std;
int x=0;char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x;
}
struct node{
int y,next;
}data[N<<1];
struct node1{
int max,min,ans,ans1;
node1(){
ans=ans1=max=0;min=inf;
}
};
struct node2{
int l,r,left,right,max,min,ans,ans1,lazy;
}tree[N<<2];
int size[N],id[N],fa[N],son[N],tp[N],a[N],dep[N],h[N],cnt,root,n,num,m,w[N];
void dfs1(int x){
size[x]=1;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if(fa[x]==y) continue;
dep[y]=dep[x]+1;fa[y]=x;dfs1(y);size[x]+=size[y];
if (size[y]>size[son[x]]) son[x]=y;
}
}
void dfs2(int x,int top){
id[x]=++cnt;tp[x]=top;w[cnt]=a[x];
if (son[x]) dfs2(son[x],top);
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (y==fa[x]||y==son[x]) continue;
dfs2(y,y);
}
}
inline void update(int x){
int l=tree[x].left,r=tree[x].right;
tree[x].ans=max(tree[r].max-tree[l].min,max(tree[l].ans,tree[r].ans));
tree[x].ans1=max(tree[l].max-tree[r].min,max(tree[l].ans1,tree[r].ans1));
tree[x].min=min(tree[l].min,tree[r].min);
tree[x].max=max(tree[l].max,tree[r].max);

}
void print(int x){
if (tree[x].left) print(tree[x].left);
printf("%d %d %d %d %d %d %d\n",tree[x].l,tree[x].r,tree[x].max,tree[x].min,tree[x].ans,tree[x].ans1,tree[x].lazy);
if (tree[x].right) print(tree[x].right);
}
void build(int &x,int l,int r){
x=++cnt;tree[x].l=l;tree[x].r=r;tree[x].lazy=0;
if(l==r){
tree[x].min=tree[x].max=w[l];
tree[x].ans=tree[x].ans1=0;return;
}
int mid=l+r>>1;
build(tree[x].left,l,mid);build(tree[x].right,mid+1,r);
update(x);
}
inline node1 update1(node1 tmp1,node1 tmp2){
node1 ans1;
ans1.ans=max(tmp2.max-tmp1.min,max(tmp1.ans,tmp2.ans));
ans1.ans1=max(tmp1.max-tmp2.min,max(tmp1.ans1,tmp2.ans1));
ans1.min=min(tmp1.min,tmp2.min);
ans1.max=max(tmp1.max,tmp2.max);
return ans1;
}
void pushdown(int x){
if (!tree[x].lazy) return;
int l=tree[x].left,r=tree[x].right;
int lazy=tree[x].lazy;
tree[l].lazy+=lazy;tree[r].lazy+=lazy;
tree[l].max+=lazy;tree[r].max+=lazy;
tree[l].min+=lazy;tree[r].min+=lazy;
tree[x].lazy=0;
}
node1 query(int rt,int l,int r,int v){
if(l<=tree[rt].l&&r>=tree[rt].r){
tree[rt].lazy+=v;tree[rt].max+=v;tree[rt].min+=v;
node1 tmp;tmp.max=tree[rt].max;tmp.min=tree[rt].min;tmp.ans=tree[rt].ans;tmp.ans1=tree[rt].ans1;
return tmp;
}
int mid=(tree[rt].l+tree[rt].r)>>1;
node1 tmp1;node1 tmp2;pushdown(rt);
if (l<=mid) tmp1=query(tree[rt].left,l,r,v);
if (r>mid) tmp2=query(tree[rt].right,l,r,v);
update(rt);
return update1(tmp1,tmp2);
}
inline void swap(int &x,int &y){
x^=y;y^=x;x^=y;
}
void solve(int x,int y,int v){
node1 ansl,ansr;
while (tp[x]!=tp[y]){
if(dep[tp[x]]>dep[tp[y]]){
ansl=update1(query(root,id[tp[x]],id[x],v),ansl);
x=fa[tp[x]];
}else{
ansr=update1(query(root,id[tp[y]],id[y],v),ansr);
y=fa[tp[y]];
}
}
swap(ansl.ans,ansl.ans1);
if (id[x]<id[y]){
ansl=update1(ansl,query(root,id[x],id[y],v));
ansl=update1(ansl,ansr);
}else{
node1 tmpp=query(root,id[y],id[x],v);
swap(tmpp.ans,tmpp.ans1);
ansr=update1(tmpp,ansr);
ansl=update1(ansl,ansr);
}
printf("%d\n",ansl.ans);
//    printf("sdfsdf\n");
//print(root);
}
int main(){
freopen("hdu5052.in","r",stdin);
while (T--){
for (int i=1;i<n;++i){
data[++num].y=y;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].next=h[y];h[y]=num;
}dep[1]=1;
dfs1(1);dfs2(1,1);cnt=0;
//for (int i=1;i<=n;++i) printf("%d ",id[i]);printf("\n");
build(root,1,n);//print(root);
}