简单题。

#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 99999999
#define ll __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
struct node{
int to;
int next;
}edge[MAXN*];
int ind,pre[MAXN],top[MAXN],fa[MAXN],siz[MAXN],son[MAXN],w[MAXN],deq[MAXN],fn;
int n;
ll val[MAXN][],tree[MAXN<<];
void add(int x,int y)
{
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind++;
}
void dfs1(int rt,int pa,int d)
{
deq[rt] = d;
siz[rt] = ;
son[rt] = ;
fa[rt] = pa;
for(int i=pre[rt]; i!=-; i=edge[i].next){
int t = edge[i].to;
if(t != fa[rt]){
dfs1(t,rt,d+);
siz[rt] += siz[t];
if(siz[son[rt]] < siz[t]){
son[rt] = t;
}
}
}
}
void dfs2(int rt,int tp)
{
w[rt] = ++fn;
top[rt] = tp;
if(son[rt] != ){
dfs2(son[rt],tp);
}
for(int i=pre[rt]; i!=-; i=edge[i].next){
int t = edge[i].to;
if(t != fa[rt] && t != son[rt]){
dfs2(t,t);
}
}
}
/****************************/
void pushup(int rt)
{
tree[rt] = tree[rt<<] + tree[rt<<|];
}
void updata(int p,ll v,int l,int r,int rt)
{
if(l == r){
tree[rt] = v;
return ;
}
int m = (l+r)/;
if(m >= p){
updata(p,v,lson);
}
else {
updata(p,v,rson);
}
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R){
return tree[rt];
}
int m = (l+r)/;
ll ans = ;
if(m >= L){
ans += query(L,R,lson);
}
if(m < R){
ans += query(L,R,rson);
}
return ans;
}
ll lca(int x,int y)
{
ll ans = ;
while(top[x] != top[y])
{
if(deq[top[x]] < deq[top[y]]){
swap(x,y);
}
ans += query(w[top[x]],w[x],,fn,);
x = fa[top[x]];
}
if(x == y)
return ans;
if(deq[x] < deq[y]){
swap(x,y);
}
ans += query(w[son[y]],w[x],,fn,);
return ans;
}
int main()
{
int i,j,m;
while(~scanf("%d%d",&n,&m))
{
ind = ;
memset(pre,-,sizeof(pre));
for(i=; i<n; i++){
int a,b;
ll c;
scanf("%d %d %I64d",&a,&b,&c);
add(a,b);
add(b,a);
val[i][] = (ll)a;
val[i][] = (ll)b;
val[i][] = c;
}
memset(son,,sizeof(son));
memset(siz,,sizeof(siz));
dfs1(,,);
fn = ;
dfs2(,);
memset(tree,,sizeof(tree)); for(i=; i<n; i++){
if(deq[val[i][]] < deq[val[i][]]){
swap(val[i][],val[i][]);
}
updata(w[val[i][]],val[i][],,fn,);
}
int t,a,b;
while(m--)
{
scanf("%d%d%d",&t,&a,&b);
if(t == ){
updata(w[val[a][]],b,,fn,);
}
else {
printf("%I64d\n",lca(a,b));
}
}
}
}
05-11 08:52