B - Housewife Wind POJ - 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 X 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
边权改为点权,把值放在深的那个点,那么查询的时候,从seg[y]+1到seg[x]
0 u,查询,s走到u的最短时间
1 x y 把第x条边的权值改为y
#include <cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=100005;
int n,m,s,e,tol,ans;
int father[maxn],dep[maxn],Size[maxn],son[maxn],val[maxn],to[maxn*2],cost[maxn*2],head[maxn],nxt[maxn*2];
int top[maxn],rev[maxn],seg[maxn];
int tree[maxn<<2];
int edge_u[maxn],edge_v[maxn],edge_c[maxn];
void add(int u,int v,int w)
{
e++;
to[e]=v;
cost[e]=w;
nxt[e]=head[u];
head[u]=e;
}
void dfs1(int u,int fa,int deep)
{
father[u]=fa;
dep[u]=deep;
Size[u]=1;
for(int i=head[u];i!=-1;i=nxt[i])
{
int v=to[i];
if(v==fa) continue;
dfs1(v,u,deep+1);
Size[u]+=Size[v];
if(Size[son[u]]<Size[v]) son[u]=v;
}
}
void dfs2(int u,int fa)
{
if(son[u])
{
seg[son[u]]=++tol;
top[son[u]]=top[u];
rev[tol]=son[u];
dfs2(son[u],u);
}
for(int i=head[u];i!=-1;i=nxt[i])
{
int v=to[i];
if(v==fa||v==son[u]) continue;
seg[v]=++tol;
top[v]=v;
rev[tol]=v;
dfs2(v,u);
}
}
void build(int l,int r,int rt)
{
tree[rt]=0;
if(l==r) return;
int mid=(l+r)/2;
build(l,mid,2*rt);
build(mid+1,r,2*rt+1);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return tree[rt];
}
int mid=(l+r)/2;
int left=0,right=0;
if(L<=mid) left+=query(L,R,l,mid,2*rt);
if(R>mid) right+=query(L,R,mid+1,r,2*rt+1);
return left+right;
}
void update(int x,int y,int l,int r,int rt)
{
if(l==r)
{
tree[rt]=y;
return;
}
int mid=(l+r)/2;
if(x<=mid) update(x,y,l,mid,2*rt);
else update(x,y,mid+1,r,2*rt+1);
tree[rt]=tree[2*rt]+tree[2*rt+1];
}
void work(int x,int y)
{
ans=0;
int fx=top[x],fy=top[y];
while(fx!=fy)
{
if(dep[fx]<dep[fy]) swap(fx,fy),swap(x,y);
ans+=query(seg[fx],seg[x],1,n,1);
x=father[fx];
fx=top[x];
}
if(x==y)
{
printf("%d\n",ans);
return;
}
if(dep[x]<dep[y]) swap(x,y);
ans+=query(seg[y]+1,seg[x],1,n,1);
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&s))
{
memset(son,0,sizeof(son));
memset(head,-1,sizeof(head));
e=tol=ans=0;
for(int i=1;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
edge_u[i]=u,edge_v[i]=v,edge_c[i]=w;
add(edge_u[i],edge_v[i],edge_c[i]);
add(edge_v[i],edge_u[i],edge_c[i]);
}
dfs1(1,-1,0);
seg[1]=top[1]=rev[1]=++tol;
dfs2(1,0);
build(1,n,1);
for(int i=1;i<n;i++)
{
int u=edge_u[i],v=edge_v[i];
if(dep[u]<dep[v]) swap(u,v);
update(seg[u],edge_c[i],1,n,1);
}
while(m--)
{
int Q,x,y;
scanf("%d",&Q);
if(Q==0)
{
scanf("%d",&x);
work(s,x);
s=x;
}
else
{
scanf("%d%d",&x,&y);
int u=edge_u[x],v=edge_v[x];
if(dep[u]<dep[v]) swap(u,v);
update(seg[u],y,1,n,1);
}
}
}
return 0;
}