hdu3966Aragorn's Story(树链剖分+树状数组维护区间)

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2016
File Name   :简单的树链剖分题
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
#define lson rt << 1
#define rson rt << 1 | 1
#define bug cout << "BUG HERE\n"
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int nCase = 0;
const int maxn = 50010;
int n, m, q;
/**************************************************/
vector<int> G[maxn];
/**************************************************/
//树链剖分部分,第一次确认重链,
//第二次将链上的点连续的hash到树状数组上进行维护。
int fa[maxn], dep[maxn], top[maxn];
int size[maxn], son[maxn];
int SegId[maxn], TreeId[maxn];
int Times;
int dfs_1(int u,int pre, int depth) {
	fa[u] = pre; dep[u] = depth; size[u] = 1;
	int tmp = 0;
	for (int i = 0;i < G[u].size();++i) {
		int v = G[u][i];
		if (v == pre) continue;
		int now = dfs_1(v, u, depth + 1);
		size[u] += now;
		if (now > tmp) {
			son[u] = v;
			tmp = now;
		}
	}
	return size[u];
}
void dfs_2(int u,int header) {
	top[u] = header;
	if (son[u] != -1) {
		SegId[u] = ++Times;
		TreeId[Times] = u;
		dfs_2(son[u], header);
	}else if (son[u] == -1) {
		SegId[u] = ++Times;
		TreeId[Times] = u;
		return ;
	}
	for (int i = 0;i < G[u].size();++i) {
		int v = G[u][i];
		if (v != fa[u] && v != son[u])
			dfs_2(v, v);
	}
}
/**************************************************/
int val[maxn];
int sum[maxn];
inline void input() {
//数据读入
	int u, v;
	for (int i = 1;i <= n;++i)
		G[i].clear();
	Times = 0;
	memset(son, -1,sizeof son);
	memset(sum, 0,sizeof sum);
	for (int i = 1;i <= n;++i)
		scanf("%d",&val[i]);
	for (int i = 1;i < n;++i) {
		scanf("%d%d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
//链处理函数
	dfs_1(1, -1, 0);
	dfs_2(1, 1);

}
/**************************************************/
//树状数组完成区间更新与单点查询
inline int lowbit(int x) {
	return x & (-x);
}

inline void add(int x, int num) {
	while(x < maxn) {
		sum[x] += num;
		x += lowbit(x);
	}
}

inline int getsum(int x) {
	int ret = 0;
	while(x > 0) {
		ret += sum[x];
		x -= lowbit(x);
	}
	return ret;
}
/**************************************************/
inline void change(int u,int v,int k) {
	int p = top[u], q = top[v];
	while(q != p) {
		if (dep[p] < dep[q]) {//不在同一条链中,比较链头的深度,进行调整
			swap(u, v);
			swap(p, q);
		}
		add(SegId[p], k);
		add(SegId[u] + 1, -k);
		u = fa[p];
		p = top[u];
	}
	//已经在同一条链中了,就比较当前点的深度,进行调整
	if (dep[u] < dep[v]) swap(u, v);
	add(SegId[v], k);
	add(SegId[u] + 1, -k);
}


inline void solve() {
	char op[10];
	int u, v, k;

	for (int i = 1;i <= n;++i) {
		add(SegId[i], val[i]);
		add(SegId[i] + 1, -val[i]);
	}

	while(q--) {
		scanf("%s", op);
		if (op[0] == 'Q') {
			scanf("%d",&u);
			printf("%d\n", getsum(SegId[u]));
		}else {
			scanf("%d%d%d",&u,&v,&k);
			if (op[0] == 'D') k = -k;
			change(u, v, k);
		}
	}
}
int main(int argc, const char * argv[])
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	while(~scanf("%d%d%d",&n,&m,&q)) {
		input();
		solve();
	}
	return 0;
}

你可能感兴趣的:(树状数组,树链剖分)