POJ - 2763 Housewife Wind (树链剖分+线段树+边权)

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<iostream>
using namespace std;
const int maxn = 100010;
struct edge {
	int v, ne;
}ed[maxn * 2];
int head[maxn], cnt, n;
int fa[maxn], son[maxn], num[maxn], deep[maxn];
int p[maxn], fp[maxn], top[maxn];
int pos;
void init() {
	cnt = 0;
	memset(head, -1, sizeof head);
	pos = 1;
	memset(son, -1, sizeof son);
}
void addedge(int u, int v) {
	ed[cnt].v = v;
	ed[cnt].ne = head[u];
	head[u] = cnt++;
}
void dfs1(int u, int pre, int d) {
	deep[u] = d;
	fa[u] = pre;
	num[u] = 1;
	for (int i = head[u]; ~i; i = ed[i].ne) {
		int v = ed[i].v;
		if (v == pre)continue;
		dfs1(v, u, d + 1);
		num[u] += num[v];
		if (son[u] == -1 || num[v] > num[son[u]])
			son[u] = v;
	}
}
void getpos(int u, int sp) {
	top[u] = sp;
	p[u] = pos++;
	fp[p[u]] = u;
	if (son[u] == -1)return;
	getpos(son[u], sp);
	for (int i = head[u]; ~i; i = ed[i].ne) {
		int v = ed[i].v;
		if (v != son[u] && v != fa[u]) {
			getpos(v, v);
		}
	}
}
struct node {
	int l, r;
	int len;
}c[maxn * 4];
void build(int rt, int l, int r) {
	c[rt].l = l; c[rt].r = r;
	c[rt].len = 0;
	if (l == r)return;
	int mid = (l + r) >> 1;
	build(rt << 1, l, mid);
	build((rt << 1) | 1, mid + 1, r);
}
void pushup(int rt) {
	c[rt].len = c[rt << 1].len + c[(rt << 1) | 1].len;
}
void update(int rt, int k, int val) {
	if (c[rt].l == k&&c[rt].r == k) {
		c[rt].len = val;
		return;
	}
	int mid = (c[rt].l + c[rt].r) >> 1;
	if (k <= mid)update(rt << 1, k, val);
	else update(((rt << 1) | 1), k, val);
	pushup(rt);
}
int query(int rt, int l, int r) {
	if (c[rt].l == l&&c[rt].r == r)
		return c[rt].len;
	int mid = (c[rt].l + c[rt].r) >> 1;
	if (r <= mid)return query(rt << 1, l, r);
	else if (l > mid)return query((rt << 1) | 1, l, r);
	else return query(rt << 1, l, mid) + query((rt << 1) | 1, mid + 1, r);
}
long long int find(int u, int v) {
	int f1 = top[u], f2 = top[v];
	long long int tmp = 0;
	while (f1 != f2) {
		if (deep[f1] < deep[f2]) {
			swap(f1, f2);
			swap(u, v);
		}
		tmp += query(1, p[f1], p[u]);
		u = fa[f1]; f1 = top[u];
	}
	if (u == v)return tmp;
	if (deep[u] > deep[v])swap(u, v);
	return tmp + query(1, p[son[u]], p[v]);
}
int e[maxn][3];
int main() {
	int te, n, m, now;
	scanf("%d%d%d", &n, &m, &now);
	init();
	for (int i = 1; i <= n - 1; i++) {
		scanf("%d%d%d", &e[i][0], &e[i][1], &e[i][2]);
		addedge(e[i][0], e[i][1]);
		addedge(e[i][1], e[i][0]);
	}
	dfs1(1, 0, 0);
	getpos(1, 1);
	build(1, 1, pos);
	for (int i = 1; i <= n - 1; i++) {
		if (deep[e[i][0]] < deep[e[i][1]]) {
			swap(e[i][0], e[i][1]);
		}
		update(1, p[e[i][0]], e[i][2]);
	}
	while (m--) {
		int a, b, d;
		scanf("%d", &a);
		if (a) {
			scanf("%d%d", &b, &d);
			update(1, p[e[b][0]], d);
		}
		else {
			scanf("%d", &d);
			printf("%lld\n", find(now, d));
			now = d;
		}
	}
	return 0;
}

 

全部评论

相关推荐

11-07 13:31
怀化学院 Java
勇敢牛牛不怕难:又疯一个
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务