Housewife Wind
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 7509 | Accepted: 1952 |
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
题意:给你一棵树,边上有权值,询问操作有修改边上的权值,和查询两个点之间路径的权值和。
思路:树链刨分模板题,没什么好说的。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
int u,v,cost;
}arr[100010];
struct node2
{
int u,v,cost,next;
}edge[200010];
int T,t,n,m,tot,tot2,Head[100010];
int fa[100010],depth[100010],siz[100010],son[100010],top[100010],p[100010];
int tree[100010];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
for(;x<=tot2;x+=lowbit(x))
tree[x]+=num;
}
int query(int x)
{
int ret=0;
for(;x>0;x-=lowbit(x))
ret+=tree[x];
return ret;
}
void add(int u,int v,int cost)
{
edge[tot].u=u;
edge[tot].v=v;
edge[tot].cost=cost;
edge[tot].next=Head[u];
Head[u]=tot++;
}
void dfs1(int u)
{
int i,j,k,v;
siz[u]=1;
son[u]=0;
for(i=Head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v==fa[u])
continue;
fa[v]=u;
depth[v]=depth[u]+1;
dfs1(v);
if(siz[v]>siz[son[u]])
son[u]=v;
siz[u]+=siz[v];
}
}
void dfs2(int u,int f)
{
int i,j,k,v;
p[u]=++tot2;
top[u]=f;
if(son[u]!=0)
dfs2(son[u],f);
for(i=Head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v==son[u] || v==fa[u])
continue;
dfs2(v,v);
}
}
int solve(int u,int v)
{
int f1=top[u],f2=top[v],k,ret=0;
while(f1!=f2)
{
if(depth[f1]<depth[f2])
{
swap(u,v);
swap(f1,f2);
}
ret+=query(p[u])-query(p[f1]-1);
u=fa[f1];
f1=top[u];
}
if(u==v)
return ret;
if(depth[u]>depth[v])
swap(u,v);
ret+=query(p[v])-query(p[son[u]]-1);
return ret;
}
int main()
{
int i,j,k,s,u,v,cost,op,ans;
while(~scanf("%d%d%d",&n,&m,&s))
{
memset(Head,-1,sizeof(Head));
tot=tot2=0;
for(i=1;i<n;i++)
{
scanf("%d%d%d",&u,&v,&cost);
arr[i].u=u;
arr[i].v=v;
arr[i].cost=cost;
add(u,v,cost);
add(v,u,cost);
}
depth[1]=1;
dfs1(1);
dfs2(1,1);
for(i=1;i<n;i++)
if(fa[arr[i].u]==arr[i].v)
swap(arr[i].u,arr[i].v);
memset(tree,0,sizeof(tree));
for(i=1;i<n;i++)
update(p[arr[i].v],arr[i].cost);
u=s;
while(m--)
{
scanf("%d",&op);
if(op==0)
{
scanf("%d",&v);
ans=solve(u,v);
printf("%d\n",ans);
u=v;
}
else
{
scanf("%d%d",&k,&cost);
update(p[arr[k].v],cost-arr[k].cost);
arr[k].cost=cost;
}
}
}
}