Road
题目链接:
http://acm.split.hdu.edu.cn/showproblem.php?pid=5861
Description
There are n villages along a high way, and divided the high way into n-1 segments. Each segment would charge a certain amount of money for being open for one day, and you can open or close an arbitrary segment in an arbitrary day, but you can open or close the segment for just one time, because the workers would be angry if you told them to work multiple period. We know the transport plan in the next m days, each day there is one cargo need to transport from village ai to village bi, and you need to guarantee that the segments between ai and bi are open in the i-th day. Your boss wants to minimize the total cost of the next m days, and you need to tell him the charge for each day. (At the beginning, all the segments are closed.)Input
Multiple test case. For each test case, begins with two integers n, m(1<=n,m<=200000), next line contains n-1 integers. The i-th integer wi(1<=wi<=1000) indicates the charge for the segment between village i and village i+1 being open for one day. Next m lines, each line contains two integers ai,bi(1≤ai,bi<=n,ai!=bi).Output
For each test case, output m lines, each line contains the charge for the i-th day.Sample Input
4 3 1 2 3 1 3 3 4 2 4Sample Output
3 5 5Source
2016 Multi-University Training Contest 10##题意: 水平线上n个村子间有 n-1 条路. 每条路开放一天的价格为 Cost_i. 有 m 天的操作,每天需要用到村子 Ai~Bi 间的道路. 每条路只能开放或关闭一次. (不能重复开关) 求每天的最小花费.
##题解: 用了两次线段树维护,写得很丑,还不如写两次线段树. 第一次线段树: 维护每条路第一次和最后一次被用到的天数. 以下代码维护了 mi:第一天,ma:最后一天,has:是否已经被用过. mi和ma分别是子节点的最小值和最大值,关键是 has 的维护. (一开始没有写has, WA了) has表示当前区间的结点是否都已经被用过.(即有mi值). 如果has=1那么就可以直接用lazy维护区间中所有结点的最大值. 若has=0意味着某个子节点还没有被用过, 若当前区间被lazy维护而没有更新到点,那么这个子节点的最小值就可能被改变. 这个地方关于维护的对象和lazy的处理做的不是很好,不过幸好过了. 晚点再改一下代码. 第二次线段树: 维护每天用了多少钱. 根据以上结果维护,对于每条路(被用到过的)区间更新第一次到最后一次这些天用的钱.
##代码: ``` cpp #include