树链剖分+线段树
线段树维护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. shell编程之环境变量配置文件(4)

    1 source命令 修改了配置文件,并让它立刻生效,而不用重新登录 source 配置文件 或者 .配置文件 2 环境变量配置文件简介 PATH HISTSIZE PS1 HOSTNAME等环境变量 ...

  2. IQKeyboardManager 自动处理键盘事件的第三方库

    我们写界面要考虑很多用户体验问题,键盘事件的响应就是比较麻烦的一种.我们需要监听键盘事件,考虑点击背景收起键盘.考虑键盘遮挡输入框问题等等,而且每个界面都要做这么一套.这个库帮我们解决了这个事情. 这 ...

  3. iOS开发中UIPopoverController的使用详解

    这篇文章主要介绍了iOS开发中UIPopoverController的使用,代码基于传统的Objective-C,需要的朋友可以参考下 一.简单介绍 1.什么是UIPopoverController ...

  4. nf共享

    实验环境是两台Centos6.8 客户端是192.168.3.218 服务端是192.168.3.219 首先配置服务端 1 安装包 用yum安装需要的服务包(两边都安装) yum install n ...

  5. 【ASP.NET Core】解决“The required antiforgery cookie "xxx" is not present”的错误

    当你在页面上用 form post 内容时,可能会遇到以下异常: The required antiforgery cookie "????????" is not present ...

  6. [翻译] 编写高性能 .NET 代码--第二章 GC -- 配置选项

    配置选项 在基于"less rope to hang yourself with"思想下,.NET 框架没有给开发提供很多太多的配置选项.但在大多数情况下,GC会跟你的硬件配置,及 ...

  7. nginx虚拟域名的配置以及测试验证

    1.保证该机器上安装了nginx 未安装请看:centos/linux下的安装Nginx 2.使用root用户编辑配置文件 vim /usr/local/nginx/conf/nginx.conf 3 ...

  8. Jquery那些坑

    今天写Jquery的时候突然发现在将$("<td><td/>").appendTo(someElement)的时候发现一下子多出来两个,甚是奇怪,检查后端和 ...

  9. PHP对象和接口抽象类注意事项

    Php 的对象的实现: 注:对象名称最好以对象名.class.php来进行命名. 1. 对于静态类的成员不能使用$this->这个来调用其静态类的成员和函数,应该使用self::成员或者方法来进 ...

  10. 快了快了,你的 MacBook Pro 和 FineUICore!

    着玻璃窗,看到星巴克里那帮人拿着MacBook喝咖啡,你是不是要默念一遍:这帮傻叉,就爱装逼! 不过话说回来,你想不想尝试下这个傻叉的感觉? 是时候了,给自己一个理由,拥有自己的 MacBook Pr ...