Housewife Wind
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 6898 | Accepted: 1742 |
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?
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
树链剖分的模板题,做好线段树后,就是简单的单点更新,区域查询了
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define maxn 110000 #define lmin 1 #define rmax n #define root lmin,rmax,1 #define lson l,(l+r)/2,rt<<1 #define rson (l+r)/2+1,r,rt<<1|1 #define now l,r,rt #define int_now int l,int r,int rt struct node { int u , v , w ; int next ; } edge[maxn<<1]; int head[maxn] , vis[maxn] , cnt ;//存储树,vis标记节点 int belong[maxn] ;//记录第i条边(u,v)中的子节点v int p[maxn] ;//子节点为v的树枝的长度 int cl[maxn<<2] , s[maxn] ;//线段树数组cl,s记录标号为1到n的树杈的值,提供给cl建立线段树 int num[maxn] ;//以节点u为根的子树的节点个数 int dep[maxn] ;//节点u的深度,根的深度为1 int top[maxn] ;//u所在的重链的最顶端的节点 int son[maxn] , fa[maxn] ;//u的重儿子节点,u的父亲节点, int w[maxn] , step ;//记录节点v与父亲的连边在线段树的位置 int n , m , t ; void add(int u,int v,int w) { edge[cnt].u = u ; edge[cnt].v = v ; edge[cnt].w = w ; edge[cnt].next = head[u] ; head[u] = cnt++ ; edge[cnt].u = v ; edge[cnt].v = u ; edge[cnt].w = w ; edge[cnt].next = head[v] ; head[v] = cnt++ ; return ; } void dfs1(int u) { int i , v , max1 = -1 , k = -1 ; num[u] = 1 ; for(i = head[u] ; i != -1 ; i = edge[i].next) { v = edge[i].v ; if( vis[v] ) continue ; belong[i/2+1] = v ; vis[v] = 1 ; dep[v] = dep[u] + 1 ; fa[v] = u ; p[v] = edge[i].w ; dfs1(v) ; if( num[v] > max1 ) { max1 = num[v] ; k = v ; } } son[u] = k ; return ; } void dfs2(int u) { if( son[u] == -1 ) return ; int i , v = son[u] ; vis[v] = 1 ; w[v] = step ; s[step++] = p[v] ; top[v] = top[u] ; dfs2(v) ; for(i = head[u] ; i != -1 ; i = edge[i].next) { v = edge[i].v ; if( vis[v] ) continue ; vis[v] = 1 ; w[v] = step ; s[step++] = p[v] ; top[v] = v ; dfs2(v) ; } return ; } void dfs() { memset(vis,0,sizeof(vis)) ; vis[1] = 1 ; dep[1] = 1 ; fa[1] = -1 ; dfs1(1) ; memset(vis,0,sizeof(vis)) ; vis[1] = 1 ; step = 1 ; top[1] = 1 ; dfs2(1) ; return ; } void push_up(int_now) { cl[rt] = cl[rt<<1] + cl[rt<<1|1] ; return ; } void build(int_now) { cl[rt] = 0 ; if( l != r ) { build(lson) ; build(rson) ; push_up(now) ; } else cl[rt] = s[l] ; return ; } void update(int in,int x,int_now) { if( l == in && r == in ) { cl[rt] = x ; return ; } if( in <= (l+r)/2 ) update(in,x,lson) ; else update(in,x,rson) ; push_up(now) ; return ; } int query(int ll,int rr,int_now) { if( ll > r || rr < l ) return 0; if( ll <= l && rr >= r ) return cl[rt] ; return query(ll,rr,lson)+query(ll,rr,rson) ; } void solve(int u,int v) { int temp , f1 , f2 , ans = 0 ; while( u != v ) { if( dep[u] < dep[v] ) { temp = u ; u = v ; v = temp ; } f1 = top[u] ; f2 = top[v] ; if( f1 == f2 ) { ans += query(w[son[v]],w[u],root) ; v = u ; } else if( dep[f1] >= dep[f2] ) { ans += query(w[f1],w[u],root) ; u = fa[f1] ; } else { ans += query(w[f2],w[v],root) ; v = fa[f2] ; } } printf("%d\n", ans) ; return ; } int main() { int i , j , k ; int u , v ; while( scanf("%d %d %d", &n, &m, &t) != EOF ) { cnt = 0 ; memset(head,-1,sizeof(head)) ; for(i = 1 ; i < n ; i++) { scanf("%d %d %d", &u, &v, &k) ; add(u,v,k) ; } dfs() ; n-- ; build(root) ; i = t ; while( m-- ) { scanf("%d", &k) ; if( k == 0 ) { scanf("%d", &j) ; solve(i,j) ; i = j ; } else { scanf("%d %d", &j, &k) ; v = belong[j] ; update(w[v],k,root) ; } } } return 0 ; }