2019.01.20 bzoj3999: [TJOI2015]旅游(树链剖分)
传送门
树链剖分菜题。
题意不清差评。
题意简述(保证清晰):给一棵带权的树,每次从aaa走到bbb,在走过的路径上任意找两个点,求后访问的点与先访问的点点权差的最大值。
思路:
考虑暴力:维护路径的前缀最小值和后缀最大值然后更新答案。
然后可以用线段树优化这个过程。
对于当前的线段树节点,它的答案=max(左儿子答案,右儿子答案,右儿子最大值−左儿子最小值)max(左儿子答案,右儿子答案,右儿子最大值-左儿子最小值)max(左儿子答案,右儿子答案,右儿子最大值−左儿子最小值)
然后由于路径是有向的,因此在树链剖分两边跳的时候也要考虑方向的因素。
所以还要维护一个跟上面对称的信息,即路径的前缀最大值和后缀最小值。
然后就没了。
代码:
#include<bits/stdc++.h>
#define ri register int
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
typedef long long ll;
const int N=5e4+5;
const ll inf=1e18;
int n,tot=0,a[N],num[N],pred[N],siz[N],hson[N],top[N],dep[N],fa[N];
vector<int>e[N];
struct Node{
int l,r;ll mx,mn,det[2],add;
Node(){l=r=add=0,mx=-inf,mn=inf,det[0]=det[1]=-inf;}
}T[N<<2];
inline Node operator+(const Node&a,const Node&b){
Node ret;
ret.l=a.l,ret.r=b.r,ret.add=0;
ret.mx=max(a.mx,b.mx),ret.mn=min(a.mn,b.mn);
ret.det[0]=max(max(a.det[0],b.det[0]),b.mx-a.mn);
ret.det[1]=max(max(a.det[1],b.det[1]),a.mx-b.mn);
return ret;
}
inline void pushnow(int p,ll v){T[p].mx+=v,T[p].mn+=v,T[p].add+=v;}
inline void pushdown(int p){if(T[p].add)pushnow(lc,T[p].add),pushnow(rc,T[p].add),T[p].add=0;}
inline void build(int p,int l,int r){
T[p].l=l,T[p].r=r;
if(l==r){T[p].mx=T[p].mn=a[pred[l]];return;}
build(lc,l,mid),build(rc,mid+1,r),T[p]=T[lc]+T[rc];
}
inline void update(int p,int ql,int qr,ll v){
if(ql<=T[p].l&&T[p].r<=qr)return pushnow(p,v);
pushdown(p);
if(qr<=mid)update(lc,ql,qr,v);
else if(ql>mid)update(rc,ql,qr,v);
else update(lc,ql,mid,v),update(rc,mid+1,qr,v);
T[p]=T[lc]+T[rc];
}
inline Node query(int p,int ql,int qr){
if(ql<=T[p].l&&T[p].r<=qr)return T[p];
pushdown(p);
if(qr<=mid)return query(lc,ql,qr);
if(ql>mid)return query(rc,ql,qr);
return query(lc,ql,mid)+query(rc,mid+1,qr);
}
inline void change(int x,int y,ll v){
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
update(1,num[top[x]],num[x],v),x=fa[top[x]];
}
if(dep[x]<dep[y])swap(x,y);
update(1,num[y],num[x],v);
}
inline int lca(int x,int y){
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
inline ll ask(int x,int y){
bool f=x+y==11;
int t=lca(x,y);
Node t1,t2;
while(top[x]^top[t])t1=query(1,num[top[x]],num[x])+t1,x=fa[top[x]];
while(top[y]^top[t])t2=query(1,num[top[y]],num[y])+t2,y=fa[top[y]];
if(y==t)t1=query(1,num[t],num[x])+t1;
else t2=query(1,num[t],num[y])+t2;
return max(max(t1.det[1],t2.det[0]),max(0ll,t2.mx-t1.mn));
}
void bfs(){
static int q[N],hd,tl;
q[hd=tl=1]=1;
while(hd<=tl){
int p=q[hd++];
siz[p]=1;
for(ri i=0,v;i<e[p].size();++i){
if((v=e[p][i])==fa[p])continue;
fa[v]=p,dep[v]=dep[p]+1,q[++tl]=v;
}
}
for(ri i=n,p;i;--i){
p=q[i];
if(i^1){
siz[fa[p]]+=siz[p];
if(siz[p]>siz[hson[fa[p]]])hson[fa[p]]=p;
}
}
q[hd=tl=n]=1;
while(hd<=tl){
int p=q[hd++];
top[p]=hson[fa[p]]==p?top[fa[p]]:p;
pred[num[p]=++tot]=p;
if(!hson[p])continue;
for(ri i=0,v;i<e[p].size();++i){
if((v=e[p][i])==fa[p]||v==hson[p])continue;
q[--hd]=v;
}
q[--hd]=hson[p];
}
}
int main(){
n=read();
for(ri i=1;i<=n;++i)a[i]=read();
for(ri i=1,u,v;i<n;++i)u=read(),v=read(),e[u].push_back(v),e[v].push_back(u);
bfs(),build(1,1,n);
for(ri tt=read(),u,v;tt;--tt){
u=read(),v=read();
ll w=read();
cout<<ask(u,v)<<'\n';
change(u,v,w);
}
return 0;
}
2019.01.20 bzoj3999: [TJOI2015]旅游(树链剖分)的更多相关文章
- BZOJ3999:[TJOI2015]旅游(树链剖分)
Description 为了提高智商,ZJY准备去往一个新世界去旅游.这个世界的城市布局像一棵树.每两座城市之间只有一条路径可 以互达.每座城市都有一种宝石,有一定的价格.ZJY为了赚取最高利益,她会 ...
- BZOJ 2157: 旅游( 树链剖分 )
树链剖分.. 样例太大了根本没法调...顺便把数据生成器放上来 -------------------------------------------------------------------- ...
- BZOJ2157旅游——树链剖分+线段树
题目描述 Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路 ...
- BZOJ2157: 旅游 树链剖分 线段树
http://www.lydsy.com/JudgeOnline/problem.php?id=2157 在对树中数据进行改动的时候需要很多pushdown(具体操作见代码),不然会wa,大概原因 ...
- 2019 ACM-ICPC 西安全国邀请赛 E-Tree 树链剖分+线段树
题意 给一颗带点权的树,三种操作 \(1~s~t\) 修改从1到s的路径上的所有点,\(a[i]=a[i]|t\) \(2~s~t\) 修改从1到s的路径上的所有点,\(a[i]=a[i]\& ...
- BZOJ 2157: 旅游 (树链剖分+线段树)
树链剖分后线段树维护区间最大最小值与和. 支持单点修改与区间取反. 直接写个区间取反标记就行了.线段树板题.(200行6000B+ 1A警告) #include <cstdio> #inc ...
- 2019.01.19 bzoj3653: 谈笑风生(长链剖分优化dp)
传送门 长链剖分优化dpdpdp水题. 题意简述:给一棵树,mmm次询问,每次给一个点aaa和一个值kkk,询问满足如下条件的三元组(a,b,c)(a,b,c)(a,b,c)的个数. a,b是c的祖先 ...
- 【BZOJ2157】旅游 树链剖分+线段树
[BZOJ2157]旅游 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本 ...
- 洛谷 P1505 [国家集训队]旅游 树链剖分
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例: 输出样例: 说明 思路 AC代码 总结 题面 题目链接 P1505 [国家集训队]旅游 题目描述 Ray 乐 ...
随机推荐
- POJ-3126.PrimePath(欧拉筛素数打表 + BFS)
给出一篇有关素数线性筛和区间筛的博客,有兴趣的读者可以自取. 本题大意: 给定两个四位的素数,没有前导零,每次变换其中的一位,最终使得两个素数相等,输出最小变换次数.要求变换过程中的数也都是素数. 本 ...
- Handler Runnable 自动执行 循环 连续 延时
这是一种可以创建多线程消息的函数使用方法:1,首先创建一个Handler对象 Handler handler=new Handler(); 2,然后创建一个Runnable对象Runnable run ...
- MDK生成.bin
方法1: 默认选择编译输出的路径输出bin fromelf.exe --bin -o "$L@L.bin" "#L" 保存编译 方法2: 在要输出的目录下,新建 ...
- Java_1简介
1.Java版本 JavaSE 基础标准版 J2ME 小型版 JavaEE 企业版(主要针对Javaweb程序进行开发) 2.Java特点 开源跨平台 跨平台的原因:Java必须先只能装 ...
- 关于控制反转(IOC)容器 ,依赖注入(DI)模式必读文章收集
推荐一篇国外設計大師Martin Fowler的大作:Inversion of Control Containers and the Dependency Injection pattern http ...
- socket网络编程扫盲篇
socket 是“套接字”的意思,是计算机之间进行通信的一种约定,也可以认为是一种技术.通过 socket 这种约定,一台计算机可以接收其他计算机的数据,也可以向其他计算机发送数据. socket 的 ...
- 1C - A + B Problem II
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum o ...
- oracle 直接复制表内容到新表
不知道为什么,刚建的oracle数据库删除数据很慢,表里面有120多万数据,非常地慢 于是采用的复制的方法,命令如下: create table students_backup as select * ...
- android xml 解析汉字只出来一个字的问题
DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); // 实例化DocumentBuilder factor ...
- 解决CentOS7-python-pip安装失败
Pip介绍 pip 是一个安装和管理 Python 包的工具,python安装包的工具有easy_install, setuptools, pip,distribute.使用这些工具都能下载并安装dj ...