【题目大意】
有一棵树,每条边有边权。你最开始位于点s。有q次操作。0 u:你从s走到u,输出这条简单路径的边权和,并令 s = u。1 i w,把输入的第i条边的边权变为w。
【思路】
没有多想,直接用剖分水过。。。
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
//typedef __int64 LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const int INF=1000111222;
const double INFF=1e200;
const double eps=1e-8;
const int mod=1000000007;
const int NN=100010;
const int MM=100010;
/* ****************** */
struct G
{
int v,next,w,id;
}E[NN*2];
int p[NN],T;
int si[NN],per[NN],deep[NN],anc[NN];
int tsp,pos[NN],top[NN];
int val[NN],a[NN];
struct TR
{
int l,r,sum;
int mid()
{
return (l+r)>>1;
}
}tr[NN*4];
void add(int u,int v,int w)
{
E[T].v=v;
E[T].w=w;
E[T].next=p[u];
p[u]=T++;
}
void c_dfs1(int u,int fa,int cen)
{
anc[u] = fa;
deep[u] = cen;
si[u] = 1;
per[u] = -1;
int i,v;
for(i=p[u];i+1;i=E[i].next)
{
v=E[i].v;
if(v==fa)continue;
val[v] = E[i].w;
E[i].id = E[i^1].id= v;
c_dfs1(v,u,cen+1);
si[u]+=si[v];
if(per[u]==-1 || si[v]>si[ per[u] ])
per[u]=v;
}
}
void c_dfs2(int u,int fa,int now_top)
{
pos[u] = ++tsp;
top[u] = now_top;
a[tsp] = val[u];
if(per[u]!=-1)c_dfs2(per[u],u,now_top);
int i,v;
for(i=p[u];i+1;i=E[i].next)
{
v = E[i].v;
if(v==fa || v==per[u])continue;
c_dfs2(v,u,v);
}
}
void push_up(int R)
{
tr[R].sum = tr[R<<1].sum + tr[R<<1|1].sum;
}
void build(int l,int r,int R)
{
tr[R].l=l;
tr[R].r=r;
if(l==r)
{
tr[R].sum = a[l];
return;
}
int mid=tr[R].mid();
build(l,mid,R<<1);
build(mid+1,r,R<<1|1);
push_up(R);
}
void update(int x,int R,int col)
{
if(tr[R].l==tr[R].r)
{
tr[R].sum = col;
return;
}
int mid=tr[R].mid();
if(x<=mid)
update(x,R<<1,col);
else
update(x,R<<1|1,col);
push_up(R);
}
int query(int l,int r,int R)
{
if(l<=tr[R].l && tr[R].r<=r)
return tr[R].sum;
int mid=tr[R].mid();
int sum=0;
if(l<=mid)
sum += query(l,r,R<<1);
if(r>=mid+1)
sum += query(l,r,R<<1|1);
return sum;
}
void solve(int u,int v)
{
int ans = 0;
while(top[u] != top[v])
{
if( deep[ top[u] ] > deep[ top[v] ] )
{
ans += query(pos[ top[u] ],pos[u],1);
u = anc[ top[u] ];
}
else
{
ans += query(pos[ top[v] ],pos[v],1);
v = anc[ top[v] ];
}
}
if(pos[u]>pos[v])
swap(u,v);
ans += query(pos[u],pos[v],1);
ans -= val[u];
printf("%d\n",ans);
}
int main()
{
int n,m,s,u,v,w,i,op,id;
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
memset(p,-1,sizeof(p));
T=0;
for(i=1;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
val[1] = 0;
c_dfs1(1,-1,0);
tsp = 0;
c_dfs2(1,-1,1);
build(1,n,1);
u = s;
for(i=0;i<m;i++)
{
scanf("%d",&op);
if(op==0)
{
scanf("%d",&v);
solve(u,v);
u = v;
}
else
{
scanf("%d%d",&id,&w);
update( pos[ E[(id-1)*2].id ] , 1, w );
val[ E[(id-1)*2].id ] = w;
}
}
}
return 0;
}