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

Housewife Wind

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 11419   Accepted: 3140

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

题目链接:POJ 2763

给定一棵树,两种操作,一种是询问当前点到v点的最短距离,并且输出答案之后当前点就变成v点了;第二种是把某一条边的长度变为w。

先考虑只有询问的情况下怎么做,显然是LCA裸题,设dis[u]代表u点到根节点的距离,则有:$dis(u, v) = dis[u]+dis[v]-2*dis[LCA(u,v)]$,然后考虑第二种操作,可以发现当你改变一条边的长度(权值)的时候,会对这条边下的整颗子树造成影响,比如u——v的一条边,且u比v更靠近根节点的话,那么造成的影响是v所在整颗子树的影响,即子树是深度较深的点所连接的。由于是整颗子树,明显可以用DFS序来转换后用树状数组或者线段树来维护这个DFS序列即dis数组,操作就是区间更新,单点查询——改变边长度的时候显然整个子树序列区间长度均改变了$w‘-w$。好像这题一般写法是树链剖分,蒟蒻我不会……就用树状数组好了,树状数组怎么区间更新?左端点处+val,右端点+1处-val即可。

ZZ地WA了一下午发现是把D数组当作深度数组来用了……

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,sum) memset(arr,sum,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct edge
{
	int to, nxt, d;
	edge() {}
	edge(int _to, int _nxt, int _d): to(_to), nxt(_nxt), d(_d) {}
};
edge E[N << 1];
int head[N], tot;
int ver[N << 1], D[N << 1], F[N], sz, dp[N << 1][19];
int W[N], A[N], B[N];
int L[N], R[N], T[N], ts;
bitset<N>vis;

void init()
{
	CLR(head, -1);
	tot = 0;
	sz = 0;
	ts = 0;
	CLR(T, 0);
	vis.reset();
}
void update(int k, int v)
{
	while (k < N)
	{
		T[k] += v;
		k += (k & -k);
	}
}
int query(int k)
{
	int ret = 0;
	while (k)
	{
		ret += T[k];
		k -= (k & -k);
	}
	return ret;
}
inline void add(int s, int t, int d)
{
	E[tot] = edge(t, head[s], d);
	head[s] = tot++;
}
void dfs(int u, int d, int sumdis)
{
	ver[++sz] = u;
	D[sz] = d;
	F[u] = sz;

	L[u] = ++ts;
	update(L[u], sumdis);
	update(L[u] + 1, -sumdis);
	vis[u] = 1;
	for (int i = head[u]; ~i; i = E[i].nxt)
	{
		int v = E[i].to;
		if (!vis[v])
		{
			dfs(v, d + 1, sumdis + E[i].d);
			ver[++sz] = u;
			D[sz] = d;
		}
	}
	R[u] = ts;
}
void RMQinit(int l, int r)
{
	int i, j;
	for (i = l; i <= r; ++i)
		dp[i][0] = i;
	for (j = 1; l + (1 << j) - 1 <= r; ++j)
	{
		for (i = l; i + (1 << j) - 1 <= r; ++i)
		{
			int a = dp[i][j - 1], b = dp[i + (1 << (j - 1))][j - 1];
			dp[i][j] = D[a] < D[b] ? a : b;
		}
	}
}
int LCA(int u, int v)
{
	int l = F[u], r = F[v];
	if (l > r)
		swap(l, r);
	int len = r - l + 1;
	int k = 0;
	while (1 << (k + 1) <= len)
		++k;
	int a = dp[l][k], b = dp[r - (1 << k) + 1][k];
	return D[a] < D[b] ? ver[a] : ver[b];
}
int main(void)
{
	int n, q, s, i, a, b, w;
	while (~scanf("%d%d%d", &n, &q, &s))
	{
		init();
		for (i = 1; i <= n - 1; ++i)
		{
			scanf("%d%d%d", &a, &b, &w);
			A[i] = a;
			B[i] = b;
			W[i] = w;
			add(a, b, w);
			add(b, a, w);
		}
		dfs(s, 1, 0);
		RMQinit(1, sz);
		while (q--)
		{
			int ops;
			scanf("%d", &ops);
			if (ops == 0)
			{
				int v;
				scanf("%d", &v);
				printf("%d\n", query(L[s]) + query(L[v]) - 2 * query(L[LCA(s, v)]));
				s = v;
			}
			else
			{
				int k, neww;
				scanf("%d%d", &k, &neww);
				int u = A[k], v = B[k];
				if (L[u] > L[v])
					swap(u, v);
				update(L[v], neww - W[k]);
				update(R[v] + 1, -(neww - W[k]));
				W[k] = neww;
			}
		}
	}
	return 0;
}
时间: 2024-10-19 14:32:53

POJ 2763 Housewife Wind(DFS序+LCA+树状数组)的相关文章

HDU 6203 ping ping ping(dfs序+LCA+树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意: n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V 无法连通.问无法通行的点最少有多少个. 思路: 贪心思维,破坏两个点的LCA是最佳的.那么怎么判断现在在(u,v)之间的路径上有没有被破坏的点呢,如果没有的话那么此时就要破坏这个lca点.一开始我们要把询问按照u和v的lca深度从大到小排序,如果某个点需要被破坏,那么它的所有子节点都可以不再需要破坏别的点

HDU 3966 dfs序+LCA+树状数组

题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k Q a 查询节点a的权值 数据大小 节点个数 n[1,50000], 操作次数 op[0,30000]; 不会树链剖分 故只有想其他的方法. 这道题有点类似今年上海网络赛的1003 ,不过那题我没做: 算法思路: 以节点1 为根,求出节点i 的 dfs序列 tim[i][2]; 其中tim[i][0

BZOJ 2819 Nim 树链剖分/DFS序+LCA+树状数组

题意:给定一棵树,每个节点是一堆石子,给定两种操作: 1.改变x号节点的石子数量 2.用从x到y的路径上的所有堆石子玩一次Nim游戏,询问是否有必胜策略 Nim游戏有必胜策略的充要条件是所有堆的石子数异或起来不为零 这题首先一看就是树链剖分 然后题目很善良地告诉我们深搜会爆栈 于是我们可以选择广搜版的树链剖分 BFS序从左到右是深搜,从右到左是回溯,一遍BFS就够 单点修改区间查询还可以套用ZKW线段树 不过这题其实不用这么麻烦 有更简单的办法 详见 http://dzy493941464.is

HDU 5156 - Harry and Christmas tree (dfs序+离线树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=5156 BC#25的C题. 题意是:给出一颗大小为n的树,以1为根,然后给出m次染色,每次将节点u加上一种颜色(一个节点可以有多个颜色). 最后查询树上每个节点对应子树上包含的不同颜色数量. 当时这场比赛没有做,回来看一下题目,没看标解就试着敲了一遍,于是解题思路从一开始就走上了不归路. 标解是O(n+m)的方法,主要思路是将问题转为:一次染色表示将u到根节点的路径都染上这种颜色. 但这样做需要去重,因为如果u

【BZOJ1103】大都市meg(DFS序,树状数组)

题意:有一颗树,1号点为根,保证编号小的点深度较小,初始状态每条边都没有被标记,要求实现两个操作在线: A:将连接x,y的边标记 W:查询从1到x的路径上有多少条边未被标记 n<=2*10^5 思路:本题的特殊性质: 1.一次只标记一条边且没有重边 2.直接求1到x的路径,不用LCA 记录i点在DFS序中第一次与最后一次出现的时间b[i]与c[i] 可以发现若(x,y)(x<y)边被标记只对区间(b[y],c[y])有1的贡献 等价于前缀和s[b[y]]++ s[c[y]+1]-- 至于s[c

【Tyvj2133 BZOJ1146】网络管理Network(树套树,DFS序,树状数组,主席树,树上差分)

题意:有一棵N个点的树,每个点有一个点权a[i],要求在线实现以下操作: 1:将X号点的点权修改为Y 2:查询X到Y的路径上第K大的点权 n,q<=80000 a[i]<=10^8 思路:此题明显地体现了我对主席树理解不深 树上路径K大可以直接用树剖+二分答案+树做 但DFS序+主席树也可以 对于点U,它能影响DFS序上的区间(st[u],ed[u]) 所以维护方法就是类似序列K大一样 s[st[u]]++ s[ed[u]+1]-- 对于路径(x,y),信息为s[x]+s[y]-s[lca(x

HDOJ5877(dfs序+离散化+树状数组)

Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2081    Accepted Submission(s): 643 Problem Description You are given a rooted tree of N nodes, labeled from 1 to N. To the ith node a

POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新

题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为d[u]+d[v]-2*d[LCA(u,v)] 现在d数组是变化的 对应每一条边的变化 他修改的是一个区间 用时间戳处理每个点管辖的区域 然后用线段树修改 线段树的叶子节点村的是根到每一个点的距离 求最近公共祖先没差别 只是堕落用线段树维护d数组 各种错误 4个小时 伤不起 #include <cs

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

题目链接:POJ 2763 Housewife Wind 题意:抽象出来就是 一棵已知节点之间的边权,两个操作,1·修改边权,2·询问两个节点之间的边权和. AC代码: #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int