http://poj.org/problem?id=2763
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个节点数,给出n-1条边的关系及边的权值,给出你的初始节点,两种操作:(1)输出从当前节点到目标节点的路径长度;(2)修改第i条路径的权值。
思路:这题本来是用LCA+树状数组做的,但是树剖也是可以做的,只要把边权化为点权,这个问题不就转换成区间求和+单点修改的问题了嘛。具体操作:设第i条边连接的两个节点分别为u、v,我们比较deep[u]和deep[v]的大小,(即比较两个节点的深度)把这个边值赋给更深的那个节点。其他的操作都和树剖一样,只是我们要修改一下区间求和的操作。因为原来的区间求和操作可能使我们多算了一条边的权值,比如从节点2到节点3,只有一条边,但是有两个点。根据树剖的原理,最后两个节点一定是在同一条链上的,如果此时u=v,那么我们肯定没有多算,直接退出函数即可;否则我们多算了一条边,这条边的权值就等于深度较浅的那个节点的权值。(不理解的话可以画图模拟一下)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=1e5+5;
struct node
{
int l,r,sum;
}tree[maxn<<2]; //线段树
struct edge
{
int to,nxt;
}Edge[maxn<<1];
struct e
{
int u,v,dis;
}E[maxn];
int head[maxn];
int siz[maxn];//子树大小
int son[maxn];//重儿子
int fa[maxn];//父节点
int deep[maxn];//深度
int top[maxn];//所在链链顶
int pos[maxn];//dfs序编号
int v[maxn];//映射到序列后的值 线段树依照此序列建树
int a[maxn];//树上各节点的值
int n,m,beg,tot=0,cnt=0;
inline void addedge(int u,int v)
{
Edge[++cnt].to=v,Edge[cnt].nxt=head[u],head[u]=cnt;
Edge[++cnt].to=u,Edge[cnt].nxt=head[v],head[v]=cnt;
}
void dfs1(int u,int f)//处理出重儿子 深度 父亲 子树大小等信息
{
siz[u]=1;
son[u]=0;
fa[u]=f;
deep[u]=deep[f]+1;
int to;
for(int i=head[u];i;i=Edge[i].nxt)
{
to=Edge[i].to;
if(to!=f)
{
dfs1(to,u);
siz[u]+=siz[to];
if(siz[son[u]]<siz[to])
son[u]=to;
}
}
}
void dfs2(int u,int f,int k)//处理出 链顶 dfs序 映射后的值 等信息
{
top[u]=k;
pos[u]=++tot;
v[tot]=a[u];
if(son[u])
dfs2(son[u],u,k);
int to;
for(int i=head[u];i;i=Edge[i].nxt)
{
to=Edge[i].to;
if(to!=f&&to!=son[u])
dfs2(to,u,to);
}
}
void build(int i,int l,int r)//建线段树
{
tree[i].l=l,tree[i].r=r;
if(l==r)
{
tree[i].sum=v[l];
return ;
}
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
int querysum(int i,int l,int r)
{
if(l==tree[i].l&&r==tree[i].r)
return tree[i].sum;
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid)
return querysum(i<<1,l,r);
else if(l>mid)
return querysum(i<<1|1,l,r);
else
return querysum(i<<1,l,mid)+querysum(i<<1|1,mid+1,r);
}
int calsum(int u,int v)//计算路径和
{
int ans=0;
while(top[u]!=top[v])//不在同一条链上 每次链顶深度较大的点往上爬
{
if(deep[top[u]]<deep[top[v]])
swap(u,v);
ans+=querysum(1,pos[top[u]],pos[u]);
u=fa[top[u]];//进入新的链
}
if(u==v) //关键
return ans;
if(deep[u]>deep[v])//进入同一条链上时再求一遍区间和 深度小的点在前面
swap(u,v);
ans+=querysum(1,pos[son[u]],pos[v]); //关键
return ans;
}
void update(int i,int p,int v)
{
if(tree[i].l==tree[i].r&&tree[i].l==p)
{
tree[i].sum=v;
return ;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(p<=mid)
update(i<<1,p,v);
else
update(i<<1|1,p,v);
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
inline void prework()
{
scanf("%d%d%d",&n,&m,&beg);
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].dis);
addedge(E[i].u,E[i].v);
}
dfs1(1,0);
for(int i=1;i<n;i++)
{
if(deep[E[i].u]>deep[E[i].v])
a[E[i].u]=E[i].dis;
else
a[E[i].v]=E[i].dis;
}
dfs2(1,0,1);
build(1,1,n);
}
inline void mainwork()
{
int op,u,v,d;
u=beg;
for(int i=0;i<m;i++)
{
scanf("%d%d",&op,&v);
if(op==0)
{
printf("%d\n",calsum(u,v));
u=v;
}
else
{
scanf("%d",&d);
if(deep[E[v].u]>deep[E[v].v])
update(1,pos[E[v].u],d);
else
update(1,pos[E[v].v],d);
}
}
}
int main()
{
prework();
mainwork();
return 0;
}