[TJOI2015]旅游
树链剖分+线段树
线段树维护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]旅游的更多相关文章
- 【BZOJ3999】[TJOI2015]旅游(Link-Cut Tree)
[BZOJ3999][TJOI2015]旅游(Link-Cut Tree) 题面 BZOJ 洛谷 题解 一道不难的\(LCT\)题(用树链剖分不是为难自己吗,这种有方向的东西用\(LCT\)不是方便那 ...
- bzoj 3999: [TJOI2015]旅游
Description 为了提高智商,ZJY准备去往一个新世界去旅游.这个世界的城市布局像一棵树.每两座城市之间只有一条路径可 以互达.每座城市都有一种宝石,有一定的价格.ZJY为了赚取最高利益,她会 ...
- BZOJ3999:[TJOI2015]旅游(树链剖分)
Description 为了提高智商,ZJY准备去往一个新世界去旅游.这个世界的城市布局像一棵树.每两座城市之间只有一条路径可 以互达.每座城市都有一种宝石,有一定的价格.ZJY为了赚取最高利益,她会 ...
- BZOJ3999 [TJOI2015]旅游 【树剖 + 线段树】
题目 为了提高智商,ZJY准备去往一个新世界去旅游.这个世界的城市布局像一棵树.每两座城市之间只有一条路径可 以互达.每座城市都有一种宝石,有一定的价格.ZJY为了赚取最高利益,她会选择从A城市买入再 ...
- BZOJ3999 [TJOI2015]旅游
题面:给定一个有$n$个节点的树,每个点又点权$v_i$,每次选取一条树链$[a, b]$,求出$max(v_i - v_j)$,其中$i, j \in [a, b]$且$i$出现在$j$前面,最后树 ...
- 2019.01.20 bzoj3999: [TJOI2015]旅游(树链剖分)
传送门 树链剖分菜题. 题意不清差评. 题意简述(保证清晰):给一棵带权的树,每次从aaa走到bbb,在走过的路径上任意找两个点,求后访问的点与先访问的点点权差的最大值. 思路: 考虑暴力:维护路径的 ...
- P3976 [TJOI2015]旅游(未完成)
#include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #inc ...
- bzoj 3999: [TJOI2015]旅游 LCT
没啥难的,inf 的值设小了调了半天~ code: #include <bits/stdc++.h> #define N 50003 #define lson t[x].ch[0] #de ...
- TJOI2015 day2解题报告
TJOI2015终于写完啦~~~ T1:[TJOI2015]旅游 描述:(BZ没题面只能口述了..)一个人在一棵树上走,每次从a->b会进行一次贸易(也就是在这条路径上买入物品然后在后面卖出)然 ...
随机推荐
- ThinkPHP的基本操作
一.生成入口文件 1.打开服务器,在本地环境测试时在地址栏输入localhost/项目文件名/index.php 可以在Application下面生成一个home模块,记得在这之前,要建立一个项目文 ...
- 在阿里云上搭建 Spark 实验平台
在阿里云上搭建 Spark 实验平台 Hadoop2.7.3+Spark2.1.0 完全分布式环境 搭建全过程 [传统文化热爱者] 阿里云服务器搭建spark特别坑的地方 阿里云实现Hadoop+Sp ...
- angularjs 指令详解
一.指令定义 对于指令,可以把它简单的理解成在特定DOM元素上运行的函数,指令可以扩展这个元素的功能. 首先来看个完整的参数示例再来详细的介绍各个参数的作用及用法: <div my-direct ...
- [NOIP]2016天天爱跑步
[NOIP]2016天天爱跑步 标签: LCA 树上差分 NOIP Description 小C同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是 ...
- angular2-qrcode (转)
插件选择 angular2-qrcode npm install angular2-qrcode --savecnpm install angular2-qrcode --save 参考github ...
- 请小心使用 ng-repeat 中的 $index
自己自学的时候,遇到$index不知道它是如何使用的,所以上网搜了一下,发现了这个关于使用$index可能会出现的一个小BUG,和大家分享一下 PS:我是小白,欢迎指正,非常感谢! 以下是全文: &q ...
- PHP实现WebService的简单示例和实现步骤
首先我创建的文件有: api.php api的接口类文件 api.wsdl 我创建产生的最后要调用的接口文件 cometrue.php 注册service api类内容的所有内容的执行文件 creat ...
- Appium基于Python unittest自动化测试 & 自动化测试框架 -- PO并生成html测试报告
基于python单元测试框架unittest完成appium自动化测试,生成基于html可视化测试报告 代码示例: #利用unittest并生成测试报告 class Appium_test(unitt ...
- js中checkbox的全选和反选的实现
<head> <meta charset="utf-8"/> <script type="text/javascript"> ...
- MongoDB入门系列(四):权限管理
一.概述 本篇文章主要介绍如何创建用户和角色相关概念,同时对角色的添加和删除做了相关介绍. 版本:3.6.2 二.角色相关概念 1.数据库用户角色 read:该角色拥有数据的只读权限,系统集合以及sy ...