HDU - 3966 Aragorn's Story(树链剖分模板题)

本文介绍了一个基于洛谷在线评测系统(OJ)的树状数组实战案例,通过解决一个涉及树形结构和区间更新的问题,展示了树状数组在处理动态数据结构操作上的高效性和灵活性。案例中,我们面临的是在一个由N个节点构成的树形结构上进行多次操作,包括增加或减少路径上的士兵数量,并实时查询特定节点上的士兵总数。为了解决这个问题,我们使用了树状数组和重链剖分技术,有效地处理了大量更新和查询请求。

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

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.

存模板,只询问单点,不需要pushUp,pushDown也只用来节省时间

#include<bits/stdc++.h>
using namespace std;

const int maxn = 50005;
vector<int> G[maxn];
int n, m, q;
int in[maxn];
//子节点个数,深度
int sz[maxn], dep[maxn];
//重儿子,父节点
int ch[maxn], fa[maxn];
//重链开头,dfs序,原序
int top[maxn], tid[maxn], tid2[maxn];
int sum[maxn << 2], lazy[maxn << 2];
int tot;


void dfs1(int u, int f, int d) {
	sz[u] = 1;
	fa[u] = f;
	dep[u] = d;
	for(int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if(v == f) {
			continue;
		}
		dfs1(v, u, d + 1);
		sz[u] += sz[v];
		if(ch[u] == -1 || sz[v] > sz[ch[u]]) {
			ch[u] = v;
		}
	}
}

void dfs2(int u, int tp) {
	top[u] = tp;
	tid[u] = ++tot;
	tid2[tot] = u;
	if(ch[u] == -1) {
		return;
	}
	dfs2(ch[u], tp);
	for(int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if(v != ch[u] && v != fa[u]) {
			dfs2(v, v);
		}
	}
}

void pushDown(int i, int len) {
	if(!lazy[i]) {
		return;
	}
	lazy[i << 1] += lazy[i];
	lazy[i << 1 | 1] += lazy[i];
	sum[i << 1] += (len - (len >> 1)) * lazy[i];
	sum[i << 1 | 1] += (len >> 1) * lazy[i];
	lazy[i] = 0;
}

void build(int i, int l, int r) {
	lazy[i] = 0;
	if(l == r) {
		// 以重链优先,dfs顺序加入线段树
		sum[i] = in[tid2[l]];
		return;
	}
	int mid = (l + r) >> 1;
	build(i << 1, l, mid);
	build(i << 1 | 1, mid + 1, r);
}

void update(int i, int l, int r, int L, int R, int val) {
	if(l >= L && r <= R) {
		sum[i] += (r - l + 1) * val;
		lazy[i] += val;
		return;
	}
	pushDown(i, r - l + 1);
	int mid = (l + r) >> 1;
	if(L <= mid) {
		update(i << 1, l, mid, L, R, val);
	}
	if(R > mid) {
		update(i << 1 | 1, mid + 1, r, L, R, val);
	}
}

int query(int i, int l, int r, int pos) {
	if(l == r) {
		return sum[i];
	}
	pushDown(i, r - l + 1);
	int mid = (l + r) >> 1;
	int ans;
	if(pos <= mid) {
		ans = query(i << 1, l, mid, pos);
	} else {
		ans = query(i << 1 | 1, mid + 1, r, pos);
	}
	return ans;
}

void solve(int u, int v, int val) {
	int f1 = top[u], f2 = top[v];
	while(f1 != f2) {
		if(dep[f1] < dep[f2]) {
			swap(f1, f2);
			swap(u, v);
		}
		update(1, 1, n, tid[f1], tid[u], val);
		u = fa[f1];
		f1 = top[u];
	}
	if(dep[u] < dep[v]) {
		swap(u, v);
	}
	update(1, 1, n, tid[v], tid[u], val);
}

/*
9 8 99
1 1 1 1 1 1 1 1 1
1 2
2 3
1 4
4 5
4 6
6 8
6 7
8 9
*/


int main() {
	while(~scanf("%d%d%d", &n, &m, &q)) {
		memset(ch, -1, sizeof(ch));
		for(int i = 1; i <= n; i++) {
			G[i].clear();
		}
		for(int i = 1; i <= n; i++) {
			scanf("%d", &in[i]);
		}

		int u, v, val;
		while(m--) {
			scanf("%d%d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}

		tot = 0;
		dfs1(1, 0, 0);
		dfs2(1, 1);
		build(1, 1, n);

		char op[3];
		while(q--) {
			scanf("%s", op);
			if(op[0] == 'Q') {
				scanf("%d", &u);
				printf("%d\n", query(1, 1, n, tid[u]));
			} else {
				scanf("%d%d%d", &u, &v, &val);
				if(op[0] == 'D') {
					val = -val;
				}
				solve(u, v, val);
			}
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值