bzoj 4372 烁烁的游戏 动态点分治+线段树

题面

题目传送门

解法

动态点分治模板题

什么是动态点分治呢???

静态的点分治就是不断地找到当前树的重心,然后分成若干个子树继续递归下去

但是如果有修改似乎静态的就不好处理了

我们现在引入一个叫点分树的东西

说白了,就是每一次静态点分治的时候这一次的重心和上一次的连边,这样形成了一棵树

再说清楚一点,就是如果这棵树上有一条边\((x,y)\)\(x\)\(y\)的父亲

那么说明\(x\)是某一棵子树的重心,\(y\)\(x\)子树的重心

根据重心的性质,这一棵树的深度不超过\(O(log\ n)\)

对于这道题,修改时就是在点分树上找到这个点,然后不断向上,用线段树维护区间加法

查询的时候不断向上爬,累加答案

注意处理点分树上父亲对儿子答案产生的影响

时间复杂度:\(O(n\ log^2\ n)\)

代码

#include <bits/stdc++.h>
#define inf INT_MAX
#define N 100010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
	x = 0; int f = 1; char c = getchar();
	while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
	while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
int n, q, rt, now, cnt, d[N], p[N], ff[N], vis[N], siz[N], f[N][18];
struct Edge {
	int next, num;
} e[N * 3];
struct SegmentTree {
	struct Node {
		int lc, rc, del;
	} t[N * 100];
	int tot, rt[N];
	void New(int &k) {if (!k) k = ++tot;}
	void modify(int &k, int l, int r, int L, int R, int v) {
		if (L > R) return; New(k);
		if (L <= l && r <= R) {
			t[k].del += v;
			return;
		}
		int mid = (l + r) >> 1;
		if (L <= mid && mid < R)
			modify(t[k].lc, l, mid, L, mid, v), modify(t[k].rc, mid + 1, r, mid + 1, R, v);
		if (R <= mid) modify(t[k].lc, l, mid, L, R, v);
		if (L > mid) modify(t[k].rc, mid + 1, r, L, R, v);
	}
	void Add(int x, int l, int r, int v) {modify(rt[x], 0, n, l, r, v);}
	int query(int k, int l, int r, int x) {
		if (!k) return 0; 
		if (l == r) return t[k].del;
		int ans = t[k].del;
		int mid = (l + r) >> 1;
		if (x <= mid) return ans + query(t[k].lc, l, mid, x);
		return ans + query(t[k].rc, mid + 1, r, x);
	}
	int ask(int x, int d) {return query(rt[x], 0, n, d);}
} T1, T2;
void add(int x, int y) {
	e[++cnt] = (Edge) {e[x].next, y};
	e[x].next = cnt;
}
void getr(int x, int fa) {
	siz[x] = 1, ff[x] = 0;
	for (int p = e[x].next; p; p = e[p].next) {
		int k = e[p].num;
		if (k == fa || vis[k]) continue;
		getr(k, x); siz[x] += siz[k];
		ff[x] = max(ff[x], siz[k]);
	}
	ff[x] = max(ff[x], now - siz[x]);
	if (ff[x] < ff[rt]) rt = x;
}
void work(int x, int fa) {
	vis[x] = 1, p[x] = fa;
	for (int p = e[x].next; p; p = e[p].next) {
		int k = e[p].num;
		if (k == fa || vis[k]) continue;
		ff[rt = 0] = inf, now = siz[k];
		getr(k, x); work(rt, x);
	}
}
void dfs(int x, int fa) {
	d[x] = d[fa] + 1;
	for (int i = 1; i <= 17; i++)
		f[x][i] = f[f[x][i - 1]][i - 1];
	for (int p = e[x].next; p; p = e[p].next) {
		int k = e[p].num;
		if (k == fa) continue; f[k][0] = x;
		dfs(k, x);
	}
}
int lca(int x, int y) {
	if (d[x] < d[y]) swap(x, y);
	for (int i = 17; i >= 0; i--)
		if (d[f[x][i]] >= d[y]) x = f[x][i];
	if (x == y) return x;
	for (int i = 17; i >= 0; i--)
		if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
	return f[x][0];
}
int dis(int x, int y) {
	int z = lca(x, y);
	return d[x] + d[y] - 2 * d[z];
}
void Modify(int x, int d, int v) {
	T1.Add(x, 0, d, v);
	for (int y = x; p[y]; y = p[y]) {
		int t = dis(x, p[y]);
		T1.Add(p[y], 0, d - t, v);
		T2.Add(y, 0, d - t, v);
	}
}
int Query(int x) {
	int ret = T1.ask(x, 0);
	for (int y = x; p[y]; y = p[y]) {
		int t = dis(p[y], x);
		ret += T1.ask(p[y], t) - T2.ask(y, t);
	}
	return ret;
}
int main() {
	read(n), read(q); cnt = n;
	for (int i = 1; i < n; i++) {
		int x, y; read(x), read(y);
		add(x, y), add(y, x);
	}
	ff[rt = 0] = inf, now = n;
	getr(1, 0), work(rt, 0), dfs(1, 0);
	while (q--) {
		char c = getchar();
		while (!isalpha(c)) c = getchar();
		if (c == 'M') {
			int x, d, v;
			read(x), read(d), read(v);
			Modify(x, d, v);
		} else {
			int x; read(x);
			cout << Query(x) << "\n";
		}
	}
	return 0;
}
posted @ 2018-08-14 22:15  谜のNOIP  阅读(215)  评论(0编辑  收藏  举报