#POJ 2763 Housewife Wind (树状数组 + dfs序 + LCA)

Housewife Wind

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 17267 Accepted: 4718

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

题目大意 : 有一颗N个点的带边权树,M次询问,  每次要么更新第i条边的边权, 要么查询点U到点V的距离(V不断更新)

思路 :区间更新, 单点查询, 首先利用dfs序, 将每个点所能控制的区间找出来, 然后每次dfs回溯回来更新该区间的值, 初始化就为0, 所以不用写差分, 每次更新边权的时候, 影响的就是两点中远离根结点的点的子树到根的距离,这些一次dfs就能全部求出来了, 剩下就是简单的查询操作

这题比较详细的解析在我上一篇博客,不过是用线段树写的, 今天刚学树状数组,拿这道题练练手QAQ

Accepted code

#include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<cstdio>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct Edge
{
	int v, w, next;
}e[MAXN << 1];
struct node
{
	int u, v, w;
}t[MAXN];
int head[MAXN], tmp[MAXN], n, m, cnt;
int p[MAXN][25], d[MAXN], dep[MAXN], rt;
int in[MAXN], out[MAXN], c[MAXN], tot;
void init() {
	MEM(head, -1); MEM(p, 0); MEM(d, 0); MEM(dep, 0);
	MEM(in, 0); MEM(out, 0); MEM(c, 0); MEM(tmp, 0);
	cnt = tot = 0;
}
void add_edge(int from, int to, int wi) {
	e[++cnt].v = to;
	e[cnt].w = wi;
	e[cnt].next = head[from];
	head[from] = cnt;
}
void add(int x, int y) {   // 区间更新
	int i = x;
	while (i <= n) {
		c[i] += y;
		d[i] += (x - 1) * y;
		i += lowbit(i);
	}
}
int Query(int x) {   // 单点查询
	int ans = 0, i = x;
	while (i) {
		ans += x * c[i] - d[i];
		i -= lowbit(i);
	}
	return ans;
}
void dfs(int x, int fa) {
	dep[x] = dep[fa] + 1, p[x][0] = fa;
	tmp[x] = ++tot, in[tmp[x]] = tot;
	for (int i = 1; (1 << i) <= dep[x]; i++)
		p[x][i] = p[p[x][i - 1]][i - 1];
	for (int i = head[x]; i != -1; i = e[i].next) {
		int vi = e[i].v, wi = e[i].w;
		if (vi == fa) continue;
		dfs(vi, x);
		add(in[tmp[vi]], wi); add(out[tmp[vi]] + 1, -wi);  // 差分区间更新
	}
	out[tmp[x]] = tot;
}
int LCA(int x, int y) {
	if (dep[x] > dep[y]) swap(x, y);
	for (int i = 20; i >= 0; i--) {
		if (dep[y] - (1 << i) >= dep[x])
			y = p[y][i];
	}
	if (x == y) return x;
	for (int i = 20; i >= 0; i--) {
		if (p[x][i] == p[y][i]) continue;
		x = p[x][i], y = p[y][i];
	}
	return p[x][0];
}

int main()
{
	while (~sc("%d %d %d", &n, &m, &rt)) {
		init();
		for (int i = 1; i < n; i++) {
			int ui, vi, wi;
			sc("%d %d %d", &ui, &vi, &wi);
			t[i].u = ui, t[i].v = vi, t[i].w = wi;
			add_edge(ui, vi, wi); add_edge(vi, ui, wi);
		}
		dfs(rt, 0);
		for (int i = 0; i < m; i++) {
			int op, ui, vi;
			sc("%d", &op);
			if (op == 0) {
				sc("%d", &ui);
				int lca = LCA(ui, rt);
				int dis1 = Query(tmp[ui]) - Query(tmp[ui] - 1);
				int dis2 = Query(tmp[rt]) - Query(tmp[rt] - 1);
				int dis3 = Query(tmp[lca]) - Query(tmp[lca] - 1);
				cout << dis1 + dis2 - 2 * dis3 << endl;
				rt = ui;
			}
			else {
				sc("%d %d", &ui, &vi);
				int uu = t[ui].u, vv = t[ui].v;
				int U = dep[uu] < dep[vv] ? uu : vv;
				int V = dep[uu] > dep[vv] ? uu : vv;
				int lca = LCA(U, V);
				int dis1 = Query(tmp[V]) - Query(tmp[V] - 1);
				int dis2 = Query(tmp[U]) - Query(tmp[U] - 1);
				int scnt = vi - (dis1 - dis2);
				add(in[tmp[V]], scnt); add(out[tmp[V]] + 1, -scnt);
			}
		}
	}
	return 0;
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 2182是一道使用树状数组解决的题目,题目要求对给定的n个数进行排,并且输出每个数在排后的相对位置。树状数组是一种用来高效处理前缀和问题的数据结构。 根据引用中的描述,我们可以通过遍历数组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的数的个数。这个个数就是它在排后的相对位置。 代码中的query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排后的相对位置,并将结果存入ans数组中。 最后,我们按顺输出ans数组的元素即可得到排后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要求的排功能,其中query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排后的相对位置,并将结果存入ans数组中。最后,我们按顺输出ans数组的元素即可得到排后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值