HDU - 3966 Aragorn's Story(树链剖分入门+线段树)
HDU - 3966
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 5 Sample Output 7 Hint 1.The number of enemies may be negative. 2.Huge input, be careful. Source 题解:题目大意是给你一棵树,树有对应的点权,然后给你q个命令,可以进行对某条链上的所有点值进行加减,或者对某个点的权值进行查询。
这是一道典型的树链剖分问题。所谓树链剖分就是将树上的点按链的方式连续标号,类似离散化,这样我们就可以利用这个特性,讲问题改变为区间问题,就可以套用线段树或者树状数组去解决这一类问题。
如上图,经过树链剖分算法后,每个点离散后的id值为括号上的值。跑完树链剖分后,我们把树分成了四条链,分别为1-4-8-9-12-13,2-3,5-6-7,10-11,注意到链上的点都是连续的,当我们要进行区间操作时,可以套个线段树。 那树链剖分又是怎么进行的呢?首先要把树分成几条链,分链前先介绍一个概念——重儿子。我们先定义一个数组size保存当前结点的所有子树上的点的个数,如上图size[3]=1,size[2]=2,size[9]=5...而一个结点的重儿子就是该结点的儿子中size的值最大的那个,比如1的重儿子是4,4的重儿子是8。知道重儿子后,就可以分链了。我们只要遍历每一个结点,把每个结点和他的重儿子连接起来,这样就把链分开了,但因为我们要对他进行顺序标号,所以可以利用dfs标号 代码如下:(可以按照上图模拟一下,模拟几次就懂了) #include <iostream> |
HDU - 3966 Aragorn's Story(树链剖分入门+线段树)的更多相关文章
- HDU 4366 Successor(树链剖分+zkw线段树+扫描线)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...
- bzoj 4034 [HAOI2015] T2(树链剖分,线段树)
4034: [HAOI2015]T2 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1536 Solved: 508[Submit][Status] ...
- bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 10677 Solved: 4313[Submit ...
- poj 3237 Tree(树链剖分,线段树)
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 7268 Accepted: 1969 Description ...
- bzoj 3626 [LNOI2014]LCA(离线处理+树链剖分,线段树)
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1272 Solved: 451[Submit][Status ...
- bzoj 2243 [SDOI2011]染色(树链剖分,线段树)
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4637 Solved: 1726[Submit][Status ...
- 【BZOJ3531】旅行(树链剖分,线段树)
[BZOJ3531]旅行(树链剖分,线段树) 题面 Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足 从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教 ...
- 【BZOJ5507】[GXOI/GZOI2019]旧词(树链剖分,线段树)
[BZOJ5507][GXOI/GZOI2019]旧词(树链剖分,线段树) 题面 BZOJ 洛谷 题解 如果\(k=1\)就是链并裸题了... 其实\(k>1\)发现还是可以用类似链并的思想,这 ...
- [bzoj4196][Noi2015]软件包管理器_树链剖分_线段树
软件包管理器 bzoj-4196 Noi-2015 题目大意:Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件 ...
随机推荐
- android开发之res下的menu (xml+代码的形式)
转载请注明出处:http://blog.csdn.net/fth826595345/article/details/9199393 先来看Menu XML文件如何编写: <?xml versi ...
- 筛选实现C++实现筛选法
每日一贴,今天的内容关键字为筛选实现 筛选法 分析: 筛选法又称筛法,是求不超越自然数N(N>1)的全部质数的一种方法.据说是古希腊的埃拉托斯特尼(Eratosthenes,约公元前274-19 ...
- React的生命周期
我们先来看一张图,其实看完这张图基本就懂了,如果还不懂,请继续往下看. getDefaultProps 执行过一次后,被创建的类会有缓存,映射的值会存在this.props,前提是这个prop不是父组 ...
- C语言之if和switch的分别
If和switch的互换规则 用if语句能实现的东西,用switch也可以实现 用switch语句能实现的东西,用if也可以实现 1.如果是判断范围的时候,用switch不太好做,没法直接做 2.如果 ...
- 如何一步一步用DDD设计一个电商网站(十一)—— 最后的准备
阅读目录 前言 准备 实现 结语 一.前言 最近实在太忙,上周停更了一周.按流程一步一步走到现在,到达了整个下单流程的最后一公里——结算页的处理.从整个流程来看,这里需要用户填写的信息是最多的,那么 ...
- css修改滚动条默认样式
之前因为公司项目需要,在网上找到的: 直接上代码了 html代码 <div class="inner"> <div class="innerbox&qu ...
- CentOS 7下安装X Window
1.网上其他人都这么说: yum check-update yum groupinstall "X Window System" ... 但是运行yum groupinstall ...
- 【CSS学习笔记】CSS初始化
腾讯QQ官网(http://www.qq.com)样式初始化 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input ...
- 50道java线程面试题
50道Java线程面试题 下面是Java线程相关的热门面试题,你可以用它来好好准备面试. 1) 什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序 ...
- CrashMe分析教程1 - BreakPoint
首先,谢谢 Robert Kuster 为我们提供了这么好的CrashMe项目. 很多人想寻找一个CrashMe分析的教程, 我也想要, 但是似乎网络里没有, 所以我就决定用业余时间写一个小系列来共享 ...