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 1
1 2 1
2 3 2
0 2
1 2 3
0 3
Sample Output
1
3
题意: 知道了一颗有 n 个节点的树和树上每条边的权值,对应两种操作:
0 x 输出 当前节点到 x节点的最短距离,并移动到 x 节点位置
1 x val 把第 x 条边的权值改为 val
首先,它可以用树剖做(然而谁会呢)
其次它可以不用树剖做(废话,不然我怎能ac)
思考这是一棵树,求两点之间的距离,可以维护各点到根的距离,两点之间的距离就是dis[x]+dis[y]-2*dis[lca(x,y)]。
边权会不断被修改,想到树状数组维护dfs序,所以是单点查询,区间修改。
就完了(似乎并不难呢)。
第一次,修改树状数组,没有修改边权,wa。
第二次,lca位运算忘了优先级,wa。
我的青春啊(还浪费了poj一页的提交记录)。
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100005;
int n,q,s,cnt,tp,dep[maxn],f[maxn][21],hd[maxn],c[maxn],begin[maxn],end[maxn],tto[maxn],w[maxn];
struct edge
{
int to,nxt,num;
}v[2*maxn];
int lowbit(int x)
{
return x&(-x);
}
int getsum(int pos)
{
int sum=0;
while(pos>0)
{
sum+=c[pos];
pos-=lowbit(pos);
}
return sum;
}
void chg(int pos,int x)
{
while(pos<=n)
{
c[pos]+=x;
pos+=lowbit(pos);
}
}
void addedge(int x,int y,int id)
{
++cnt;
v[cnt].to=y;
v[cnt].nxt=hd[x];
v[cnt].num=id;
hd[x]=cnt;
}
void dfs(int u,int val,int fa)
{
begin[u]=++tp;
dep[u]=dep[fa]+1;
chg(tp,val);
chg(tp+1,-val);
f[u][0]=fa;
for(int i=hd[u];i;i=v[i].nxt)
if(v[i].to!=fa)
{
tto[v[i].num]=v[i].to;
dfs(v[i].to,val+w[v[i].num],u);
}
end[u]=tp;
}
int lca(int a,int b)
{
if(dep[a]<dep[b])
swap(a,b);
for(int i=17;i>=0;--i)
if(dep[f[a][i]]>=dep[b])
a=f[a][i];
if(a==b)
return a;
for(int i=17;i>=0;--i)
if(f[a][i]!=f[b][i])
a=f[a][i],b=f[b][i];
return f[a][0];
}
int main()
{
scanf("%d%d%d",&n,&q,&s);
for(int i=1;i<=n-1;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
addedge(x,y,i);
addedge(y,x,i);
w[i]=z;
}
dfs(1,0,0);
for(int i=0;i<=17;i++)
for(int j=1;j<=n;j++)
f[j][i+1]=f[f[j][i]][i];
while(q--)
{
int k,x,y;
scanf("%d%d",&k,&x);
if(k==0)
{
printf("%d\n",getsum(begin[x])+getsum(begin[s])-2*getsum(begin[lca(x,s)]));
s=x;
}
else
{
scanf("%d",&y);
chg(begin[tto[x]],y-w[x]);
chg(end[tto[x]]+1,w[x]-y);
w[x]=y;
}
}
return 0;
}