欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2020 CCPC Wannafly Winter Camp Day1 E- 树与路径

程序员文章站 2022-07-12 15:06:54
...

通过观察可以发现,当以u为根节点的时候,向他的子节点转移x次,此时差值固定,即等差数列。
我们就可以通过树链剖分,给每一条链上,维护线段树,保存等差数列。
最后再通过求前缀和的方式求得。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#pragma GCC optimize(2)
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
	char ch = getchar(); ll x = 0, f = 1;
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 5e5 + 10;
struct node { int to, next; }edge[2*N];
int head[2*N];
int id[N], rk[N], son[N], fa[N], top[N], dep[N], siz[N];
int cnt = 0;
int n, m;
void addedge(int u, int v)
{
	edge[cnt].to = v;
	edge[cnt].next = head[u];
	head[u] = cnt++;
}
void dfs1(int u, int f, int d)
{
	fa[u] = f; dep[u] = d; siz[u] = 1;
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (v == f)continue;
		dfs1(v, u, d + 1);
		siz[u] += siz[v];
		if (son[u] == -1 || siz[son[u]] < siz[v])
		{
			son[u] = v;
		}
	}
}
int num = 0;
void dfs2(int u, int tp)
{
	top[u] = tp;
	id[u] = ++num;
	rk[num] = u;
	if (son[u] == -1)return;
	dfs2(son[u], tp);
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (v == fa[u]||v==son[u])continue;
		dfs2(v, v);
	}
}
struct  {
	ll st;
	ll d;
}lazy[N<<2];
void pushdown(int root,int l,int r)
{
		int mid = (l + r) >> 1;
		lazy[root << 1].d += lazy[root].d;
		lazy[root << 1 | 1].d += lazy[root].d;
		lazy[root << 1].st += lazy[root].st;
		lazy[root << 1 | 1].st += lazy[root].st - 1ll * 2 * (mid - l + 1)*lazy[root].d;
		lazy[root].d = lazy[root].st = 0;
	
}
void update(int l, int r, int root, int lf, int rt, ll st, ll d)
{
	if (lf <= l && r <= rt)
	{
		lazy[root].st += st;
		lazy[root].d += d;
		return;
	}
	pushdown(root, l, r);
	int mid = (l + r) >> 1;
	if (lf <= mid)update(lson, lf, rt, st, d);
	if (rt > mid)
	{
		if (lf <= mid)update(rson, lf, rt, st - 1ll * 2 * (mid - max(lf, l) + 1)*d, d);
		else update(rson, lf, rt, st, d);
	}
}
int lca(int u, int v)
{
	while (top[u] != top[v])
	{
		if (dep[top[u]] < dep[top[v]])swap(u, v);
		u = fa[top[u]];
	}
	if (dep[u] < dep[v])swap(u, v);
	return v;
}
ll ans[N];
void modify(int x, int y)
{
	int tp = lca(x, y);
	ll depx = dep[x] - dep[tp];
	ll depy = dep[y] - dep[tp];
	ll tempx = 0;
	ll tempy = 0;
	while (top[x] != top[tp])
	{
		tempx = dep[tp] - dep[top[x]] + 1;
		update(1, n, 1, id[top[x]], id[x], depx - depy - 1 + 2 * tempx, 1);
		x = fa[top[x]];
	}
	if (x != tp)
		update(1, n, 1, id[tp] + 1, id[x], depx - depy - 1, 1);
	while (top[y] != top[tp])
	{
		tempy = dep[tp] - dep[top[y]] + 1;
		update(1, n, 1, id[top[y]], id[y], depy - depx - 1 + 2 * tempy, 1);
		y = fa[top[y]];
	}
	if(y!=tp)
		update(1, n, 1, id[tp] + 1, id[y], depy - depx - 1, 1);
	ans[1] += depx * depy;
}
void rebuild(int l, int r, int root)
{
	if (l == r) {
		if (l == 1)return;
		//cout << "rkl" << rk[l] << endl;
		ans[rk[l]] = lazy[root].st;
		//cout << ans[rk[l]] << endl;
		return;
	}
	pushdown(root, l, r);
	int mid = (l + r) >> 1;
	rebuild(lson);
	rebuild(rson);
}
void dfs3(int u, int f)
{
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (v == f)continue;
		ans[v] += ans[u];
		dfs3(v, u);
	}
}
int main()
{
	memset(head, -1, sizeof(head));
	memset(son, -1, sizeof(son));
	n = read(), m = read();
	int u, v;
	up(i, 0, n - 1)
	{
		u = read(), v = read();
		addedge(u, v);
		addedge(v, u);
		/*addedge(i + 1, i + 2);
		addedge(i + 2, i + 1);*/
	}
	dfs1(1, 1, 1);
	dfs2(1, 1);
	upd(i, 0, m - 1)
	{
		u = read(), v = read();
		modify(u, v);
	}
	rebuild(1, n, 1);
	dfs3(1, 0);
	upd(i, 1, n)
	{
		printf("%lld\n", ans[i]);
	}
	return 0;
}