经典题,动态修改树上的边权,询问树上2点距离。LCA + 树状数组。
len[i] 表示 点 i 到根距离
容易看出,修改一条边必然使一棵子树所有点的 len 增加相同的数,由此问题迎刃而解。
Housewife Wind
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 6554 | Accepted: 1680 |
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?
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.
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
Source
POJ Monthly--2006.02.26,zgl & twb
/*
ID:huang_l1
LANG:C++
PROG:combo
*/
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define prt(k) cout<<#k" = "<<k<<endl;
const int N = 100005;
int head[N], n, m, dep[N], len[N]; /// len[i] 表示 i 到根距离
int val[N];
struct Edge
{
int to, next, w;
}e[N << 1];
int f[N][22];
int mm;
int p[N];
void add(int u, int v, int w=1)
{
e[mm] = (Edge){v, head[u], w};
head[u] = mm++;
}
int cur;
int L[N], R[N];
int to[N];
int cost[N << 1];
void dfs(int u, int fa)
{
L[u] = ++cur;
f[u][0] = fa;
p[u] = fa;
dep[u] = dep[fa] + 1;
for (int i=head[u]; ~i; i=e[i].next)
{
int v = e[i].to;
if (v == fa) continue;
to[i / 2] = v; /// to[i] 表示第 i 条边连接的儿子
len[v] = len[u] + e[i].w;
dfs(v, u);
}
R[u] = cur;
}
int maxh;
void gao(int root = 1)
{
int j;
for (j=1;(1<<j)<n;j++) {
for (int i=1;i<=n;i++)
{
f[i][j] = f[ f[i][j-1] ][j-1];
}
}
maxh = j - 1;
}
int swim(int x, int k)
{
for (int i=maxh;i>=0;i--) {
if (k >> i & 1)
x = f[x][i];
}
return x;
}
int LCA(int x, int y)
{
if (dep[x] > dep[y]) swap(x, y);
y = swim(y, dep[y]-dep[x]);
if (x == y) return x;
for (int i=maxh;i>=0;i--) {
if (f[x][i] != f[y][i]) {
x = f[x][i], y = f[y][i];
}
}
return f[x][0];
}
#include <vector>
#include <algorithm>
int Q, pos;
int tree[N << 2];
int low(int x) { return x&-x; }
void Add(int p, int x) {
for(;p>0;p-=low(p)) tree[p]+=x;
}
void Add(int l, int r, int x)
{
Add(r, x); Add(l-1, -x);
}
int sum(int p)
{
int ret = 0;
for(;p<N;p+=low(p)) ret += tree[p];
return ret;
}
int getlen(int u)
{
return sum(L[u]);
}
int main()
{
while (cin>>n>>Q>>pos) {
mm = 0;
memset(head, -1, sizeof head);
for (int i=0;i<n-1;i++) {
int u,v,w;
scanf("%d%d%d", &u, &v, &w);
cost[i] = w;
add(u,v,w); add(v,u,w);
}
cur = 0;
dfs(1, 0);
gao();
memset(tree, 0, sizeof tree);
for(int i=1;i<=n;i++) Add(L[i], L[i], len[i]);
while (Q--) {
int op; scanf("%d", &op);
if (op == 0) {
int u; scanf("%d", &u);
int lca = LCA(pos, u);
int ans = getlen(pos) + getlen(u) - 2*getlen(lca);
printf("%d\n", ans);
pos = u;
}
else {
int id, w;
scanf("%d%d", &id, &w);
id--;
int u = to[id];
Add(L[u], R[u], w - cost[id]);
cost[id] = w;
}
}
}
}