[原创]POJ 2763 Housewife Wind [树链剖分(边权)+树状数组]【数据结构】
[原创]POJ 2763 Housewife Wind [树链剖分(边权)+树状数组]【数据结构】
2017-03-09 22:59:37 Tabris_ 阅读数:279
博客爬取于2020-06-14 22:41:24
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/60982742
题目连接:http://poj.org/problem?id=2763
--------------------------------------------------------------------------.
Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 10703 Accepted: 2969
Description
After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique.
Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: ‘Mummy, take me home!’
At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.
Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?
Input
The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001.
The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.
The following q lines each is one of the following two types:
Message A: 0 u
A kid in hut u calls Wind. She should go to hut u from her current position.
Message B: 1 i w
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid.
Output
For each message A, print an integer X, the time required to take the next child.
Sample Input
3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3
Sample Output
1
3
--------------------------------------------------------------------------.
题目大意:
一颗n个节点的生成树,有边权,一个人开始在s位置,有两种操作,
1) 0 a 从s走到n 输出路径长度
2) 1 a b 将第a条边的权值变成b
解题思路:
这题就是基于边权的树链剖分。找路径的和我们可以采用树状数组维护
其实边权对于点权来说,基本一样,我们只要将u->v的权值给u,v中距离根节点远的就行了
然后查找的时候再讲lca(u,v)这个点的权值减去 得到的就是u->v的路径长度
注意下本题卡时间卡的非常紧
vector是不行的 要用前向星 .
附本题代码
--------------------------------------------------------------------------.
1 | # include <stdio.h> |