# fzu2082 过路费

http://www.elijahqi.win/archives/815
Problem Description

Input

Output

Sample Input
2 3
1 2 1
1 1 2
0 1 2
1 2 1
Sample Output
1
2
Source
FOJ有奖月赛-2012年4月（校赛热身赛）

#include<cstdio>
#include<cstring>
#define N  55000
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch<='9'&&ch>='0') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct node{
int y,next,z;
}data[N<<1];
struct node1{
int left,right,l,r;long long sum;
}tree[N<<2];
inline void swap(int &x,int &y){
x^=y;y^=x;x^=y;
}
int size[N],a[N],h[N],son[N],w[N],id[N],num,dep[N],fa[N],tp[N],n,son1[N],root,m;
void dfs1(int x){
size[x]=1;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if (fa[x]==y) continue;a[y]=z;son1[i>>1]=y;
dep[y]=dep[x]+1;fa[y]=x;dfs1(y),size[x]+=size[y];
if (size[y]>size[son[x]]) son[x]=y;
}
}
void dfs2(int x,int top){
id[x]=++num;tp[x]=top;w[num]=a[x];
if (son[x]) dfs2(son[x],top);
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (fa[x]==y||son[x]==y) continue;
dfs2(y,y);
}
}
inline void update(int x){
int l=tree[x].left,r=tree[x].right;
tree[x].sum=tree[l].sum+tree[r].sum;
}
void build(int &x,int l,int r){
x=++num;tree[x].l=l;tree[x].r=r;
if (l==r) {tree[x].left=tree[x].right=0;tree[x].sum=w[l];return;}
int mid=l+r>>1;
build(tree[x].left,l,mid);build(tree[x].right,mid+1,r);
update(x);
}
void change(int x,int l,int v){
if (!x) return;
if (tree[x].l==tree[x].r){tree[x].sum=v;return;}
int mid=(tree[x].l+tree[x].r)>>1;
if (l<=mid) change(tree[x].left,l,v);
if (l>mid) change(tree[x].right,l,v);
update(x);
}
long long query(int x,int l,int r){
if (!x) return 0;
if (l<=tree[x].l&&r>=tree[x].r) return tree[x].sum;
int mid=(tree[x].l+tree[x].r)>>1;long long tmp1=0;
if (l<=mid) tmp1+=query(tree[x].left,l,r);
if (r>mid) tmp1+=query(tree[x].right,l,r);
return tmp1;
}
int main(){
freopen("fzu2082.in","r",stdin);
while (~scanf("%d%d",&n,&m)){
num=1;memset(h,0,sizeof(h));memset(son,0,sizeof(son));
for (int i=1;i<n;++i){
data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].z=z;data[num].next=h[y];h[y]=num;
}dep[1]=1;num=0;
dfs1(1);dfs2(1,1);num=0;
build(root,1,n);
for (int i=1;i<=m;++i){
if (op==0){
change(root,id[son1[x]],y);
}else{
long long ans=0;
while(tp[x]!=tp[y]){
if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
ans+=query(root,id[tp[x]],id[x]);
x=fa[tp[x]];
}
if (id[x]>id[y]) swap(x,y);if (id[x]==id[y]) {printf("%lld\n",ans);continue;}
ans+=query(root,id[x]+1,id[y]);
printf("%lld\n",ans);
}
}
}
return 0;
}