hdu 3966 Aragorn's Story (树链剖分+树状数组差分)

本文介绍了一种解决树形结构问题的方法,通过树链剖分和差分树状数组实现区间查询和更新操作。文章详细解释了树链剖分的概念,包括重链和轻链的划分,以及如何利用差分树状数组进行区间修改和单点查询。通过实例讲解了算法的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966

Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time.

 

 

Input

Multiple test cases, process to the end of input.

For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1.

The next line contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies.

The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v.

The next P lines will start with a capital letter 'I', 'D' or 'Q' for each line.

'I', followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps.

'D', followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps.

'Q', followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.

 

 

Output

For each query, you need to output the actually number of enemies in the specified camp.

 

 

Sample Input

 

3 2 5 1 2 3 2 1 2 3 I 1 3 5 Q 2 D 1 2 2 Q 1 Q 3

 

 

Sample Output

 

7 4 8

 

给定一棵树。q次操作。

今天让队友讲了一下树链剖分和树状数组,给他看了一下这个题,他说是差分树状数组,也就学了一下。

树链剖分是把一棵树划分成重链和轻链,用来做区间查询求值的。这个题用了差分树状数组维护。

树链剖分还时常与线段树配合使用。

树状数组与差分树装数组几乎一样。都是用前缀和来处理。树状数组是区间查询,单点修改。差分数组是区间修改,单点查询。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 50010;
struct Edge
{
	int to, nxt;
}edge[maxn * 2];
int head[maxn], tot;
int top[maxn]; //top[v]表示v所在重链的顶端节点
int fa[maxn]; //父亲节点
int dep[maxn]; //深度
int num[maxn]; //num[v]表示以v为根的子树的节点数
int p[maxn]; //每个节点剖分后的新编号 
int fp[maxn]; //当前节点在树状数组中的位置 
int son[maxn]; //重儿子
int pos, n;
int bit[maxn];
int a[maxn];

void init()
{
	tot = 0;
	memset(head, -1, sizeof(head));
	pos = 1; //使用树状数组,编号从1开始
	memset(son, -1, sizeof(son));
}

void addedge(int u, int v)
{
	edge[tot].to = v;
	edge[tot].nxt = head[u];
	head[u] = tot++;
}

void dfs1(int u, int pre, int d)
{
	dep[u] = d;
	fa[u] = pre;
	num[u] = 1;
	for (int i = head[u]; i != -1; i = edge[i].nxt)
	{
		int v = edge[i].to;
		if (v != pre)
		{
			dfs1(v, u, d + 1);
			num[u] += num[v];
			if (son[u] == -1 || num[v] > num[son[u]])
			{
				son[u] = v;
			}
		}
	}
}

void dfs2(int u, int sp)
{
	top[u] = sp;
	p[u] = pos++;
	fp[p[u]] = u;
	if (son[u] == -1)
	{
		return;
	}
	dfs2(son[u], sp);
	for (int i = head[u]; i != -1; i = edge[i].nxt)
	{
		int v = edge[i].to;
		if (v != son[u] && v != fa[u])
		{
			dfs2(v, v);
		}
	}
}

int lowbit(int x)
{
	return x & (-x);
}

void add(int k, int val)
{
	while (k <= n)
	{
		bit[k] += val;
		k += lowbit(k);
	}
}

int sum(int k)
{
	int s = 0;
	while (k)
	{
		s += bit[k];
		k -= lowbit(k);
	}
	return s;
}

void update(int u, int v, int val) //u->v的路径上点的值的改变
{
	int f1 = top[u], f2 = top[v];
	int tmp = 0;
	while (f1 != f2)
	{
		if (dep[f1] < dep[f2])
		{
			swap(f1, f2);
			swap(u, v);
		}
		add(p[f1], val);     //p是每个节点剖分后的新编号
		add(p[u] + 1, -val);
		u = fa[f1];
		f1 = top[u];
	}
	if (dep[u] > dep[v])
	{
		swap(u, v);
	}
	add(p[u], val);
	add(p[v] + 1, -val);
}

int main()
{
	//freopen("C://input.txt", "r", stdin);
	int m, P, u, v, C1, C2, K;
	char op[10];
	while (scanf("%d%d%d", &n, &m, &P) != EOF)
	{
		init();
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
		while (m--)
		{
			scanf("%d%d", &u, &v);
			addedge(u, v);
			addedge(v, u);
		}
		dfs1(1, 0, 0);
		dfs2(1, 1);
		memset(bit, 0, sizeof(bit));
		for (int i = 1; i <= n; i++)
		{
			add(p[i], a[i]);
			add(p[i] + 1, -a[i]);
		}
		while (P--)
		{
			scanf("%s", op);
			if (op[0] == 'Q')
			{
				scanf("%d", &u);
				printf("%d\n", sum(p[u]));
			}
			else
			{
				scanf("%d%d%d", &C1, &C2, &K);
				if (op[0] == 'D') K = -K;
				update(C1, C2, K);
			}
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值