POJ 2763 Housewife Wind

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

Solution

树链剖分题目,每次修改的时候修改线段树的值就行了。
PS:脑子太乱啦!TIDTTIDTTTID傻傻分不清…(貌似TID是点x在线段树中的位置,TTID是TID的反函数,TTTID是编号为x的边在线段树中的位置)

Code

#include <cstdio>
int getint() {
	int r = 0, k = 1; char c = getchar();
	for (; c < '0' || c > '9'; c = getchar()) if (c == '-') k = -1;
	for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
	return r * k;
}
const int maxn = 101010, maxe = maxn<<1;
struct edge_type {
	int to, next, w, id;
} edge[maxe];
int rdu[maxn], rdv[maxn], rdw[maxn];
int dis[maxn], fa[maxn], dep[maxn], siz[maxn], tid[maxn], ttid[maxn], son[maxn], top[maxn];
int n, q, s, cnte = 0, h[maxn];
void ins(int u, int v, int w, int id) {
	edge[++cnte].to = v;
	edge[cnte].next = h[u];
	edge[cnte].w = w;
	edge[cnte].id = id;
	h[u] = cnte;
	edge[++cnte].to = u;
	edge[cnte].next = h[v];
	edge[cnte].w = w;
	edge[cnte].id = id;
	h[v] = cnte;
}
void dfs1(int now, int father, int deep, int distance) {
	dis[now] = distance; fa[now] = father; dep[now] = deep; siz[now] = 1; son[now] = 0;
	for (int i = h[now]; i; i = edge[i].next) {
		if (edge[i].to == father) continue;
		dfs1(edge[i].to, now, deep+1, distance + edge[i].w);
		siz[now] += siz[edge[i].to];
		if (siz[son[now]] < siz[edge[i].to]) son[now] = edge[i].to;
	}
}
int dfs_clock = 0, tree[maxn*6], tttid[maxn];
void dfs2(int now, int father, int tp, int id) {
	top[now] = tp;
	tid[now] = ++dfs_clock;
	ttid[tid[now]] = id;
	tttid[id] = tid[now];
	if (son[now]) {
		int iid;
		for (int i = h[now]; i; i = edge[i].next) if (edge[i].to == son[now]) iid = i;
		dfs2(son[now], now, tp, edge[iid].id);
		for (int i = h[now]; i; i = edge[i].next) {
			if (father == edge[i].to || edge[i].to == son[now]) continue;
			dfs2(edge[i].to, now, edge[i].to, edge[i].id);
		}
	}
}
void build(int now, int l, int r) {
	if (l == r) {
		tree[now] = rdw[ttid[l]];
		return;
	}
	int mid = (l + r) >> 1;
	build(now<<1, l, mid);
	build(now<<1|1, mid+1, r);
	tree[now] = tree[now<<1]+tree[now<<1|1];
}
void update(int now, int l, int r, int pos, int d) {
	if (l == r) {
		tree[now] = d;
		return;
	}
	int mid = (l + r) >> 1;
	if (pos <= mid) update(now<<1, l, mid, pos, d);
	else update(now<<1|1, mid+1, r, pos, d);
	tree[now] = tree[now<<1] + tree[now<<1|1];
}
int qqq(int now, int l, int r, int ll, int rr) {
	if (ll <= l && r <= rr)
		return tree[now];
	int mid = (l + r) >> 1;
	int ret = 0;
	if (ll <= mid) ret = qqq(now<<1, l, mid, ll, rr);
	if (rr > mid) ret += qqq(now<<1|1, mid+1, r, ll, rr);
	return ret;
}
int lca(int u, int v) {
	while (top[u] != top[v]) {
		if (dep[top[u]] < dep[top[v]]) v = fa[top[v]];
		else u = fa[top[u]];
	}
	if (dep[u] < dep[v]) return u;
	return v;
}
int query(int u, int v) {
	int ret = 0;
	int l = lca(u, v);
	while (top[u] != top[l]) {
		ret += qqq(1, 1, n, tid[top[u]], tid[u]);
		u = fa[top[u]];
	}
	if (u != l) ret += qqq(1, 1, n, tid[son[l]], tid[u]);
	u = v;
	while (top[u] != top[l]) {
		ret += qqq(1, 1, n, tid[top[u]], tid[u]);
		u = fa[top[u]];
	}
	if (u != l) ret += qqq(1, 1, n, tid[son[l]], tid[u]);
	return ret;
}
int main() {
	n = getint(); q = getint(); s = getint();
	int u, v, w;
	for (int i = 1; i < n; ++i) {
		u = getint(); v = getint(); w = getint();
		ins(u, v, w, i);
		rdu[i] = u;
		rdv[i] = v;
		rdw[i] = w;
	}
	dfs1(1, -1, 1, 0);
	dfs2(1, -1, 1, -1);
	build(1, 1, n);
	while (q--) {
		u = getint();
		if (u) {
			v = getint(); w = getint();
			update(1, 1, n, tttid[v], w);
		} else {
			v = getint();
			printf("%d\n", query(v, s));
			s = v;
		}
	}
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据