树链剖分+线段树
线段树维护max,min,左往右的最大差,右往左的最大差
求LCA时一定要注意方向

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 10), INF(1e9); IL ll Read(){
char c = '%'; ll x = 0, z = 1;
for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * z;
} int n, cnt, fst[_], to[_], nxt[_], fa[_], son[_], size[_], top[_], deep[_], dfn[_], w[_], id[_], tag[_ << 2];
struct Data{
int lr, rl, mx, mn;
IL void Init(){ lr = rl = mx = -INF; mn = INF; }
} t[_ << 2]; IL void Add(RG int u, RG int v){ to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++; } IL void Dfs1(RG int u){
size[u] = 1;
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(size[to[e]]) continue;
deep[to[e]] = deep[u] + 1; fa[to[e]] = u;
Dfs1(to[e]);
size[u] += size[to[e]];
if(size[to[e]] > size[son[u]]) son[u] = to[e];
}
} IL void Dfs2(RG int u, RG int Top){
top[u] = Top; dfn[u] = ++cnt; id[cnt] = u;
if(son[u]) Dfs2(son[u], Top);
for(RG int e = fst[u]; e != -1; e = nxt[e])
if(!dfn[to[e]]) Dfs2(to[e], to[e]);
} IL Data Merge(RG Data A, RG Data B){
RG Data C; C.Init();
C.mx = max(A.mx, B.mx); C.mn = min(A.mn, B.mn);
C.lr = max(max(A.lr, B.lr), B.mx - A.mn);
C.rl = max(max(A.rl, B.rl), A.mx - B.mn);
return C;
} # define lson x << 1, l, mid
# define rson x << 1 | 1, mid + 1, r IL void Pushdown(RG int x){
if(!tag[x]) return;
RG int ls = x << 1, rs = x << 1 | 1;
t[ls].mx += tag[x]; t[ls].mn += tag[x]; tag[ls] += tag[x];
t[rs].mx += tag[x]; t[rs].mn += tag[x]; tag[rs] += tag[x];
tag[x] = 0;
} IL void Build(RG int x, RG int l, RG int r){
if(l == r){ t[x].mx = t[x].mn = w[id[l]]; return; }
RG int mid = (l + r) >> 1;
Build(lson); Build(rson);
t[x] = Merge(t[x << 1], t[x << 1 | 1]);
} IL Data Query(RG int x, RG int l, RG int r, RG int L, RG int R, RG int v){
RG Data ans; ans.Init();
if(L <= l && R >= r){
ans = t[x]; t[x].mx += v; t[x].mn += v; tag[x] += v;
return ans;
}
Pushdown(x);
RG int mid = (l + r) >> 1;
if(L <= mid) ans = Query(lson, L, R, v);
if(R > mid) ans = Merge(ans, Query(rson, L, R, v));
t[x] = Merge(t[x << 1], t[x << 1 | 1]);
return ans;
} IL void Cover(RG int x, RG int y, RG int v){
RG Data ansl, ansr; ansl.Init(); ansr.Init();
while(top[x] != top[y]){
if(deep[top[x]] > deep[top[y]]) ansl = Merge(Query(1, 1, n, dfn[top[x]], dfn[x], v), ansl), x = fa[top[x]];
else ansr = Merge(Query(1, 1, n, dfn[top[y]], dfn[y], v), ansr), y = fa[top[y]];
}
RG Data Max; swap(ansl.lr, ansl.rl);
if(dfn[x] < dfn[y]){
ansl = Merge(ansl, Query(1, 1, n, dfn[x], dfn[y], v));
Max = Merge(ansl, ansr);
}
else{
Max = Query(1, 1, n, dfn[y], dfn[x], v); swap(Max.lr, Max.rl);
ansr = Merge(Max, ansr); Max = Merge(ansl, ansr);
}
printf("%d\n", Max.lr > 0 ? Max.lr : 0);
} int main(RG int argc, RG char *argv[]){
n = Read();
for(RG int i = 1; i <= n; i++) w[i] = Read(), fst[i] = -1;
for(RG int i = 1, a, b; i < n; i++) a = Read(), b = Read(), Add(a, b), Add(b, a);
Dfs1(1); cnt = 0; Dfs2(1, 1); Build(1, 1, n);
for(RG int Q = Read(), a, b, v; Q; Q--) a = Read(), b = Read(), v = Read(), Cover(a, b, v);
return 0;
}

