[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会进行一次贸易(也就是在这条路径上买入物品然后在后面卖出)然 ...
随机推荐
- JSP的几种跳转方式的异同
1 <jsp:foward page="url" /> 服务端跳转,立即跳转,后续语句不会执行: 2 <% response.sendRedirect(" ...
- iOS 9 HTTPS 的配置
方法有两种: (1)废话少说直接上图: (2)右击info.plist 文件 open as ->source code 在里面注入如下代码就行了(位置不固定,但要在指定的文件夹选项里) < ...
- Java经典编程题50道之十三
一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? public class Example13 { public static void main(St ...
- ajax上传图片chrome报错net::ERR_CONNECTION_RESET/net::ERR_CONNECTION_ABORTED
网上搜了一下,base64图片太大,tomcat对post请求大小有默认限制,要在tomcat配置文件server.xml 加一个:maxPostSize="0",0表示无限制 & ...
- shiro进行散列算法操作
shiro最闪亮的四大特征:认证,权限,加密,会话管理 为了提高应用系统的安全性,这里主要关注shiro提供的密码服务模块: 1.加密工具类的熟悉 首先来个结构图,看看shiro提供了哪些加密工具类: ...
- js的继承实现
1.原型链继承 1.创建父类对象2.创建子类函数对象3.将父类的实例对象赋值给子类的原型4.将子类的原型属性的构造函数设置为 子类本身 function Person(name) { this.nam ...
- mysql那些事
---恢复内容开始--- 登录 mysql登录 -u+用户 -p 密码 显示数据库 show databases; 使用某个数据库 use xxx; 显示数据库表 show tables 显示表结构 ...
- Smarty3.1.3安装使用
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Verdana } span.s1 { } Smarty简介 Smarty是一个PHP的模板引 ...
- 使用CrashHandler来获取应用的crash信息
源码地址https://github.com/king1039/android-art-res/tree/master/Chapter_13/CrashTest/src/com/ryg/crashte ...
- 傅里叶变换 - Fourier Transform
傅里叶级数 傅里叶在他的专著<热的解析理论>中提出,任何一个周期函数都可以表示为若干个正弦函数的和,即: \[f(t)=a_0+\sum_{n=1}^{\infty}(a_ncos(n\o ...