HYSBZ - 1036 树的统计

思路:这是经典的树链剖分,有三个操作。

1.两点路径中的最大点权;

2.两点路径中的点权和;

3.更改某个点的权值;

如果不用数据结构维护的话,直接搜带来的就是时间消耗太大,主要是因为搜的过程中做了很多无用功,不够直接的找到目标点和线段。树剖就是先按照儿子节点数的节点多少进行划分险段,线段不会相交,树上两个点的路径必然是通过它们的LCA的。我们可以通过线段往上找,知道两个点在同一个线段中,这样我们就可以成段的更新值了。

/*****************************************
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 = 30100;
int n, q;
int a[maxn];
/*************************************************/
struct node {
	int l, r;
	int maxNum;
	LL sum;
}p[maxn<<2];
int tmp[maxn];
void up(int rt) {
	p[rt].maxNum = max(p[lson].maxNum, p[rson].maxNum);
	p[rt].sum = p[lson].sum + p[rson].sum;
}
void build(int rt,int L, int R) {
	p[rt].l = L, p[rt].r = R;
	if (L == R) {
		p[rt].maxNum = p[rt].sum = tmp[L];
		return ;
	}
	int mid = (L + R) >> 1;
	build(lson, L, mid);
	build(rson, mid + 1, R);
	up(rt);
}
void updata(int rt, int pos,int val) {
	if (p[rt].l == pos && p[rt].r == pos) {
		p[rt].sum = p[rt].maxNum = val;
		return ;
	}
	int mid = (p[rt].l + p[rt].r) >> 1;
	if (pos <= mid) updata(lson, pos, val);
	if (pos > mid) updata(rson, pos, val);
	up(rt);
}
LL QuerySum(int rt,int L, int R) {
	if (L <= p[rt].l && p[rt].r <= R) {
		return 1LL*p[rt].sum;
	}
	LL ret = 0LL;
	int mid = (p[rt].l + p[rt].r) >> 1;
	if (L <= mid) ret += QuerySum(lson, L, R);
	if (R > mid) ret += QuerySum(rson, L, R);
	return ret;
}
int QueryMax(int rt,int L, int R) {
	if (L <= p[rt].l && p[rt].r <= R) {
		return p[rt].maxNum;
	}
	int ans = -inf;
	int mid = (p[rt].l + p[rt].r) >> 1;
	if (L <= mid) ans = max(ans, QueryMax(lson, L, R));
	if (R > mid) ans = max(ans, QueryMax(rson, L, R));
	return ans;
}
/*************************************************/
vector<int> G[maxn];
int Times;
int fa[maxn], top[maxn], size[maxn], son[maxn];
int SegId[maxn], TreeId[maxn];
int dep[maxn];

int dfs_first(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_first(v, u, depth + 1);
		size[u] += now;
		if (now > tmp) {
			tmp = now;
			son[u] = v;
		}
	}
	return size[u];
}


void dfs_second(int u,int header) {
	top[u] = header;
	if (son[u] != -1) {
		SegId[u] = ++Times;
		TreeId[Times] = u;
		dfs_second(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]) continue;
		dfs_second(v, v);
	}
}
/*************************************************/
int findmax(int u,int v) {
	int ans = -inf;
	int p = top[u], q = top[v];
	while(p != q) {
		if (dep[p] < dep[q]) {
			swap(u, v);
			swap(p, q);
		}
		ans = max(ans, QueryMax(1,SegId[p], SegId[u]));
		u = fa[p];
		p = top[u];
	}
	if (dep[u] < dep[v]) swap(u, v);
	ans = max(ans, QueryMax(1, SegId[v], SegId[u]));
	return ans;
}
LL findsum(int u,int v) {
	int p = top[u], q = top[v];
	LL sum = 0LL;
	while(p != q) {
		if (dep[p] < dep[q]) {
			swap(u, v);
			swap(p, q);
		}
		sum += QuerySum(1, SegId[p], SegId[u]);
		u = fa[p];
		p = top[u];
	}
	if (dep[u] < dep[v]) swap(u, v);
	sum += QuerySum(1, SegId[v], SegId[u]);
	return sum;
}
void change(int u,int val) {
	updata(1, SegId[u], val);
}
/*************************************************/
int main(int argc, const char * argv[])
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	while(~scanf("%d",&n)) {
		for (int i = 1;i <= n;++i)
			G[i].clear();
		int u, v;
		for (int i = 1;i < n;++i) {
			scanf("%d%d",&u,&v);
			G[u].push_back(v);
			G[v].push_back(u);
		}

		memset(son, -1, sizeof son);
		Times = 0;
		for (int i = 1;i <= n;++i)
			scanf("%d", &a[i]);

		//链的剖分
		dfs_first(1, -1, 0);
		dfs_second(1, 1);


		for (int i = 1;i <= n;++i) {
			tmp[ SegId[i] ] = a[i];
		}
		//建立线段树
		build(1, 1, n);
		scanf("%d",&q);
		char op[10];
		while(q--) {
			scanf("%s", op);
			if (strcmp(op, "QMAX") == 0) {
				scanf("%d%d",&u,&v);
				printf("%d\n", findmax(u, v));
			}
			if (strcmp(op, "QSUM") == 0) {
				scanf("%d%d",&u,&v);
				printf("%lld\n", findsum(u, v));
			}
			if (strcmp(op, "CHANGE") == 0) {
				scanf("%d%d",&u,&v);
				change(u, v);
			}
		}
	}
	return 0;
}



你可能感兴趣的:(数据结构,树链剖分)