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.
ince 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.
ind 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
思路
树链剖分,线段树单点修改,区间查询。
可以将边权问题转换成结点问题:线段树维护当前结点到其父结点边的边权(所耗时间)。
代码
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int maxn=100005;
struct haha{
int father,size,deep,top,faw;
}root[maxn];
struct x{
int v,w;
};
vector <struct x> bian[maxn];
int sum[maxn<<2];//线段树
int flag,n,m,q,num1[maxn],num2[maxn],son[maxn],markb[maxn][3],ans;
void dfs1(int u,int f,int d)//处理结点高度,父结点,子树大小,重儿子
{
root[u].deep=d;
root[u].father=f;
root[u].size=1;
int i,len=bian[u].size();
struct x v;
for(i=0;i<len;i++)
{
v=bian[u][i];
if(v.v!=f)
{
dfs1(v.v,u,d+1);
root[u].size+=root[v.v].size;
if(son[u]==-1||root[v.v].size>root[son[u]].size)
son[u]=v.v;
}
else root[u].faw=v.w;
}
}
void dfs2(int u,int t)//分出轻重链,所在链顶端
{
root[u].top=t;
num1[u]=flag++;
num2[num1[u]]=u;
if(son[u]==-1)return;
dfs2(son[u],t);
int i,len=bian[u].size();
struct x v;
for(i=0;i<len;i++)
{
v=bian[u][i];
if(v.v!=root[u].father&&v.v!=son[u])dfs2(v.v,v.v);
}
}
void build(int l,int r,int k)//建立线段树
{
if(l==r)
{
sum[k]=root[num2[l]].faw;
return;
}
int mid=(l+r)/2;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
sum[k]=sum[k<<1]+sum[k<<1|1];
}
void change(int l,int r,int k,int u,int value)//单点修改
{
if(l==r)
{
sum[k]=value;return;
}
int mid=(l+r)/2;
if(u<=mid)change(l,mid,k<<1,u,value);
else change(mid+1,r,k<<1|1,u,value);
sum[k]=sum[k<<1]+sum[k<<1|1];
}
void add(int L,int R,int l,int r,int k)//区间查询
{
if(L<=l&&R>=r)
{
ans+=sum[k];
return;
}
int mid=(l+r)/2;
if(L<=mid)add(L,R,l,mid,k<<1);
if(R>mid)add(L,R,mid+1,r,k<<1|1);
}
void que(int u,int v)//处理询问
{
int a=root[u].top,b=root[v].top;
while(a!=b)
{
if(root[a].deep<root[b].deep){swap(a,b);swap(u,v);}
add(num1[a],num1[u],1,n,1);
u=root[a].father;a=root[u].top;
}
if(root[u].deep<root[v].deep)swap(u,v);
add(num1[v],num1[u],1,n,1);
ans-=root[v].faw;//减去不必要的
}
int main( )
{
int i,u,v,j,st,value,s;
struct x k;
scanf("%d %d %d",&n,&q,&st);
memset(son,-1,sizeof(son));
memset(sum,0,sizeof(sum));
flag=1;
for (i=1;i<=n;i++)bian[i].clear();
for(i=1;i<n;i++)
{
scanf("%d %d %d",&u,&v,&value);
k.v=v,k.w=value;bian[u].push_back(k);
k.v=u;bian[v].push_back(k);
markb[i][0]=u,markb[i][1]=v;
}
dfs1(1,0,0);
dfs2(1,1);
build(1,n,1);
for(i=0;i<q;i++)
{
scanf("%d",&s);
if(s==0)
{
scanf("%d",&u);
ans=0;
que(st,u);
st=u;
printf("%d\n",ans);
}
else
{
scanf("%d %d",&u,&value);//注意修改是边权,先处理成修改结点
v=markb[u][1];u=markb[u][0];
if(root[u].deep<root[v].deep)u=v;
root[u].faw=value;
change(1,n,1,num1[u],value);
}
}
return 0;
}