【luogu P3258 [JLOI2014]松鼠的新家】 题解

题目链接:https://www.luogu.org/problemnew/show/P3258

谁说树剖过不去会RE呢?

我今天就是要强行树剖了

树剖强艹

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 500010;
ll ans[maxn], son[maxn], deep[maxn], size[maxn], fa[maxn], top[maxn], seg[maxn], num, n, m, path[maxn];
struct edge{
	ll to, next;
}e[maxn<<2];
ll head[maxn], cnt;
void add(ll u, ll v)
{
	e[++cnt].to = v; e[cnt].next = head[u]; head[u] = cnt;
	e[++cnt].to = u; e[cnt].next = head[v]; head[v] = cnt;
}
class Segment_Tree{
	public:
		#define lson l, mid, rt<<1
		#define rson mid+1, r, rt<<1|1
		ll tree[maxn<<2], lazy[maxn<<2];
		void build(ll l, ll r, ll rt)
		{
			if(l == r)
			{
				tree[rt] = 0;
				return;
			}
			ll mid = (l + r) >> 1;
			build(lson);
			build(rson);
			PushUP(rt);
		}
		void update(ll left, ll right, ll add, ll l, ll r, ll rt)
		{
			if(left <= l && right >= r)
			{
				lazy[rt] += add;
				tree[rt] += (r - l + 1) * add;
				return;
			}
			ll mid = (l + r) >> 1;
			PushDOWN(l, r, rt);
			if(left <= mid) update(left, right, add, lson);
			if(right > mid) update(left, right, add, rson);
			PushUP(rt);
		}
		ll query(ll left, ll right, ll l, ll r, ll rt)
		{
			ll res = 0;
			if(left <= l && right >= r)
			{
				return tree[rt];
			}
			ll mid = (l + r) >> 1;
			PushDOWN(l, r, rt);
			if(left <= mid) res += query(left, right, lson);
			if(right > mid) res += query(left, right, rson);
			return res;
		}
	private:
		void PushUP(ll rt)
		{
			tree[rt] = tree[rt<<1] + tree[rt<<1|1];
		}
		void PushDOWN(ll l, ll r, ll rt)
		{
			ll mid = (l + r) >> 1;
			if(lazy[rt])
			{
				lazy[rt<<1] += lazy[rt];
				lazy[rt<<1|1] += lazy[rt];
				tree[rt<<1] += (mid-l+1)*lazy[rt];
				tree[rt<<1|1] += (r-mid)*lazy[rt];
				lazy[rt] = 0;
			}
		}
}T;
void dfs1(ll u, ll f, ll d)
{
	ll maxson = -1;
	size[u] = 1;
	deep[u] = d;
	fa[u] = f;
	for(ll i = head[u]; i != -1; i = e[i].next)
	{
		ll v = e[i].to;
		if(f != v)
		{
			dfs1(v, u, d+1);
			size[u] += size[v];
			if(size[v] > maxson) son[u] = v, maxson = size[v];
		}
	}
}
void dfs2(ll u, ll t)
{
	seg[u] = ++num;
	top[u] = t;
	if(!son[u]) return;
	dfs2(son[u], t);
	for(ll i = head[u]; i != -1; i = e[i].next)
	{
		ll v = e[i].to;
		if(v == son[u] || v == fa[u]) continue;
		dfs2(v, v);
	}
}
ll LCA(ll x, ll y)
{
	while(top[x] != top[y])
	{
		if(deep[top[x]] < deep[top[y]]) swap(x, y);
		x = fa[top[x]];
	}
	return deep[x] < deep[y] ? x : y;
}
ll Qson(ll x)
{
	ll res = 0;
	res += T.query(seg[x], seg[x]+size[x]-1, 1, n, 1);
	return res;
}
void Updson(ll x, ll y)
{
	T.update(seg[x], seg[x]+size[x]-1, y, 1, n, 1);
}
ll Qrange(ll x, ll y)
{
	ll res = 0;
	while(top[x] != top[y])
	{
		if(deep[top[x]] < deep[top[y]]) swap(x, y);
		res += T.query(seg[top[x]], seg[x], 1, n, 1);
		x = fa[top[x]];
	}
	if(deep[x] < deep[y]) swap(x, y);
	res += T.query(seg[y], seg[x], 1, n, 1);
	return res;
}
void Updrange(ll x, ll y, ll z)
{
	while(top[x] != top[y])
	{
		if(deep[top[x]] < deep[top[y]]) swap(x, y);
		T.update(seg[top[x]], seg[x], z, 1, n, 1);
		x = fa[top[x]];
	}
	if(deep[x] < deep[y]) swap(x, y);
	T.update(seg[y], seg[x], z, 1, n, 1);
}
int main()
{
	memset(head, -1, sizeof(head));
	scanf("%lld%lld",&n,&path[1]);
	for(ll i = 2; i <= n; i++)
	{
		scanf("%lld",&path[i]);
		ans[path[i]]--;
	}
	for(ll i = 1; i <= n-1; i++)
	{
		ll u, v;
		scanf("%lld%lld",&u,&v);
		add(u, v);
	}
	dfs1(1, 0, 1); dfs2(1, 1);
	T.build(1, n, 1);
	for(ll i = 2; i <= n; i++)
	{
		Updrange(path[i], path[i-1], 1);
	}
	for(ll i = 1; i <= n; i++) ans[i] += Qrange(i, i);
	for(ll i = 1; i <= n; i++)
	{
		printf("%lld\n",ans[i]);
	}
	return 0;
}
posted @ 2018-10-30 15:54  Misaka_Azusa  阅读(86)  评论(0编辑  收藏
Live2D