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


思路:树链剖分,每条路径下面的点记录该条路径的权值,之后就是单点更新区间查询的操作了.


#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN=101234;
int sum[MAXN*4],id_data[MAXN],add[MAXN*4];
int top[MAXN],tid[MAXN],son[MAXN],siz[MAXN],data[MAXN];
int fa[MAXN],deep[MAXN];
int n,m,s;
int ans;
void build(int root,int l,int r)
{
if(l==r)
{
sum[root]=id_data[l];
return ;
}
int mid=(l+r)/2;
build(root*2,l,mid);
build(root*2+1,mid+1,r);
sum[root]=sum[root*2]+sum[root*2+1];
}
//void push_down(int root,int ln,int rn)
//{
// if(add[root])
// {
// add[root*2]+=add[root];
// add[root*2+1]+=add[root];
// sum[root*2]+=add[root]*ln;
// sum[root*2+1]+=add[root]*rn;
// add[root]=0;
// }
//}
void updata(int L,int C,int root,int l,int r)
{
if(l==r)
{
sum[root]=C;
//add[root]+=C;
return ;
}
int mid=(l+r)/2;
// push_down(root,mid-l+1,r-mid);
if(L<=mid)
updata(L,C,root*2,l,mid);
else if(L>mid)
updata(L,C,root*2+1,mid+1,r);
sum[root]=sum[root*2]+sum[root*2+1];
}
void query(int L,int R,int root,int l,int r)
{
if(L<=l&&R>=r)
{
ans+=sum[root];
return ;
}
int mid=(l+r)/2;
// push_down(root,mid-l+1,r-mid);
if(L<=mid)
query(L,R,root*2,l,mid);
if(R>mid)
query(L,R,root*2+1,mid+1,r);
}
int head[MAXN];
int cnt;
int _cnt;
struct node
{
int u,v,w,next;
} a[MAXN*2];
void Add(int u,int v,int w)
{
a[cnt].u=u;
a[cnt].v=v;
a[cnt].w=w;
a[cnt].next=head[u];
head[u]=cnt++;
}
void dfs1(int u,int f,int d)
{
siz[u]=1,deep[u]=d;
fa[u]=f,son[u]=-1;
for(int i=head[u]; i!=-1; i=a[i].next)
{
int v=a[i].v;
if(v==f)
continue ;
dfs1(v,u,d+1);
siz[u]+=siz[v];
if(son[u]==-1||siz[son[u]]<siz[v])
son[u]=v;
}
}
void getnum(int u,int fa)
{
for(int i=head[u]; i!=-1; i=a[i].next)
{
if(a[i].v!=fa)
data[a[i].v]=a[i].w;
if(a[i].v!=fa)
getnum(a[i].v,u);
}
}
void dfs2(int u,int t)
{
top[u]=t,tid[u]=_cnt++;
id_data[_cnt-1]=data[u];
if(son[u]!=-1)
dfs2(son[u],t);
for(int i=head[u]; i!=-1; i=a[i].next)
{
int v=a[i].v;
if(son[u]!=v&&fa[u]!=v)
{
dfs2(v,v);
}
}
}
void init()
{
_cnt=1;
dfs1(1,-1,0);
getnum(1,1);
dfs2(1,1);
}
void Query(int x,int y)
{
int fx=top[x],fy=top[y];
while(fx!=fy)
{
if(deep[fx]<deep[fy])
{
swap(x,y);
swap(fx,fy);
}
query(tid[fx],tid[x],1,1,n);
x=fa[fx],fx=top[x];
}
if(deep[x]==deep[y]) return ;
if(deep[x]<deep[y]) swap(x,y);
query(tid[y]+1,tid[x],1,1,n);
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&s))
{
memset(head,-1,sizeof(head));
memset(add,0,sizeof(add));
memset(data,0,sizeof(data));
cnt=1;
int u,v,w;
for(int i=1; i<n; i++)
{
scanf("%d%d%d",&u,&v,&w);
Add(u,v,w);
Add(v,u,w);
}
init();
build(1,1,n);
for(int i=1; i<=m; i++)
{
int op;
scanf("%d",&op);
if(op==0)
{
scanf("%d",&u);
ans=0;
Query(s,u);
s=u;
printf("%d\n",ans);
}
else
{
scanf("%d%d",&u,&v);
int x=a[u*2-1].u,y=a[u*2-1].v;
if(deep[x]<deep[y]) swap(x,y);
updata(tid[x],v,1,1,n);
}
}
}
return 0;
}