链接:poj2763
题解
- 单点修改区间查询,注意维护边信息…坑了好几次
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using namespace std;
const int N=1e6;
ll rt[N<<2],id[N],hson[N],size[N],deep[N],f[N],top[N];
ll head[N],to[N],nxt[N],w[N],wt[N],e[N][2];
int n,m,s,cnt;
inline void adde(int x,int y,int z,int k){nxt[k]=head[x];to[k]=y;wt[k]=z;head[x]=k;}
inline void pushup(int o){rt[o]=rt[o<<1]+rt[o<<1|1];}
inline void build(int o,int l,int r){
if(l==r){rt[o]=w[l];return ;}
int mid=l+r>>1;
build(lson);
build(rson);
pushup(o);
}
inline void update(int o,int l,int r,int x,int y){
if(l==r){rt[o]=y;return ;}
int mid=l+r>>1;
if(x<=mid)update(lson,x,y);
else update(rson,x,y);
pushup(o);
}
inline ll query(int o,int l,int r,int L,int R){
if(L<=l&&R>=r)return rt[o];
int mid=l+r>>1;ll ans=0;
if(L<=mid)ans+=query(lson,L,R);
if(R>mid)ans+=query(rson,L,R);
return ans;
}
inline void dfs1(int x){
size[x]=1;
for(int i=head[x];i;i=nxt[i])if(to[i]!=f[x]){
f[to[i]]=x;
deep[to[i]]=deep[x]+1;
dfs1(to[i]);
size[x]+=size[to[i]];
if(size[hson[x]]<size[to[i]])hson[x]=to[i];
}
}
inline void dfs2(int x,int t){
top[x]=t;id[x]=++cnt;
if(hson[x])dfs2(hson[x],t);
for(int i=head[x];i;i=nxt[i])if(hson[x]!=to[i]&&f[x]!=to[i])dfs2(to[i],to[i]);
}
inline ll queryrange(int x,int y){
ll ans=0;
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]])swap(x,y);
ans+=query(1,1,n,id[top[x]],id[x]);
x=f[top[x]];
}
if(x==y)return ans;
if(deep[x]>deep[y])swap(x,y);
return ans+query(1,1,n,id[hson[x]],id[y]);
}
int main(){
scanf("%d%d%d",&n,&m,&s);
rep(i,1,n-1){
int u,v,c;
scanf("%d%d%d",&e[i][0],&e[i][1],&c);
adde(e[i][0],e[i][1],c,i);adde(e[i][1],e[i][0],c,i+n);
}
dfs1(1);
dfs2(1,1);
rep(i,1,n-1){if(deep[e[i][0]]<deep[e[i][1]])swap(e[i][0],e[i][1]);w[id[e[i][0]]]=wt[i];}
build(1,1,n);
while(m--){
int a,b;
scanf("%d",&a);
if(!a){scanf("%d",&b);printf("%lld\n",queryrange(s,b));s=b;}
else {scanf("%d%d",&a,&b);update(1,1,n,id[e[a][0]],b);}
}
return 0;
}