Housewife Wind(边权树链剖分)
Housewife Wind
http://poj.org/problem?id=2763
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 14820 | Accepted: 4097 |
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 1 1 2 1 2 3 2 0 2 1 2 3 0 3
Sample Output
1 3
Source
POJ Monthly--2006.02.26,zgl & twb
边权树链剖分模板题
比较一条边上哪个点的深度大,就把权值加在哪个点上
1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<cstdio> 6 #include<algorithm> 7 #include<vector> 8 #define maxn 200005 9 #define MAXN 200005 10 #define lson l,mid,rt<<1 11 #define rson mid+1,r,rt<<1|1 12 using namespace std; 13 14 long long tree[maxn<<3]; 15 int n; 16 int v[maxn],val[maxn]; 17 int dep[maxn],fa[maxn],siz[maxn],son[maxn],id[maxn],top[maxn],cnt; 18 int co,head[MAXN]; 19 struct Edge { 20 int to, next; 21 }edge[MAXN]; 22 struct E { 23 int u, v, c; 24 }e[MAXN]; 25 void addedge(int u, int v) { 26 edge[cnt].to = v; 27 edge[cnt].next = head[u]; 28 head[u] = cnt++; 29 } 30 struct sair{ 31 int x,y,len; 32 }p[maxn]; 33 34 void pushup(int rt){ 35 tree[rt]=(tree[rt<<1]+tree[rt<<1|1]); 36 } 37 38 void build(int l,int r,int rt){ 39 if(l==r){ 40 tree[rt]=0; 41 return; 42 } 43 int mid=(l+r)/2; 44 build(lson); 45 build(rson); 46 pushup(rt); 47 } 48 49 void add(int L,int R,int k,int l,int r,int rt){ 50 if(L<=l&&R>=r){ 51 tree[rt]=k; 52 return; 53 } 54 int mid=(l+r)/2; 55 if(L<=mid) add(L,R,k,lson); 56 if(R>mid) add(L,R,k,rson); 57 pushup(rt); 58 } 59 60 long long query(int L,int R,int l,int r,int rt){ 61 if(L<=l&&R>=r){ 62 return tree[rt]; 63 } 64 int mid=(l+r)/2; 65 long long ans=0; 66 if(L<=mid) ans+=query(L,R,lson); 67 if(R>mid) ans+=query(L,R,rson); 68 pushup(rt); 69 return ans; 70 } 71 72 void dfs1(int now,int f,int deep){ 73 dep[now]=deep; 74 siz[now]=1; 75 fa[now]=f; 76 int maxson=-1; 77 for(int i=head[now];~i;i=edge[i].next){ 78 if(edge[i].to != fa[now]) { 79 dfs1(edge[i].to,now,deep+1); 80 siz[now]+=siz[edge[i].to]; 81 if(siz[edge[i].to]>maxson){ 82 maxson=siz[edge[i].to]; 83 son[now]=edge[i].to; 84 } 85 } 86 } 87 } 88 89 void dfs2(int now,int topp){ 90 id[now]=++cnt; 91 val[cnt]=v[now]; 92 top[now]=topp; 93 if(!son[now]) return; 94 dfs2(son[now],topp); 95 for(int i=head[now];~i;i=edge[i].next){ 96 int vvv = edge[i].to; 97 if(vvv==son[now]||vvv==fa[now]) continue; 98 dfs2(vvv,vvv); 99 } 100 } 101 102 long long qRange(int x,int y){ 103 int t1 = top[x], t2 = top[y]; 104 long long res = 0; 105 while(t1 != t2) { 106 if(dep[t1] < dep[t2]) { 107 swap(t1, t2); swap(x, y); 108 } 109 res += query(id[t1], id[x], 1, n, 1); 110 x = fa[t1]; t1 = top[x]; 111 } 112 if(x == y) return res; 113 if(dep[x] > dep[y]) swap(x, y); 114 return res + query(id[son[x]], id[y], 1, n, 1); 115 } 116 117 void addRange(int x,int y,int k){ 118 while(top[x]!=top[y]){ 119 if(dep[top[x]]<dep[top[y]]) swap(x,y); 120 add(id[top[x]],id[x],k,1,n,1); 121 x=fa[top[x]]; 122 } 123 if(dep[x]>dep[y]) swap(x,y); 124 add(id[x],id[y],k,1,n,1); 125 } 126 127 int main(){ 128 int m,r; 129 scanf("%d %d %d",&n,&m,&r); 130 memset(head, -1, sizeof head); 131 int pos,z,x,y; 132 co=0; 133 for(int i=1;i<n;i++){ 134 scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].len); 135 addedge(p[i].x,p[i].y); 136 addedge(p[i].y,p[i].x); 137 } 138 cnt=0; 139 int xx; 140 dfs1(1,0,1); 141 dfs2(1,1); 142 build(1,n,1); 143 for(int i=1;i<n;i++){ 144 if(dep[p[i].x]<dep[p[i].y]) xx=p[i].y; 145 else xx=p[i].x; 146 addRange(xx,xx,p[i].len); 147 } 148 for(int i=1;i<=m;i++){ 149 scanf("%d %d",&pos,&x); 150 if(!pos){ 151 printf("%lld\n",qRange(x,r)); 152 r=x; 153 } 154 else if(pos){ 155 scanf("%d",&y); 156 p[x].len=y; 157 if(dep[p[x].x]<dep[p[x].y]) xx=p[x].y; 158 else xx=p[x].x; 159 addRange(xx,xx,p[x].len); 160 } 161 } 162 163 }
posted on 2018-09-23 22:33 Fighting_sh 阅读(528) 评论(0) 编辑 收藏 举报