码风优良,WA on#2 141,#3 191,#9 1382
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read() {
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const ll N=1e5+10;
ll n,_;
vector<pair<ll,ll> > e[N];
struct Edge{
ll u,v,w;
}edge[N];
ll op,x,y,cnt,ring_pos;
ll top[N],sz[N],fa[N],deep[N],dfn[N],ww[N],last_w[N],son[N];
struct Segmentree{
ll l,r,sum;
}t[N*4];
void build(ll q,ll l,ll r){
t[q].l=l;
t[q].r=r;
if(l==r){
t[q].sum=last_w[l];
return ;
}
ll mid=(l+r)/2;
build(q*2,l,mid);
build(q*2+1,mid+1,r);
t[q].sum=t[q*2].sum+t[q*2+1].sum;
}
void change(ll q,ll seat,ll d){
if(t[q].l==t[q].r){
t[q].sum=d;
return ;
}
ll mid=(t[q].l+t[q].r)/2;
if(seat<=mid) change(q*2,seat,d);
else change(q*2+1,seat,d);
t[q].sum=t[q*2].sum+t[q*2+1].sum;
}
ll ask(ll q,ll l,ll r){
if(t[q].l>=l&&t[q].r<=r) return t[q].sum;
ll sum=0,mid=(t[q].l+t[q].r)/2;
if(l<=mid) sum+=ask(q*2,l,r);
if(r>mid) sum+=ask(q*2+1,l,r);
return sum;
}
ll Ask_LCA(ll u,ll v){
ll sum=0;
while(top[u]!=top[v]){
if(deep[top[u]]<deep[top[v]]) swap(u,v);
sum+=ask(1,dfn[top[u]],dfn[u]);
u=fa[top[u]];
}
if(u==v) return sum;
if(deep[u]>deep[v]) swap(u,v);
sum+=ask(1,dfn[u]+1,dfn[v]);
return sum;
}
ll find(ll t){
return fa[t]==t?t:fa[t]=find(fa[t]);
}
void dfs1(ll t,ll last){
sz[t]=1;
fa[t]=last;
deep[t]=deep[last]+1;
for (int i=0;i<e[t].size();i++){
if(e[t][i].first==last) continue;
dfs1(e[t][i].first,t);
sz[t]+=sz[e[t][i].first];
ww[e[t][i].first]=e[t][i].second;
if(sz[son[t]]<sz[e[t][i].first]) son[t]=e[t][i].first;
}
}
void dfs2(ll tp,ll t){
top[t]=tp;
dfn[t]=cnt;
last_w[cnt]=ww[t];
cnt++;
if(son[t]==0) return ;
dfs2(tp,son[t]);
for (int i=0;i<e[t].size();i++){
if(e[t][i].first==fa[t]||e[t][i].first==son[t]) continue;
dfs2(e[t][i].first,e[t][i].first);
}
}
signed main(){
n=read();
_=read();
for (int i=1;i<=n;i++) fa[i]=i;
for (int i=1;i<=n;i++){
edge[i].u=read();
edge[i].v=read();
edge[i].w=read();
ll uu=find(edge[i].u);
ll vv=find(edge[i].v);
if(uu==vv) ring_pos=i;
else {
fa[uu]=vv;
e[edge[i].u].push_back(make_pair(edge[i].v,edge[i].w));
e[edge[i].v].push_back(make_pair(edge[i].u,edge[i].w));
}
}
cnt=0;
dfs1(1,0);
dfs2(1,1);
cnt--;
build(1,1,cnt);
while(_--){
op=read();
x=read();
y=read();
if(op==1){
edge[x].w=y;
ll tmp;
if(deep[edge[x].u]>deep[edge[x].v]) tmp=edge[x].u;
else tmp=edge[x].v;
change(1,dfn[tmp],y);
} else if(op==2){
printf("%lld\n",min(Ask_LCA(x,y),min(Ask_LCA(x,edge[ring_pos].u)+Ask_LCA(y,edge[ring_pos].v),Ask_LCA(x,edge[ring_pos].v)+Ask_LCA(y,edge[ring_pos].u))+edge[ring_pos].w));
}
}
}