「JLOI2014」松鼠的新家

「JLOI2014」松鼠的新家

传送门

两种做法:

  • 树上差分 \(O(n)\)
  • 树链剖分 \(O(nlogn)\)

树剖比较好写而且无脑,树上差分复杂度优秀一些但是会有点难调。
这里给出树剖写法:
唯一要讲的就是记得每次都把路径终点的贡献 \(-1\)

参考代码:

/*--------------------------------
  Code name: E.cpp
  Author: The Ace Bee
  This code is made by The Ace Bee
--------------------------------*/
#include <cstdio>
#define rg register
#define file(x)									\
	freopen(x".in", "r", stdin);				\
	freopen(x".out", "w", stdout);
const int $ = 500010;
inline void swap(int& a, int& b) { int t = a; a = b; b = t; }
inline int read() {
	int s = 0; bool f = false; char c = getchar();
	while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
	while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
	return f ? -s : s;
}
int n, a[$];
int tot, head[$], nxt[$ << 1], ver[$ << 1];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }
int siz[$], son[$], dep[$];
int top[$], rev[$], seg[$], father[$];
int sum[$ << 2], tag[$ << 2];
inline int lc(int rt) { return rt << 1; }
inline int rc(int rt) { return rt << 1 | 1; }
inline void pushup(int rt) {
	sum[rt] = sum[lc(rt)] + sum[rc(rt)];
}
inline void f(int rt, int l, int r, int v) {
	tag[rt] += v, sum[rt] += v * (r - l + 1);
}
inline void pushdown(int rt, int l, int r, int mid) {
	if (tag[rt]) f(lc(rt), l, mid, tag[rt]), f(rc(rt), mid + 1, r, tag[rt]), tag[rt] = 0;
}
inline void update(int rt, int l, int r, int x, int y, int v) {
	if (x <= l && r <= y) return f(rt, l, r, v);
	int mid = (l + r) >> 1;
	pushdown(rt, l, r, mid);
	if (x <= mid) update(lc(rt), l, mid, x, y, v);
	if (y > mid) update(rc(rt), mid + 1, r, x, y, v);
	pushup(rt);
}
inline int query(int rt, int l, int r, int id) {
	if (l == r) return sum[rt];
	int mid = (l + r) >> 1, res;
	pushdown(rt, l, r, mid);
	if (id <= mid) res = query(lc(rt), l, mid, id);
	else res = query(rc(rt), mid + 1, r, id);
	return res;
}
inline void dfs1(int u, int fa) {
	siz[u] = 1, father[u] = fa, dep[u] = dep[fa] + 1;
	for (rg int v, i = head[u]; i; i = nxt[i])
		if (!dep[v = ver[i]]) {
			dfs1(v, u), siz[u] += siz[v];
			if (siz[v] > siz[son[u]]) son[u] = v;
		}
}
inline void dfs2(int u, int topf) {
	top[rev[seg[u] = ++seg[0]] = u] = topf;
	if (!son[u]) return; dfs2(son[u], topf);
	for (rg int v, i = head[u]; i; i = nxt[i])
		if (!top[v = ver[i]]) dfs2(v, v);
}
inline void uptRange(int x, int y, int v) {
	int fx = top[x], fy = top[y];
	while (fx != fy) {
		if (dep[fx] < dep[fy]) swap(x, y), swap(fx, fy);
		update(1, 1, n, seg[fx], seg[x], v);
		x = father[fx], fx = top[x];
	}
	if (dep[x] > dep[y]) swap(x, y);
	update(1, 1, n, seg[x], seg[y], v);
}
int main() {
//	file("E");
	n = read();
	for (rg int i = 1; i <= n; ++i) a[i] = read();
	for (rg int u, v, i = 1; i <= n - 1; ++i)
		u = read(), v = read(), Add_edge(u, v), Add_edge(v, u);
	dfs1(1, 0), dfs2(1, 1);
	for (rg int i = 1; i < n; ++i)
		uptRange(a[i], a[i + 1], 1), uptRange(a[i + 1], a[i + 1], -1);
	for (rg int i = 1; i <= n; ++i)
		printf("%d\n", query(1, 1, n, seg[i]));
	return 0;
}
posted @ 2020-01-31 21:31  Sangber  阅读(106)  评论(0编辑  收藏  举报