洛谷P3178 [HAOI2015]树上操作(树链剖分)

P3178 [HAOI2015]树上操作

题目描述
有一棵点数为 N 的树,以点 1 为根,且树点有边权。然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a 。操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a 。操作 3 :询问某个节点 x 到根的路径中所有点的点权和。

输入输出格式
输入格式:
第一行包含两个整数 N, M 。表示点数和操作数。接下来一行 N 个整数,表示树中节点的初始权值。接下来 N-1 行每行两个正整数 from, to , 表示该树中存在一条边 (from, to) 。再接下来 M 行,每行分别表示一次操作。其中第一个数表示该操作的种类( 1-3 ) ,之后接这个操作的参数( x 或者 x a ) 。

输出格式:
对于每个询问操作,输出该询问的答案。答案之间用换行隔开。

输入输出样例

输入样例#1:
5 5
1 2 3 4 5
1 2
1 4
2 3
2 5
3 3
1 2 1
3 5
2 1 2
3 3
输出样例#1:
6
9
13
说明
对于 100% 的数据, N,M<=100000 ,且所有输入数据的绝对值都不会超过 10^6 。

代码

#include <cstdio>
#include <algorithm>
#define ll long long
#define N 300000
using namespace std;

struct arr
{
	int to,nxt;
}e[N];
struct tree
{
	int l,r;
	ll sm,lz;
}tr[N];
int n,m;
ll a[N],b[N];
int ls[N],l,cnt;
int dep[N],son[N],fa[N],siz[N];
int id[N],top[N];

void update(int p){tr[p].sm = tr[p * 2].sm + tr[p * 2 + 1].sm;}

void add(int x, int y)
{
	e[++l].to = y;
	e[l].nxt = ls[x];
	ls[x] = l;
}

void dfs1(int x, int father, int deep)
{
	siz[x] = 1;
	fa[x] = father;
	dep[x] = deep;
	for (int i = ls[x]; i; i = e[i].nxt)
		if (e[i].to != father)
		{
			dfs1(e[i].to, x, deep + 1);
			siz[x] += siz[e[i].to];
			if (!son[x] || siz[e[i].to] > siz[son[x]]) son[x] = e[i].to;
		}
}

void dfs2(int x, int tp)
{
	id[x] = ++cnt;
	top[x] = tp;
	a[cnt] = b[x];
	if (!son[x]) return;
	dfs2(son[x], tp);
	for (int i = ls[x]; i; i = e[i].nxt)
		if (!id[e[i].to]) dfs2(e[i].to, e[i].to);
}

void build(int p, int l, int r)
{
	tr[p].l = l;
	tr[p].r = r;
	if (l == r)
	{
		tr[p].sm = a[l];
		return;
	}
	int mid = (tr[p].l + tr[p].r) / 2;
	build(p * 2, l, mid);
	build(p * 2 + 1, mid + 1, r);
	update(p);
}

void down(int p)
{
	if (!tr[p].lz) return;
	int ls = p * 2;
	int rs = ls + 1;
	tr[ls].sm += tr[p].lz * (tr[ls].r - tr[ls].l + 1);
	tr[rs].sm += tr[p].lz * (tr[rs].r - tr[rs].l + 1);
	tr[ls].lz += tr[p].lz;
	tr[rs].lz += tr[p].lz;
	tr[p].lz = 0;
}

void ins(int p, int x, int y, ll w)
{
	if (x <= tr[p].l && y >= tr[p].r)
	{
		tr[p].sm += (tr[p].r - tr[p].l + 1) * w;
		tr[p].lz += w;
		return;
	}
	down(p);
	int mid = (tr[p].l + tr[p].r) / 2;
	if (x <= mid) ins(p * 2, x, y, w);
	if (y > mid) ins(p * 2 + 1, x, y, w);
	update(p);
}

ll sum(int p, int x, int y)
{
	ll ans = 0;
	if (x <= tr[p].l && y >= tr[p].r) return tr[p].sm;
	down(p);
	int mid = (tr[p].l + tr[p].r) / 2;
	if (x <= mid) ans += sum(p * 2, x, y);
	if (y > mid) ans += sum(p * 2 + 1, x, y);
	return ans;
}

ll treesum(int x, int y)
{
	ll ans = 0;
	while (top[x] != top[y])
	{
		if (dep[top[x]] < dep[top[y]]) swap(x, y);
		ans += sum(1, id[top[x]], id[x]);
		x = fa[top[x]];
	}
	if (dep[x] > dep[y]) swap(x, y);
	ans += sum(1, id[x], id[y]);
	return ans;
}

int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
	for (int i = 1; i < n; i++)
	{
		int x, y;
		scanf("%d%d", &x ,&y);
		add(x, y);
		add(y, x);
	}
	dfs1(1, 0, 1);
	dfs2(1, 1);
	build(1, 1, n);
	while (m--)
	{
		int o, x;
		ll w;
		scanf("%d", &o);
		if (o == 1)
		{
			scanf("%d%lld", &x, &w);
			ins(1, id[x], id[x], w);
		}
		if (o == 2)
		{
			scanf("%d%lld", &x, &w);
			ins(1, id[x], id[x] + siz[x] - 1, w);
		}
		if (o == 3)
		{
			scanf("%d", &x);
			printf("%lld\n", treesum(1, x));
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题目还可以使用树状数组或线段树来实现,时间复杂度也为 $\mathcal{O}(n\log n)$。这里给出使用树状数组的实现代码。 解题思路: 1. 读入数据; 2. 将原数列离散化,得到一个新的数列 b; 3. 从右往左依次将 b 数列中的元素插入到树状数组中,并计算逆序对数; 4. 输出逆序对数。 代码实现: ```c++ #include <cstdio> #include <cstdlib> #include <algorithm> const int MAXN = 500005; struct Node { int val, id; bool operator<(const Node& other) const { return val < other.val; } } nodes[MAXN]; int n, a[MAXN], b[MAXN], c[MAXN]; long long ans; inline int lowbit(int x) { return x & (-x); } void update(int x, int val) { for (int i = x; i <= n; i += lowbit(i)) { c[i] += val; } } int query(int x) { int res = 0; for (int i = x; i > 0; i -= lowbit(i)) { res += c[i]; } return res; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); nodes[i] = {a[i], i}; } std::sort(nodes + 1, nodes + n + 1); int cnt = 0; for (int i = 1; i <= n; ++i) { if (i == 1 || nodes[i].val != nodes[i - 1].val) { ++cnt; } b[nodes[i].id] = cnt; } for (int i = n; i >= 1; --i) { ans += query(b[i] - 1); update(b[i], 1); } printf("%lld\n", ans); return 0; } ``` 注意事项: - 在对原数列进行离散化时,需要记录每个元素在原数列中的位置,便于后面计算逆序对数; - 设树状数组的大小为 $n$,则树状数组中的下标从 $1$ 到 $n$,而不是从 $0$ 到 $n-1$; - 在计算逆序对数时,需要查询离散化后的数列中比当前元素小的元素个数,即查询 $b_i-1$ 位置上的值; - 在插入元素时,需要将离散化后的数列的元素从右往左依次插入树状数组中,而不是从左往右。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值