poj 2763 树链剖分(单点更新,区间求值)
来源:互联网 发布:iphoneairplay连接mac 编辑:程序博客网 时间:2024/05/15 23:47
http://poj.org/problem?id=2763
Description
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?
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.
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 11 2 12 3 20 21 2 30 3
Sample Output
13
/**poj 2763 树链剖分(单点更新,区间求值)题目大意:给定一个棵树,对于每条边的权值都在动态的变化,请求出在动态变化过程中,指定两个点的距离解题思路:树链剖分,单点跟新,区间维护和*/#include <stdio.h>#include <algorithm>#include <iostream>#include <string.h>using namespace std;const int maxn=100005;typedef long long LL;int fa[maxn],dep[maxn],siz[maxn],son[maxn],num[maxn];int top[maxn],Hash[maxn],d[maxn][3];int n,s,z,q,tree[maxn*4];int head[maxn],ip;void init(){ memset(head,-1,sizeof(head)); memset(tree,0,sizeof(tree)); ip=0;}struct note{ int v,w,next;}edge[maxn*4];void addedge(int u,int v,int w){ edge[ip].v=v,edge[ip].w=w,edge[ip].next=head[u],head[u]=ip++;}void dfs(int u,int pre){ son[u]=0,siz[u]=1; dep[u]=dep[pre]+1; fa[u]=pre; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==pre)continue; dfs(v,u); siz[u]+=siz[v]; if(siz[son[u]]<siz[v]) { son[u]=v; } } /// printf("%d dep fa siz son %d %d %d %d\n",u,dep[u],fa[u],siz[u],son[u]);}void set_que(int u,int tp){ num[u]=++z,top[u]=tp; Hash[z]=u; if(son[u]) { set_que(son[u],tp); } for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==son[u]||v==fa[u])continue; set_que(v,v); }}void push_up(int root){ tree[root]=tree[root<<1]+tree[root<<1|1];}void update(int root,int l,int r,int loc,int z){ if(l>loc||r<loc) return; if(l==r) { tree[root]=z; return; } int mid=(l+r)>>1; update(root<<1,l,mid,loc,z); update(root<<1|1,mid+1,r,loc,z); push_up(root);}int query(int root,int l,int r,int x,int y){ if(r<x||l>y)return 0; if(x<=l&&r<=y) { return tree[root]; } int mid=(l+r)>>1; return query(root<<1,l,mid,x,y)+query(root<<1|1,mid+1,r,x,y);}int main(){ //freopen("data.txt","r",stdin); while(~scanf("%d%d%d",&n,&q,&s)) { init(); for(int i=1;i<n;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); d[i][0]=u,d[i][1]=v,d[i][2]=w; addedge(u,v,w); addedge(v,u,w); } int root=(n+1)>>1; z=0,dep[0]=0,siz[0]=0; dfs(root,0); set_que(root,root); for(int i=1;i<n;i++) { if(dep[d[i][0]]>dep[d[i][1]]) { swap(d[i][0],d[i][1]); } update(1,1,z,num[d[i][1]],d[i][2]); } int s1; while(q--) { int x,y,zz; scanf("%d",&x); if(x==0) { scanf("%d",&y); s1=s; s=y; int f1=top[s1],f2=top[y],sum=0; while(f1!=f2) { if(dep[f1]<dep[f2]) { swap(f1,f2); swap(s1,y); } sum+=query(1,1,z,num[f1],num[s1]); s1=fa[f1],f1=top[s1]; } if(y!=s1) { if(dep[s1]>dep[y]) { swap(s1,y); } sum+=query(1,1,z,num[son[s1]],num[y]); } printf("%d\n",sum); } else { scanf("%d%d",&y,&zz); update(1,1,z,num[d[y][1]],zz); } } } return 0;}
0 0
- poj 2763 树链剖分(单点更新,区间求值)
- FZU2082 树链剖分(单点更新区间求值)
- hdu3966 树链剖分(区间更新和单点求值)
- light oj 1348 树链剖分(单点更新区间求值)
- poj 2155 Matrix 树状数组 区间更新单点求值
- POJ - 2155 Matrix (二维树状数组 + 区间修改 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)
- hdu1166敌兵布阵(线段树---单点更新,区间求值)
- hdu1754I Hate It(线段树---单点更新,区间求值)
- hdu3074Multiply game(线段树---单点更新,区间求值)
- 1005(三维树状数组+区间更新,单点求值)
- POJ 3237 Tree(树链剖分 + 单点更新 + 区间更新 + 区间查询)
- spoj375 树链剖分(单点更新,区间查询)
- 三维树状数组(区间更新,单点查询)POJ
- 1036: [ZJOI2008]树的统计Count 【树链剖分】【点权:单点更新区间求值】
- HDU 1166 敌兵布阵 (树状数组--单点更新,区间求值)
- HDU1556 Color the ball 树状数组(区间更新单点求值)
- [HDU]1556 Color the ball (树状数组--区间更新,单点求值)
- Color the ball ---树状数组(区间更新及单点求值)
- 几个iOS工程通用模块第三方类库介绍
- #305 (div.2) C. Mike and Frog
- 怎么样恢复别人删除的腾讯微博私信聊天内容
- 怎么做才可以把之前删除的微信聊天记录恢复
- Partition List
- poj 2763 树链剖分(单点更新,区间求值)
- jsp+js实现二级级联
- 怎样才能查看爱人的微信聊天记录QQ聊天记录
- 【移动语义和精准转发系列二】std::move和std::forward
- 如何计算IP地址的网络号和主机号?
- 怎样才能看到老公手机上已删除的QQ聊天记录
- Perl 若A则B判断的表示方法
- 3次握手4次挥手
- CIDR