[TJOI2015]旅游的更多相关文章

  1. 【BZOJ3999】[TJOI2015]旅游(Link-Cut Tree)

    [BZOJ3999][TJOI2015]旅游(Link-Cut Tree) 题面 BZOJ 洛谷 题解 一道不难的\(LCT\)题(用树链剖分不是为难自己吗,这种有方向的东西用\(LCT\)不是方便那 ...

  2. bzoj 3999: [TJOI2015]旅游

    Description 为了提高智商,ZJY准备去往一个新世界去旅游.这个世界的城市布局像一棵树.每两座城市之间只有一条路径可 以互达.每座城市都有一种宝石,有一定的价格.ZJY为了赚取最高利益,她会 ...

  3. BZOJ3999:[TJOI2015]旅游(树链剖分)

    Description 为了提高智商,ZJY准备去往一个新世界去旅游.这个世界的城市布局像一棵树.每两座城市之间只有一条路径可 以互达.每座城市都有一种宝石,有一定的价格.ZJY为了赚取最高利益,她会 ...

  4. BZOJ3999 [TJOI2015]旅游 【树剖 + 线段树】

    题目 为了提高智商,ZJY准备去往一个新世界去旅游.这个世界的城市布局像一棵树.每两座城市之间只有一条路径可 以互达.每座城市都有一种宝石,有一定的价格.ZJY为了赚取最高利益,她会选择从A城市买入再 ...

  5. BZOJ3999 [TJOI2015]旅游

    题面:给定一个有$n$个节点的树,每个点又点权$v_i$,每次选取一条树链$[a, b]$,求出$max(v_i - v_j)$,其中$i, j \in [a, b]$且$i$出现在$j$前面,最后树 ...

  6. 2019.01.20 bzoj3999: [TJOI2015]旅游(树链剖分)

    传送门 树链剖分菜题. 题意不清差评. 题意简述(保证清晰):给一棵带权的树,每次从aaa走到bbb,在走过的路径上任意找两个点,求后访问的点与先访问的点点权差的最大值. 思路: 考虑暴力:维护路径的 ...

  7. P3976 [TJOI2015]旅游(未完成)

    #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #inc ...

  8. bzoj 3999: [TJOI2015]旅游 LCT

    没啥难的,inf 的值设小了调了半天~ code: #include <bits/stdc++.h> #define N 50003 #define lson t[x].ch[0] #de ...

  9. TJOI2015 day2解题报告

    TJOI2015终于写完啦~~~ T1:[TJOI2015]旅游 描述:(BZ没题面只能口述了..)一个人在一棵树上走,每次从a->b会进行一次贸易(也就是在这条路径上买入物品然后在后面卖出)然 ...

随机推荐

  1. [译]Serilog Tutorial

    在过去的几年中,结构化日志已经大受欢迎.而Serilog是 .NET 中最著名的结构化日志类库 ,我们提供了这份的精简指南来帮助你快速了解并运用它. 0. 内容 设定目标 认识Serilog 事件和级 ...

  2. GNU autotools自动生成Makefile 介绍

    一.目的 使用autotools工具来帮助我们自动地生成符合自由软件惯例的makefile(这样就可以像常见的GNU程序一样,只要使用"./configure", "ma ...

  3. asp.net core 使用 swagger 生成接口文档

    参考地址:http://www.cnblogs.com/daxnet/p/6181366.html http://www.jianshu.com/p/fa5a9b76f3ed 微软参考文档:https ...

  4. PHP 个人用到的琐碎代码记录

    查找字符串出现次数的方法 substr_count(string,substring,[start],[length]) 函数延迟代码执行若干秒,若成功,返回 0,否则返回 false. sleep( ...

  5. PHP - curl实现采集

    1.开启curl支持 由于php环境安装后默认是没有打开curl支持的,需修改php.ini文件,找到;extension=php_curl.dll,把前面的冒号去掉,重启服务即可: 2.使用curl ...

  6. 1 Python数据类型--

    常见的Python数据类型: (1)数值类型:就是平时处理的数字(整数.浮点数) (2)序列类型:有一系列的对象并排或者排列的情况.如字符串(str),列表(list),元组(tuple)等 (3)集 ...

  7. soj1091 指环王 bfs+hash+剪枝

    原题链接http://acm.scu.edu.cn/soj/problem.action?id=1091 这题的主要解法就是搜索,我用的是bfs,用map将二维数组处理成字符串作为主键,到达当前状态的 ...

  8. iOS开发之emoji处理

    看似emoji处理应该和我们iOS开发有着很大的关系,实则不然,对于emoji的处理只要在服务器端做就可以了,我所说的处理是我们将表情提交到服务器,那么在服务器端需要做些什么处理呢? 服务器如果直接存 ...

  9. Servlet中forward和redirect的区别(转)

    forward方式:request.getRequestDispatcher("/somePage.jsp").forwardrequest, response);     red ...

  10. 自动化测试(—)Web自动化测试理解

          最近几天,由于工作需要接触Web自动化测试,所以我从网上找的资料,学习了解,然后自己总结汇总的随笔,如文章中有不足的地方,请大家多多指教:或者文章内容与他人相似,望见谅. 手工测试跟自动化 ...