POJ 2763 Housewife Wind
来源:互联网 发布:linux如何做ftp客户端 编辑:程序博客网 时间:2024/05/06 06:07
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?
【题目分析】
树链剖分+线段树
【代码】
#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int n,cur,q,root=1,node=0,opt,x,y;int dep[100001],son[100001],size[100001],fa[100001],pos[100001],v[100001],top[100001];int data[100001];int h[200001],to[200001],ne[200001],w[200001],en=0;struct node{ int l,r,sum;}t[100001*8];void add(int a,int b,int c){ ne[en]=h[a]; to[en]=b; w[en]=c; h[a]=en++;}void dfs(int k){ size[k]=1; for (int i=h[k];i>=0;i=ne[i]) { if (to[i]!=fa[k]) { fa[to[i]]=k; dep[to[i]]=dep[k]+1; v[to[i]]=w[i]; dfs(to[i]); size[k]+=size[to[i]]; if (size[to[i]]>size[son[k]]) son[k]=to[i]; } }}void dfs(int k,int tp){ top[k]=tp; pos[k]=++node; data[node]=v[k]; if (son[k]) dfs(son[k],tp); for (int i=h[k];i>=0;i=ne[i]) if (to[i]!=fa[k]&&to[i]!=son[k]) dfs(to[i],to[i]);}void build(int k,int L,int R){ t[k].l=L; t[k].r=R; if (L==R) { t[k].sum=data[L]; return ; } build(k*2,L,(L+R)/2); build(k*2+1,(L+R)/2+1,R); t[k].sum=t[k*2].sum+t[k*2+1].sum;}int qsum(int k,int L,int R){ if (t[k].l>R||t[k].r<L) return 0; if (t[k].l>=L&&t[k].r<=R) return t[k].sum; return qsum(k*2,L,R)+qsum(k*2+1,L,R);}void mod(int k,int x){ if (t[k].l==t[k].r) { t[k].sum=data[t[k].l]; return ; } if (x<=(t[k].l+t[k].r)/2) mod(k*2,x); else mod(k*2+1,x); t[k].sum=t[k*2].sum+t[k*2+1].sum;}int main(){ memset(h,-1,sizeof h); scanf("%d%d%d",&n,&q,&cur); for (int i=1;i<n;++i) { int a,b,c; scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,c); } dfs(root); dfs(root,root); build(1,1,node); while (q--) { scanf("%d",&opt); if (opt==0) { int ans=0; scanf("%d",&x); int a=cur,b=x; cur=x; while (top[a]!=top[b]) { if (dep[top[a]]<dep[top[b]]) swap(a,b); ans+=qsum(1,pos[top[a]],pos[a]); a=fa[top[a]]; } if (dep[a]<dep[b]) swap(a,b); if (a!=b) ans+=qsum(1,pos[b]+1,pos[a]); printf("%d\n",ans); } else { scanf("%d%d",&x,&y); int a=to[(x-1)*2],b=to[(x-1)*2+1];// printf("%d %d\n",a,b); if (dep[a]<dep[b]) swap(a,b); data[pos[a]]=y; mod(1,pos[a]); } }}
- 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 树链刨分
- POJ 2763 Housewife Wind 树链剖分
- 重构-改善既有代码的设计
- NOIP 2015 运输计划
- Qt5---ftp上传功能
- 实战1-糗事百科Spider
- Java数据结构-图
- POJ 2763 Housewife Wind
- Myeclipse10配置Tomcat
- 算法练习11
- jsp传值乱码的解决办法
- http状态码详解
- spring+mabitas事务控制无法回滚综合分析
- HashSet类是如何实现添加元素保证不重复的---哈希码的原理
- 8.javaweb之基于XML配置的Hello World案例
- poj 1080 Human Gene Functions 动态规划(最长公共子序列)