POJ 2763 Housewife Wind 两种解法
来源:互联网 发布:家庭网络用哪个好 编辑:程序博客网 时间:2024/05/16 07:15
Housewife Wind
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 9637 Accepted: 2642
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
【题意】给一个数,边之间有权值,然后两种操作,第一种:求任意两点的权值和,第二,修改树上两点的权值。
【解题方法1】树剖。
【AC 代码1】
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn=100010;const int inf =1e9;struct edge{ int to,next;}E[maxn*3];int head[maxn],tot,tim;int e[maxn][3];int num[maxn];int siz[maxn],top[maxn],son[maxn];int dep[maxn],Rank[maxn],tid[maxn],fa[maxn];void init(){ tot=tim=0; memset(head,-1,sizeof(head)); memset(son,-1,sizeof(son));}void addedge(int u,int v){ E[tot].to=v,E[tot].next=head[u],head[u]=tot++; E[tot].to=u,E[tot].next=head[v],head[v]=tot++;}void dfs1(int u,int father,int d){ dep[u]=d; fa[u]=father; siz[u]=1; for(int i=head[u]; ~i; i=E[i].next){ int v=E[i].to; if(v!=father){ dfs1(v,u,d+1); if(son[u]==-1||siz[v]>siz[son[u]]) son[u]=v; } }}void dfs2(int u,int tp){ top[u]=tp; tid[u]=++tim; Rank[tid[u]]=u; if(son[u]==-1) return ; dfs2(son[u],tp); for(int i=head[u]; ~i; i=E[i].next){ int v=E[i].to; if(v!=fa[u]&&v!=son[u]){ dfs2(v,v); } }}struct node{ int l,r,sum;}Tree[maxn<<2];void pushup(int rt){ Tree[rt].sum=Tree[rt*2].sum+Tree[rt*2+1].sum;}void Build(int l,int r,int rt){ Tree[rt].l=l,Tree[rt].r=r; if(l==r){ Tree[rt].sum=num[l]; return ; } int m=(l+r)/2; Build(l,m,rt*2); Build(m+1,r,rt*2+1); pushup(rt);}void update(int pos,int val,int rt){ if(Tree[rt].l==pos&&Tree[rt].r==pos){ Tree[rt].sum=val; return ; } int mid=(Tree[rt].l+Tree[rt].r)/2; if(pos<=mid) update(pos,val,rt*2); else update(pos,val,rt*2+1); pushup(rt);}int queryans(int L,int R,int rt){ if(L==Tree[rt].l&&Tree[rt].r==R){ return Tree[rt].sum; } int mid=(Tree[rt].l+Tree[rt].r)/2; if(R<=mid) return queryans(L,R,rt*2); else if(L>mid) return queryans(L,R,rt*2+1); else{ return queryans(L,mid,rt*2)+queryans(mid+1,R,rt*2+1); }}int querysum(int x,int y){ int ans=0; while(top[x]!=top[y]){ if(dep[top[x]]<dep[top[y]]) swap(x,y); ans+=queryans(tid[top[x]],tid[x],1); x=fa[top[x]]; } if(dep[x]>dep[y]) swap(x,y); if(x!=y) ans+=queryans(tid[x]+1,tid[y],1); return ans;}int main(){ int n,q,s; while(~scanf("%d%d%d",&n,&q,&s)){ init(); for(int i=1; i<n; i++){ scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]); addedge(e[i][0],e[i][1]); }// dfs1(1,0,0);// dfs2(1,1);// Build(0,tim,1);// for(int i=0; i<n-1; i++){// if(dep[e[i][0]]>dep[e[i][1]]) swap(e[i][0],e[i][1]);//// } dfs1(1,0,1); dfs2(1,1); for(int i=1; i<n; i++) { if(dep[e[i][0]]<dep[e[i][1]]) swap(e[i][0],e[i][1]); num[tid[e[i][0]]]=e[i][2]; } Build(1,tim,1);// puts("success"); int op,x,y; for(int i=0; i<q; i++) { scanf("%d",&op); if(op==0){ scanf("%d",&x); printf("%d\n",querysum(s,x)); s=x; } else{ scanf("%d%d",&x,&y); update(tid[e[x][0]],y,1); } } }}
【解题方法2】LCA+BIT,具体可以参见《挑战程序设计》P332。
【AC 代码】
////Created By just_sort 2016/9/3//All rights reserved//POJ.2763 Housewife Wind//#include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 100005;const int maxm = 200005;int n,q,s;int vs[maxm],es[maxm],dep[maxm],first[maxn],dfsnum,c[maxn];int getmin(int x,int y){ return dep[x]<dep[y]?x:y;}struct SparseTable{ int dp[20][maxm]; void Init(int n){ for(int i=1; i<=n; i++) dp[0][i]=i; for(int i=1; (1<<i)<=n; i++) for(int j=1; j+(1<<i)-1<=n; j++) dp[i][j]=getmin(dp[i-1][j],dp[i-1][j+(1<<(i-1))]); } int RMQ(int l,int r) { int k=31-__builtin_clz(r-l+1); return getmin(dp[k][l],dp[k][r-(1<<k)+1]); }}st;struct BIT{ int b[maxm],n; void init(int _n){ n=_n;memset(b,0,sizeof(b)); } void update(int i,int v){ while(i<=n){ b[i]+=v; i+=i&-i; } } int getans(int i){ int ret=0; while(i){ ret+=b[i]; i-=i&-i; } return ret; }}bit;int head[maxn],tot;struct edge{ int id,v,cost; edge(){} edge(int id,int v,int cost):id(id),v(v),cost(cost){}};vector<edge>G[maxn];void dfs(int u,int f,int d){ vs[++dfsnum]=u; dep[dfsnum]=d; first[u]=dfsnum; for(int i=0; i<(int)G[u].size(); i++){ edge e=G[u][i]; if(e.v==f) continue; es[2*e.id-1]=dfsnum; bit.update(dfsnum,e.cost); dfs(e.v,u,d+1); vs[++dfsnum]=u; dep[dfsnum]=d; es[2*e.id]=dfsnum; bit.update(dfsnum,-e.cost); }}void init(){ dfsnum=0; bit.init(2*(n-1)); dfs(1,-1,0); st.Init(2*n-1);}int lca(int u,int v){ if(first[u]>first[v]) swap(u,v); int idx=st.RMQ(first[u],first[v]); return vs[idx];}int main(){ while(scanf("%d%d%d",&n,&q,&s)!=EOF) { int i,u,w,v; for(int i=1; i<=n; i++) G[i].clear(); for(int i=1; i<n; i++) { scanf("%d%d%d",&u,&v,&c[i]); G[u].push_back(edge(i,v,c[i])); G[v].push_back(edge(i,u,c[i])); } init(); while(q--) { int op; scanf("%d",&op); if(op){ scanf("%d%d",&i,&w); bit.update(es[2*i-1],w-c[i]); bit.update(es[2*i],c[i]-w); c[i]=w; } else{ scanf("%d",&v); int _lca=lca(s,v); printf("%d\n",bit.getans(first[s]-1)+bit.getans(first[v]-1)-2*bit.getans(first[_lca]-1)); s=v; } } } return 0;}
0 0
- POJ 2763 Housewife Wind 两种解法
- POJ 2763 Housewife Wind
- POJ 2763 Housewife Wind
- poj 2763 Housewife Wind
- POJ 2763 Housewife Wind
- POJ 2763 Housewife Wind
- poj 2763 Housewife Wind
- POJ 2763 Housewife Wind
- POJ 2763 Housewife Wind
- POJ 2763 Housewife Wind
- poj 2763 Housewife Wind
- poj 2763 Housewife Wind(树链剖分)
- POJ 2763 Housewife Wind 树链剖分
- POJ 2763 Housewife Wind 树链剖分
- poj 2763 Housewife Wind(树链剖分)
- poj 2763 Housewife Wind 树链剖分
- POJ 2763 Housewife Wind (树链剖分)
- Housewife Wind - POJ 2763 树链刨分
- Swift2.0学习三
- 信号量 SEM_UNDO标识
- 1341 与3和5无关的数
- JavaApplication和JavaApplet的区别
- CSUOJ 1808 地铁(最短路)
- POJ 2763 Housewife Wind 两种解法
- MYSQL 优化常用方法
- 大型网站架构演变史
- 树模型和线性回归 在回归问题中的比较
- [http] 缓存
- WebSocket实战
- 如何快速的开发一个完整的iOS直播app(播放篇)
- 设计模式之禅——中介者模式
- 209. Minimum Size Subarray Sum