HDU 6162 树链剖分

题意:给你一颗树,每个节点有有一个权值,每次询问从x到y的最短路上权值在c到d之间的所有的点的权值和是多少。

思路:肯定要用树剖,因为询问c到d之间这种操作树上倍增很难做,但是用其它数据结构可以比较好的查询。我们可以用线段树来进行这种操作。每次询问一个区间时,如果当前区间被查询区间完全覆盖,并且区间里的最大指小于等于d,最小值大于等于c,才返回,否则继续查询。这种做法其实可以被卡掉,比如很长的路径上点权都是1, 2, 1, 2这种,而询问的c和d都是1,这样线段树上的询问会被卡成O(n)的。我感觉比较可行的做法是离散化之后用主席树,这样可以保证O(logn),但是既然没卡这个,就懒得写这种做法了。

线段树做法:

#include <bits/stdc++.h>
#define LL long long
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
using namespace std;
const int maxn = 100010;
int head[maxn], Next[maxn * 2], ver[maxn * 2];
int son[maxn], d[maxn], f[maxn], top[maxn], sz[maxn], dfn[maxn];
int n, m, tot, cnt;
int a[maxn], b[maxn];
void add(int x, int y) {
	ver[++tot] = y;
	Next[tot] = head[x];
	head[x] = tot;
}
struct SegmentTree {
	int mx, mi;
	LL sum;
};
SegmentTree tr[maxn * 4];
void pushup(int o) {
	tr[o].sum = tr[ls(o)].sum + tr[rs(o)].sum;
	tr[o].mi = min(tr[ls(o)].mi, tr[rs(o)].mi);
	tr[o].mx = max(tr[ls(o)].mx, tr[rs(o)].mx);
}
void build(int o, int l, int r) {
	if(l == r) {
		tr[o].mx = tr[o].mi = tr[o].sum = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(ls(o), l, mid);
	build(rs(o), mid + 1, r);
	pushup(o);
}
bool match(int o, int l, int r) {
	return tr[o].mi >= l && tr[o].mx <= r;
}
LL query(int o, int l, int r, int ql, int qr, int lb, int rb) {
	if(l >= ql && r <= qr && match(o, lb, rb)) {
		return tr[o].sum;
	}
	int mid = (l + r) >> 1;
	LL ans = 0;
	if(ql <= mid && !(tr[ls(o)].mx < lb || tr[ls(o)].mi > rb)) ans += query(ls(o), l, mid, ql, qr, lb, rb);
	if(qr > mid && !(tr[rs(o)].mx < lb || tr[rs(o)].mi > rb)) ans += query(rs(o), mid + 1, r, ql, qr, lb, rb);
	return ans;
}
void dfs1(int x, int fa) {
	sz[x] = 1;
	f[x] = fa;
	d[x] = d[fa] + 1;
	for (int i = head[x]; i; i = Next[i]) {
		int y = ver[i];
		if(y == fa) continue;
		dfs1(y, x);
		sz[x] += sz[y];
		if(!son[x] || sz[y] > sz[son[x]])
			son[x] = y;
	}
}
void dfs2(int x, int fa, int t) {
	top[x] = t;
	dfn[x] = ++cnt;
	a[dfn[x]] = b[x];
	if(son[x]) dfs2(son[x], x, t);
	for (int i = head[x]; i; i = Next[i]) {
		int y = ver[i];
		if(y == fa || y == son[x]) continue;
			dfs2(y, x, y);
	}
}
LL solve(int l, int r, int x, int y) {
	LL ans = 0;
	while(top[l] != top[r]) {
		if(d[top[l]] > d[top[r]]) {
			ans += query(1, 1, n, dfn[top[l]], dfn[l], x, y);
			l = f[top[l]];
		} else {
			ans += query(1, 1, n, dfn[top[r]], dfn[r], x, y);
			r = f[top[r]];
		}
	}
	if(d[l] < d[r]) ans += query(1, 1, n, dfn[l], dfn[r], x, y);
	else ans += query(1, 1, n, dfn[r], dfn[l], x, y);
	return ans;
}
int main() {
	int x, y, l, r;
	while(~scanf("%d%d", &n, &m)) {
		memset(head, 0, sizeof(head));
		memset(son, 0, sizeof(son));
		memset(f, 0, sizeof(f));
		memset(sz, 0, sizeof(sz));
		memset(top, 0, sizeof(top));
		memset(d, 0, sizeof(d));
		tot = 0;
		cnt = 0;
		for (int i = 1; i <= n; i++)
			scanf("%d", &b[i]);
		for (int i = 1; i <= n - 1; i++) {
			scanf("%d%d", &x, &y);
			add(x, y);
			add(y, x);
		}
		dfs1(1, 0);
		dfs2(1, 0, 1);
		build(1, 1, n);
		while(m--) {
			scanf("%d%d%d%d", &l, &r, &x, &y);
			cout << solve(l, r, x, y);
			if(m == 0) cout << endl;
			else cout << " ";
		}
	}
}

  

原文地址:https://www.cnblogs.com/pkgunboat/p/10651445.html

时间: 2024-10-26 19:50:45

HDU 6162 树链剖分的相关文章

hdu 5893 (树链剖分+合并)

List wants to travel Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 429    Accepted Submission(s): 92 Problem Description A boy named List who is perfect in English. Now he wants to travel an

hdu 5052 树链剖分

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 982    Accepted Submission(s): 274 Problem Description Yaoge likes to eat chicken chops late at night. Yaoge has eaten too

hdu 5242 树链剖分找权值最大的前k条链

http://acm.hdu.edu.cn/showproblem.php?pid=5242 Problem Description It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play k games sim

hdu 5274 树链剖分

Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1484    Accepted Submission(s): 347 Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes

hdu 4897 树链剖分(重轻链)

Little Devil I Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 894    Accepted Submission(s): 296 Problem Description There is an old country and the king fell in love with a devil. The devil

HDU 3966 (树链剖分+线段树)

Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板题. 实质上树链剖分进行了点对点的一次映射,保证了重链上的点在线段树上的位置是连续的. 树链剖分的两个性质(转): 性质1:如果(v,u)为轻边,则siz[u] * 2 < siz[v]: 性质2:从根到某一点的路径上轻链.重链的个数都不大于logn. 保证了一个区间的时间复杂度是log2(n).

HDU 5274(树链剖分)

树链剖分第一题QAQ,纪念下 #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const ll mod = 1e9 + 7; const int maxn = 1e5 + 10; #define

HDU 5029 树链剖分

Relief grain Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 861    Accepted Submission(s): 219 Problem Description The soil is cracking up because of the drought and the rabbit kingdom is f

hdu 3804树链剖分+离线操作

/* 树链刨分+离线操作 题意:给你一棵树,和询问x,y 从节点x--节点1的小于等于y的最大值. 解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序 对于每次询问y将小于等于y的边权值的边加入,在进行询问将结果储存最后输出即可 易错点:要考虑到节点1到节点1的情况需特判. */ #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string