P2590 [ZJOI2008]树的统计 树剖+线段树


P2590 [ZJOI2008]树的统计


标签

  • 树链剖分
  • 线段树

简明题意

  • 啊啊啊,太简单了不讲了,3分钟水过啊啊啊啊

思路


注意事项

  • 不要写反了啊啊啊,我把dfs1中siz[u] += siz[v]写成了siz[v]+=siz[u],然后T了两个点…我初步分析,觉得,是因为链的划分是由子树决定的,这样后,链的划分就不合理,复杂度就高了…

总结


AC代码

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn = 3e4 + 10;

int n, a[maxn];
vector<int> g[maxn];

int fa[maxn], siz[maxn], son[maxn], dep[maxn];
void dfs1(int u, int f, int deep)
{
   fa[u] = f;
   dep[u] = deep;
   siz[u] = 1;

   int max_son = -1;
   for (auto& v : g[u])
   	if (v != f)
   	{
   		dfs1(v, u, deep + 1);
   		siz[u] += siz[v];
   		if (siz[v] > max_son)
   			max_son = siz[v], son[u] = v;
   	}
}

int top[maxn], id[maxn], cnt, w[maxn];
void dfs2(int u, int topf)
{
   id[u] = ++cnt;
   w[cnt] = a[u];
   top[u] = topf;

   if (son[u])
   {
   	dfs2(son[u], topf);
   	for (auto& v : g[u])
   		if (v != fa[u] && v != son[u])
   			dfs2(v, v);
   }
}

struct Node
{
   int l, r, max, sum;
};

Node tree[maxn * 4];

void update(int o)
{
   if (tree[o].l != tree[o].r)
   	(tree[o].sum = tree[o * 2].sum + tree[o * 2 + 1].sum), tree[o].max = max(tree[o * 2].max, tree[o * 2 + 1].max);
}

void build(int o, int l, int r)
{
   tree[o].l = l, tree[o].r = r;
   if (l == r)
   {
   	tree[o].max = tree[o].sum = w[l];
   	return;
   }

   int mid = (l + r) / 2;
   build(o * 2, l, mid);
   build(o * 2 + 1, mid + 1, r);

   update(o);
}

void change(int o, int x, int c)
{
   if (tree[o].l == tree[o].r)
   {
   	tree[o].max = tree[o].sum = c;
   	return;
   }

   int mid = (tree[o].l + tree[o].r) / 2;
   if (x <= mid)
   	change(o * 2, x, c);
   else
   	change(o * 2 + 1, x, c);

   update(o);
}

int ask_max(int o, int l, int r)
{
   if (tree[o].l == l && tree[o].r == r)
   	return tree[o].max;

   int mid = (tree[o].l + tree[o].r) / 2;
   if (r <= mid)
   	return ask_max(o * 2, l, r);
   else if (l > mid)
   	return ask_max(o * 2 + 1, l, r);
   else
   	return max(ask_max(o * 2, l, mid), ask_max(o * 2 + 1, mid + 1, r));
}

int ask_sum(int o, int l, int r)
{
   if (tree[o].l == l && tree[o].r == r)
   	return tree[o].sum;

   int mid = (tree[o].l + tree[o].r) / 2;
   if (r <= mid)
   	return ask_sum(o * 2, l, r);
   else if (l > mid)
   	return ask_sum(o * 2 + 1, l, r);
   else
   	return ask_sum(o * 2, l, mid) + ask_sum(o * 2 + 1, mid + 1, r);
}

void solve()
{
   scanf("%d", &n);
   for (int i = 1; i < n; i++)
   {
   	int u, v;
   	scanf("%d%d", &u, &v);

   	g[u].push_back(v), g[v].push_back(u);
   }

   for (int i = 1; i <= n; i++)
   	scanf("%d", &a[i]);

   dfs1(1, 1, 1);
   dfs2(1, 1);

   build(1, 1, n);

   int m;
   scanf("%d", &m);
   while (m--)
   {
   	char cmd[20];
   	int u, v;
   	scanf("%s%d%d", cmd, &u, &v);

   	if (cmd[1] == 'H')
   	{
   		change(1, id[u], v);
   	}
   	else if (cmd[1] == 'S')
   	{
   		int ans = 0;
   		while (top[u] != top[v])
   		{
   			if (dep[top[v]] < dep[top[u]]) swap(u, v);

   			ans += ask_sum(1, id[top[v]], id[v]);

   			v = fa[top[v]];
   		}
   		printf("%d\n", ans + ask_sum(1, min(id[u], id[v]), max(id[u], id[v])));
   	}
   	else if (cmd[1] == 'M')
   	{
   		int ans = -1e9;
   		while (top[u] != top[v])
   		{
   			if (dep[top[v]] < dep[top[u]]) swap(u, v);

   			ans = max(ans, ask_max(1, id[top[v]], id[v]));

   			v = fa[top[v]];
   		}
   		printf("%d\n", max(ans, ask_max(1, min(id[u], id[v]), max(id[u], id[v]))));
   	}
   }
}

int main()
{
   freopen("Testin.txt", "r", stdin);
   solve();
   return 0;
}

双倍经验

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值