2014广州网络预选赛1008(树链剖分)HDU5029

来源:互联网 发布:淘宝招聘网最新招聘 编辑:程序博客网 时间:2024/04/29 17:18

Relief grain

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 332    Accepted Submission(s): 78


Problem Description
The soil is cracking up because of the drought and the rabbit kingdom is facing a serious famine. The RRC(Rabbit Red Cross) organizes the distribution of relief grain in the disaster area.

We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.

There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.
 

Input
The input consists of at most 25 test cases.

For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.

The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village.
  
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)

The input ends by n = 0 and m = 0.
 

Output
For each test case, output n integers. The i-th integer denotes the type that is distributed the most times in the i-th village. If there are multiple types which have the same times of distribution, output the minimal one. If there is no relief grain in a village, just output 0.
 

Sample Input
2 41 21 1 11 2 22 2 22 2 15 31 23 13 45 32 3 31 5 23 3 30 0
 

Sample Output
1223302

题意:RT

思路:树链剖分,比赛的时候不会树链剖分,但是用LCT又yy不出来,只好哭等比赛结束,sad~

            今天去看了树链剖分,才豁然开朗啊(LCT也是可做的)

            用一个vector维护每条重链即可,如果要在u和v之间路径上的所有点加一个数z

            那么就在u和v路径上的所有重链上都将z加上去,因为每条重链上的点转化为线性结构以后在线段上一定是连续的一段区间

            那么就转化为了在一个区间里加z,这个可以用经典操作[l,r],在l这里+z,在r+1这里-z,那么前缀和就是该点的值

            其中对每条路径的操作复杂度为logn,最后的查询用线段树来维护区间最优值,其中线段树维护的是z,操作只有+1或-1

            具体看代码吧~~

0 0
原创粉丝点击