POJ2763-Housewife Wind

来源:互联网 发布:淘宝手办散货 编辑:程序博客网 时间:2024/05/28 16:04

Housewife Wind
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 12443 Accepted: 3437

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 11 2 12 3 20 21 2 30 3

Sample Output

13

Source

POJ Monthly--2006.02.26,zgl & twb


题意:给出一棵无根树。开始的时候你在1号节点。有两种操作:1.求你的位置到x的位置的距离,然后你走到x点。2.把第x条边边权改成y

解题思路:树链剖分,把边权给点权即可



#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int n, m, x, y, z;int s[200009], nt[200009], e[200009], v[200009], cnt,ss,tt;int ct[200009], mx[200009], fa[200009], dep[200009];int top[200009], g[200009],G[200009], a[200009],sum[200009];int lowbit(int x){return x&-x;}void add(int x, int y){while (x<=n){sum[x] += y;x += lowbit(x);}}int getsum(int x){int res = 0;while (x){res += sum[x];x -= lowbit(x);}return res;}void dfs(int x, int f){dep[x] = dep[f] + 1;fa[x] = f; ct[x] = 1; mx[x] = 0;for (int i = s[x]; ~i; i = nt[i]){if (e[i] == f) continue;dfs(e[i], x);ct[x] += ct[e[i]];if (ct[e[i]] > ct[mx[x]]) mx[x] = e[i];}}void Dfs(int x, int t){top[x] = !t ? x : top[fa[x]];g[x] = ++cnt;if (mx[x]) Dfs(mx[x], 1);for (int i = s[x]; ~i; i = nt[i]){if (e[i] == fa[x]) continue;if (e[i] == mx[x]) { G[v[i]] = g[e[i]]; continue; }Dfs(e[i], 0);G[v[i]] = g[e[i]];}}int getans(int x, int y){int ans = 0;while (top[x] != top[y]){if (dep[top[x]] < dep[top[y]]) swap(x, y);ans += getsum(g[x]) - getsum(g[top[x]] - 1);x = fa[top[x]];}if (dep[x] > dep[y]) swap(x, y);return ans + getsum(g[y]) - getsum(g[x]);}int main(){while (~scanf("%d%d%d", &n, &m,&ss)){memset(s,- 1, sizeof s);memset(sum, 0, sizeof sum);dep[0] = ct[0] = cnt = 0;for (int i = 1; i < n; i++){scanf("%d%d%d", &x, &y,&a[i]);nt[cnt] = s[x], s[x] = cnt,v[cnt]=i,e[cnt++] = y;nt[cnt] = s[y], s[y] = cnt,v[cnt]=i,e[cnt++] = x;}dfs(1, 0);Dfs(1, cnt = 0);for (int i = 1; i < n; i++) add(G[i], a[i]);while (m--){scanf("%d", &x);if (!x){scanf("%d", &tt);printf("%d\n", getans(ss, tt));ss = tt;}else{scanf("%d%d", &y, &z);add(G[y], -a[y]);add(G[y], a[y] = z);}}}return 0;}