POJ - 2763 Housewife Wind 【树链剖分+边权】
作者:互联网
After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique.
Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: ‘Mummy, take me home!’
At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.
Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?
Input
The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001.
The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.
The following q lines each is one of the following two types:
Message A: 0 u
A kid in hut u calls Wind. She should go to hut u from her current position.
Message B: 1 i w
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid.
Output
For each message A, print an integer X, the time required to take the next child.
Sample Input
3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3
Sample Output
1
3
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 200010
#define INF 0x3f3f3f3f
struct Edg{
int u,v,w;
}edg[maxn<<2];
struct Edge{
int to,w,nxt;
}edge[maxn<<2];
struct Tree{
int l,r,sum;
}tree[maxn<<2];
int n,m,s,siz[maxn],a[maxn],d[maxn],head[maxn],pre[maxn],rk[maxn],id[maxn],top[maxn],son[maxn];
int tot,cnt;
void add(int u,int v,int w){
edge[++tot].to=v; edge[tot].w=w; edge[tot].nxt=head[u]; head[u]=tot;
edge[++tot].to=u; edge[tot].w=w; edge[tot].nxt=head[v]; head[v]=tot;
}
void dfs1(int x,int fa,int depth,int val){
pre[x]=fa; d[x]=depth; siz[x]=1; a[x]=val;
for(int i=head[x];~i;i=edge[i].nxt){
int to=edge[i].to;
if(to==fa) continue;
dfs1(to,x,depth+1,edge[i].w);
siz[x]+=siz[to];
if(siz[to]>siz[son[x]]) son[x]=to;
}
}
void dfs2(int x,int fp){
top[x]=fp; id[x]=++cnt; rk[cnt]=x;
if(son[x]) dfs2(son[x],fp);
for(int i=head[x];~i;i=edge[i].nxt){
int to=edge[i].to;
if(to!=pre[x]&&to!=son[x]) dfs2(to,to);
}
}
void pushup(int k){
tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
}
void build(int l,int r,int k){
tree[k].l=l; tree[k].r=r;
if(l==r){
tree[k].sum=a[rk[l]];
return;
}
int mid=(l+r)>>1;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
pushup(k);
}
void update(int pos,int val,int k){
if(tree[k].l==pos && tree[k].r==pos){
tree[k].sum=val;
return;
}
int mid=(tree[k].l+tree[k].r)>>1;
if(pos<=mid) update(pos,val,k<<1);
else update(pos,val,k<<1|1);
pushup(k);
}
int query(int l,int r,int k){
if(tree[k].l>r||tree[k].r<l) return 0;
if(l<=tree[k].l&&tree[k].r<=r) return tree[k].sum;
int mid=(tree[k].l+tree[k].r)>>1,res=0;
if(l<=mid) res+=query(l,r,k<<1);
if(r> mid) res+=query(l,r,k<<1|1);
return res;
}
int sum(int x,int y){
int res=0;
while(top[x]!=top[y]){
if(d[top[x]]<d[top[y]]) swap(x,y);
res+=query(id[top[x]],id[x],1);
x=pre[top[x]];
}
if(x==y) return res; //树剖+边权的题一定要加上 卡了两小时QAQ
if(id[x]>id[y]) swap(x,y);
return res+query(id[son[x]],id[y],1);
}
int main(){
while(~scanf("%d%d%d",&n,&m,&s)){
tot=0; cnt=0;
memset(head,-1,sizeof(head));
memset(siz,0,sizeof(siz));
memset(son,0,sizeof(son));
for(int i=1;i<n;i++){
scanf("%d%d%d",&edg[i].u,&edg[i].v,&edg[i].w);
add(edg[i].u,edg[i].v,edg[i].w);
}
dfs1(1,0,1,0); dfs2(1,1);
build(1,n,1);
for(int i=1;i<=m;i++){
int op; scanf("%d",&op);
if(op==1){
int a,b; scanf("%d%d",&a,&b);
if(d[edg[a].u]>d[edg[a].v]) update(id[edg[a].u],b,1);
else update(id[edg[a].v],b,1);
}
else{
int t; scanf("%d",&t);
printf("%d\n",sum(s,t));
s=t;
}
}
}
return 0;
}
标签:剖分,2763,边权,son,int,road,time,id,Wind 来源: https://blog.csdn.net/zt2650693774/article/details/98446342