Housewife Wind
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 6656 | Accepted: 1695 |
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
此题由于需要修改边权,而且我们可以发现如果修改一条边,那么他会影响所有经过这条边的路径长度,这里有点类似线段树的成段更新,于是我们想了一个办法,利用树的dfs序来维护,由于一个树有n-1条边,而dfs序有2n个值,我们去掉根节点的两个值,那么每条边会对应两个值,是哪两个值呢?我们利用在dfs时搜索(u,v)边时,因为这时刚进入,于是我们用l[u]这一点来对应(u,v)的进入,而用r[u]这一点来对应(v,u),这里l[u]表示第一次访问u的时间戳,r[u]表示访问完所有u的子树回溯到u的时间戳,可以发现在l[u]到r[u]的这段时间里,访问了u的所有子树,如果我们让l[u]变为(u,v)的权值,r[u]表示(u,v)权值的相反数,那么从l[u]到r[u]的这段区间和刚好为0,这正是我们想要的,任意一点到根的路径上会携带一些访问过的子树,而这些子树权值和为0,因此这样前缀和刚好表示该点到根的路径长度。任意两点的路径长度即可利用LCA来求。
具体参看代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 200010
using namespace std;
int n,c[Maxn];
void update(int x,int d){
while(x<=2*n){
c[x]+=d;
x+=x&-x;
}
}
int sum(int x){
int ans=0;
while(x){
ans+=c[x];
x-=x&-x;
}
return ans;
}
struct edge{
int to,w,id,next;
}p[Maxn<<1];
int head[Maxn];
int tot;
void addedge(int a,int b,int c,int d){
p[tot].to=b;
p[tot].w=c;
p[tot].id=d;
p[tot].next=head[a];
head[a]=tot++;
}
int pw[Maxn<<1];
int dp[Maxn<<1][30];
int es[Maxn<<1];
int d[Maxn<<1];
int fs[Maxn];
int num,dfn;
int ed[Maxn];
int l[Maxn],r[Maxn];
int mymin(int x,int y){
return d[x]<d[y]?x:y;
}
void rmq_init(int n){
pw[0]=-1;
for(int i=1;i<=n;i++)
pw[i]=i&i-1?pw[i-1]:pw[i-1]+1;
for(int i=1;i<=n;i++) dp[i][0]=i;
for(int j=1;j<=pw[n];j++)
for(int i=1;i+(1<<j)-1<=n;i++)
dp[i][j]=mymin(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
}
int rmq_ask(int l,int r){
l=fs[l],r=fs[r];
if(l>r) swap(l,r);
int k=pw[r-l+1];
return es[mymin(dp[l][k],dp[r-(1<<k)+1][k])];
}
void dfs(int u,int fa,int sp){
es[++num]=u;
d[num]=sp;
fs[u]=num;
l[u]=++dfn;
for(int i=head[u];i!=-1;i=p[i].next){
int v=p[i].to;
if(v!=fa){
ed[p[i].id]=v;
dfs(v,u,sp+1);
es[++num]=u;
d[num]=sp;
}
}
r[u]=++dfn;
}
void init(){
tot=num=dfn=0;
memset(head,-1,sizeof head);
}
void LCA(int n){
dfs(1,-1,0);
rmq_init(2*n-1);
}
int val[Maxn];
int main()
{
int q,s,a,b,w,x,y,z,t;
while(cin>>n>>q>>s){
init();
for(int i=1;i<n;i++){
scanf("%d%d%d",&a,&b,&w);
addedge(a,b,w,i);
addedge(b,a,w,i);
}
LCA(n);
for(int i=0;i<tot;i+=2){
t=ed[p[i].id];
update(l[t],p[i].w);
update(r[t],-p[i].w);
val[l[t]]=p[i].w;
val[r[t]]=-p[i].w;
}
while(q--){
scanf("%d%d",&x,&y);
if(x==0){
printf("%d\n",sum(l[s])+sum(l[y])-2*sum(l[rmq_ask(s,y)]));
s=y;
}
else{
scanf("%d",&z);
t=ed[y];
update(l[t],z-val[l[t]]);
update(r[t],val[l[t]]-z);
val[l[t]]=z;
val[r[t]]=-z;
}
}
}
return 0;
}