# [POJ 2763] Housewife Wind【树的DFS序+树状数组+LCA】

• 2018-01-21
• 0
• 0

## Problem:

 Time Limit: 4000MS Memory Limit: 65536K

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
```

Source

POJ Monthly--2006.02.26,zgl & twb

## Solution:

DFS 序的用处就是将子树转化为连续区间，而区间问题一般可以用高级数据结构来维护。

## Code: O((n+q)logn) [12892K, 1219MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

inline int getint(){
char ch; int num;
while(!isdigit(ch = getchar()));
num = ch - '0';
while(isdigit(ch = getchar())) num = (num << 1) + (num << 3) + ch - '0';
return num;
}

int n, q, s;
int fa, dep, jmp;
int dfn, dfstime = 0;
int upp;
int dis;  // The distance to the root with the subscription of dfn[]

struct Edge{
int np, cost;
Edge *nxt;
};

struct Graph{
Edge *V, E;
int tope;

inline void addedge(int u, int v, int w){
E[++tope].np = v, E[tope].cost = w;
E[tope].nxt = V[u], V[u] = &E[tope];
}
} G;

struct BIT{
int node;

inline void add(int u, int v) {while(u < 100010) node[u] += v, u += u & -u;}

inline int query(int u) {int res = 0; while(u) res += node[u], u -= u & -u; return res;}
} b;

inline void DFS(int u, int dist){
dep[u] = dep[fa[u]] + 1;
dis[dfn[u] = ++dfstime] = dist;
for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
if(ne->np != fa[u]) fa[ne->np] = u, DFS(ne->np, dist + ne->cost);
upp[u] = dfstime;
}

inline void Init_jmp(){
for(register int i = 1; i <= n; i++) jmp[i] = fa[i];
for(register int ex = 1; ex < 17; ex++)
for(register int i = 1; i <= n; i++)
jmp[ex][i] = jmp[ex - 1][jmp[ex - 1][i]];
}

inline int Get_LCA(int u, int v){
if(dep[u] < dep[v]) swap(u, v);
int ddep = dep[u] - dep[v];
for(register int ex = 16; ex >= 0; ex--)
if(ddep & (1 << ex)) u = jmp[ex][u];
if(u == v) return v;
for(register int ex = 16; ex >= 0; ex--)
if(jmp[ex][u] != jmp[ex][v])
u = jmp[ex][u], v = jmp[ex][v];
return fa[v];
}

int main(){
n = getint(), q = getint(), s = getint();
for(register int i = 1; i < n; i++){
int u = getint(), v = getint(), w = getint();
}
DFS(1, 0), Init_jmp();
for(register int i = 2; i <= n; i++) b.add(i, dis[i] - dis[i - 1]);  // Region modification & Single query
for(register int i = 1; i <= q; i++){
int op = getint();
if(!op){
int u = getint(), lca = Get_LCA(s, u);
printf("%d\n", b.query(dfn[s]) + b.query(dfn[u]) - (b.query(dfn[lca]) << 1));
s = u;
}
else{
int e = getint() << 1, w = getint();
if(G.E[e].np == fa[G.E[e - 1].np]) e--;
G.E[e].cost = w;
}
}
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai