POJ 2763 Housewife Wind
2015-09-28 15:25
351 查看
Housewife Wind
Time Limit: 4000msMemory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2763
64-bit integer IO format: %lld Java class name: Main
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
Source
POJ Monthly--2006.02.26解题:树链剖分
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100010; struct arc{ int to,w,next; arc(int x = 0,int y = 0,int z = -1){ to = x; w = y; next = z; } }e[maxn<<1]; struct node{ int lt,rt,sum; }tree[maxn<<2]; int head[maxn],fa[maxn],top[maxn],dep[maxn]; int siz[maxn],son[maxn],loc[maxn]; int n,q,s,tot,clk; void add(int u,int v,int w){ e[tot] = arc(v,w,head[u]); head[u] = tot++; } void FindHeavyEdge(int u,int father,int depth){ fa[u] = father; son[u] = -1; siz[u] = 1; dep[u] = depth; for(int i = head[u]; ~i; i = e[i].next){ if(e[i].to == father) continue; FindHeavyEdge(e[i].to,u,depth + 1); siz[u] += siz[e[i].to]; if(son[u] == -1 || siz[e[i].to] > siz[son[u]]) son[u] = e[i].to; } } void ConnectHeavyEdge(int u,int ancestor){ top[u] = ancestor; loc[u] = clk++; if(son[u] != -1) ConnectHeavyEdge(son[u],ancestor); for(int i = head[u]; ~i; i = e[i].next){ if(e[i].to == fa[u] || son[u] == e[i].to) continue; ConnectHeavyEdge(e[i].to,e[i].to); } } void build(int lt,int rt,int v){ tree[v].lt = lt; tree[v].rt = rt; tree[v].sum = 0; if(lt == rt) return; int mid = (lt + rt)>>1; build(lt,mid,v<<1); build(mid + 1,rt,v<<1|1); } void update(int pos,int val,int v){ if(tree[v].lt == tree[v].rt){ tree[v].sum = val; return; } if(pos <= tree[v<<1].rt) update(pos,val,v<<1); else update(pos,val,v<<1|1); tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum; } int query(int lt,int rt,int v){ if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].sum; int ret = 0; if(lt <= tree[v<<1].rt) ret = query(lt,rt,v<<1); if(rt >= tree[v<<1|1].lt) ret += query(lt,rt,v<<1|1); return ret; } int QUERY(int u,int v){ if(u == v) return 0; int ret = 0; while(top[u] != top[v]){ if(dep[top[u]] < dep[top[v]]) swap(u,v); ret += query(loc[top[u]],loc[u],1); u = fa[top[u]]; } if(u == v) return ret; if(dep[u] > dep[v]) swap(u,v); ret += query(loc[son[u]],loc[v],1); return ret; } int main(){ int u,v,w,op; while(~scanf("%d%d%d",&n,&q,&s)){ memset(head,-1,sizeof head); tot = clk = 0; for(int i = 1; i < n; ++i){ scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } FindHeavyEdge(1,0,0); ConnectHeavyEdge(1,1); build(0,clk-1,1); for(int i = 0; i < tot; i += 2){ u = e[i].to; v = e[i + 1].to; if(dep[u] < dep[v]) swap(u,v); update(loc[u],e[i].w,1); } while(q--){ scanf("%d",&op); if(op){ scanf("%d%d",&u,&w); int ith = (u-1)*2; if(dep[e[ith].to] < dep[e[ith + 1].to]) ++ith; update(loc[e[ith].to],w,1); }else{ scanf("%d",&u); printf("%d\n",QUERY(s,u)); s = u; } } } return 0; }
View Code
相关文章推荐
- Perfect Squares【279】
- 前端见微知著JavaScript基础篇:this or that ?
- 前端精选文摘:css之BFC 神奇背后的原理(转载)
- jquery 实现点击后1秒展开div
- HTML5拖放本地资源
- HDOJ 5487 Difference of Languages
- JavaScript高级程序设计之面向对象的程序设计之继承之寄生组合式继承第6.3.6讲笔记
- jquery 动态的添加数据,获取动态的数据
- What is the difference between a soft reference and a weak reference in Java?
- JS写的一个日期选择控件(移动网页的),写得不当的地方,望各位大神指正
- Js 实现图片放大功能
- Htmlparser Filter
- JavaScript高级程序设计之面向对象的程序设计之继承之寄生式继承第6.3.5讲笔记
- JavaScript多图片上传案例
- 转载:(个人学习用)JSP文件过大编译失败,异常如is exceeding the 65535 bytes l...
- JSON数组操作
- 百度2014前端笔试题
- 源码版本管理gitcafe(已经迁移到coding)
- [LeetCode]题解(python):019-Remove Nth Node From End of List
- JavaScript高级程序设计之面向对象的程序设计之继承之原型式继承第6.3.4讲笔记