轻量级前端框架助力开发者提升项目效率与性能
571
2022-10-03
HDU 3966 Aragorn's Story (树链剖分)
Problem Description
Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time.
Input
Multiple test cases, process to the end of input.For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1.The next line contains N integers A1, A2, …AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies.The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v.The next P lines will start with a capital letter ‘I’, ‘D’ or ‘Q’ for each line.‘I’, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps.‘D’, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps.‘Q’, followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.
Output
For each query, you need to output the actually number of enemies in the specified camp.
Sample Input
3 2 51 2 32 12 3I 1 3 5Q 2D 1 2 2Q 1 Q 3
Sample Output
748
题意
在一棵树中,我们定义了两种操作:
给树中u->v 路径上的所有点的权值增加或减少k查询树中的某一个点的当前权值
思路
基础的树链剖分题目,先对树进行剖分,然后用线段树或者树状数组等去维护这些链。
树状数组的话我们可以利用区间减法的性质,把对一个区间 [a,b] 的更新拆解成对两个区间 [0,a]、[0,b] 相反的更新。
AC 代码
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~