CodeForces 916E Jamie and Tree(树链剖分+LCA)
To your surprise, Jamie is the final boss! Ehehehe.
Jamie has given you a tree with n vertices, numbered from 1 to n. Initially, the root of the tree is the vertex with number 1. Also, each vertex has a value on it.
Jamie also gives you three types of queries on the tree:
1 v — Change the tree's root to vertex with number v.
2 u v x — For each vertex in the subtree of smallest size that contains u and v, add x to its value.
3 v — Find sum of values of vertices in the subtree of vertex with number v.
A subtree of vertex v is a set of vertices such that v lies on shortest path from this vertex to root of the tree. Pay attention that subtree of a vertex can change after changing the tree's root.
Show your strength in programming to Jamie by performing the queries accurately!
The first line of input contains two space-separated integers n and q (1 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of vertices in the tree and the number of queries to process respectively.
The second line contains n space-separated integers a1, a2, ..., an ( - 108 ≤ ai ≤ 108) — initial values of the vertices.
Next n - 1 lines contains two space-separated integers ui, vi (1 ≤ ui, vi ≤ n) describing edge between vertices ui and vi in the tree.
The following q lines describe the queries.
Each query has one of following formats depending on its type:
1 v (1 ≤ v ≤ n) for queries of the first type.
2 u v x (1 ≤ u, v ≤ n, - 108 ≤ x ≤ 108) for queries of the second type.
3 v (1 ≤ v ≤ n) for queries of the third type.
All numbers in queries' descriptions are integers.
The queries must be carried out in the given order. It is guaranteed that the tree is valid.
For each query of the third type, output the required answer. It is guaranteed that at least one query of the third type is given by Jamie.
6 7
1 4 2 8 5 7
1 2
3 1
4 3
4 5
3 6
3 1
2 4 6 3
3 4
1 6
2 2 4 -5
1 4
3 3
27
19
5
4 6
4 3 5 6
1 2
2 3
3 4
3 1
1 3
2 2 4 3
1 1
2 2 4 -3
3 1
18
21
The following picture shows how the tree varies after the queries in the first sample.
题意(来自mangoyang大佬)
有一棵n个节点的有根树,标号为1-n,你需要维护一下三种操作
1.给定一个点v,将整颗树的根变为v
2.给定两个点u, v,将lca(u, v)所在的子树都加上x
3.给定一个点v,你需要回答以v所在的子树的权值和
题解:
这道题大概有以下几个难点
首先是换根情况下的子树权值和查询,但如果你做过洛谷P3979遥远的国度这道题,而且你还是用树链剖分写的,你大致就会做这个东西了。
维护一个LCA,然后从根节点跳到当前这个子树根节点的儿子所处的深度,记这个点为A,如果A点的父亲是我们询问的子树根节点,那么根节点在子树中,此时当前的新子树就是除了A点和A点的原子树以外的所有点,否则的话就是子树根节点的原子树,当然也要特判一下,如果根节点刚好是要查询的子树根节点,那么查询范围就是全部节点
修改也是同理的
然后是包括u和v的最小子树,画一下图就可以知道子树的根节点应该是u到根节点的路径与v到根节点的路径的第一个交点
这个交点可以通过对根和u,根和v,v和u两两取LCA后取深度最大的点获得,挺简单的想法,可以自行模拟体会一下
假设我们已经获得了这个根节点,那么之后的操作就是换根情况下子树的权值修改了,这在上面已经提到过了,就不再赘述了。
CodeForces 916E Jamie and Tree(树链剖分+LCA)的更多相关文章
- Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树
D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...
- CodeForces 343D water tree(树链剖分)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- Water Tree(树链剖分+dfs时间戳)
Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
随机推荐
- 基于hi-nginx的web开发(python篇)——utf-8编码
一致地utf-8编码,非常重要.对python2而言,尤其如此. 如果在hi-nginx中使用的是python2,同时又需要无障碍地使用中日韩等文字,那么一定不要忘记使用: #-*- coding:u ...
- 【Linux】 用户管理
Linux用户管理 ■ 查看用户整体情况 cat /etc/passwd可以查看用户的一些基本信息.用finger <user>似乎更加方便 查看某一个特定的用户的话就可以 grep &l ...
- c++ --> sizeof()使用小结
sizeof()使用小结 特性0:sizeof是运算符,不是函数 sizeof最基本特性,后面的很多特性都是受到这个特性的影响,正因为sizeof不是函数,因此不把它所要求得长度的对象叫做参数,习惯上 ...
- 如何打包静态库.a文件 iOS
代码调试好了开始打包成sdk,下面是将要打包的FRSDK代码(FRSDK.h暴露在外面有别人调用) 1.创建新工程(Xcode File-New-Project) 2.把下面的红色框的东西移除 3.将 ...
- [BZOJ 1190][HNOI2007]梦幻岛宝珠
1190: [HNOI2007]梦幻岛宝珠 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1057 Solved: 611[Submit][Stat ...
- 2017-2018-1 20155201 《信息安全系统设计基础》 pwd命令的实现
2017-2018-1 20155201 <信息安全系统设计基础> pwd命令的实现 一.对pwd命令的学习 在终端中输入man pwd查看手册中对pwd这一命令的解释: 以绝对路径的方式 ...
- Beta冲刺 第六天
Beta冲刺 第六天 1. 昨天的困难 1.对于设计模式的应用不熟悉,所以在应用上出现了很大的困难. 2.SSH中数据库的管理是用HQL语句实现的,所以在多表查询时出现了很大的问题. 3.页面结构太凌 ...
- 20162330 实验三 《敏捷开发与XP实践》 实验报告
2016-2017-2 实验报告目录: 1 2 3 4 5 20162330 实验三 <敏捷开发与XP实践> 实验报告 课程名称:<程序设计与数据结构> 学生班级:1623班 ...
- from nltk.book import * 出错的解决方法
import nltknltk.download() 在使用上面命令安装了nltk库并运行下载后,再输入from nltk.book import * 往往会出现这样的错误提示: 出现这种错误往往是由 ...
- 《高级软件测试》Windows平台Jira的配置
昨天完成了Jira的下载,很开心地去睡觉等明天天亮秒配环境愉快进行使用,撰写文档,开始徜徉于软件管理测试实践,早日走向代码巅峰. 我们把安装和配置的过程来走一遍. 安装完成汤姆猫长这样子: 安装Jir ...