洛谷P4315 月下“毛景树” 树剖 +边权化点权+线段树+多重trag


洛谷P4315 月下“毛景树”


标签

  • 树剖
  • 边权化点权
  • 线段树
  • 多重rag

简明题意

  • 给一棵树及边权,需要支持4种操作:
    1. 将第i条边权改为w
    2. u–v路径边权全部改为w
    3. u–v路径边权全部增加w
    4. 查询u–v路径的最大边权

思路

  • 首先边权化点权不必说了。
  • 难点在于区间操作有两种,一个是区间覆盖, 一个是区间增加。这里我的处理方法是给每个节点写两个tag,一个记录覆盖,一个记录增加。然后这题就很简单了

注意事项

  • 注意LCA处的处理

总结


AC代码

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

const int maxn = 1e5 + 10;

struct Edge
{
   int v, w;
   Edge(int v, int w) : v(v), w(w) {}
};

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

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

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

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

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

struct Node
{
   int l, r, max;
   int tag1, tag2;
};

Node tree[maxn * 4];

void spread(int o)
{
   if (tree[o].tag1)
   {
   	tree[o].max = tree[o].tag1 + tree[o].tag2;
   	if (tree[o].l != tree[o].r)
   	{
   		tree[o * 2].tag1 = tree[o * 2 + 1].tag1 = tree[o].tag1;
   		tree[o * 2].tag2 = tree[o * 2 + 1].tag2 = tree[o].tag2;
   	}
   	tree[o].tag1 = 0, tree[o].tag2 = 0;
   }
   else if (tree[o].tag2)
   {
   	tree[o].max += tree[o].tag2;
   	if (tree[o].l != tree[o].r)
   		tree[o * 2].tag2 += tree[o].tag2, tree[o * 2 + 1].tag2 += tree[o].tag2;
   	tree[o].tag2 = 0;
   }
}

void update(int o)
{
   if (tree[o].l != tree[o].r)
   {
   	spread(o * 2), spread(o * 2 + 1);
   	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 = 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 l, int r, int c, bool type)
{
   spread(o);

   if (tree[o].l == l && tree[o].r == r)
   {
   	if (type == 1)
   		tree[o].tag1 = c, tree[o].tag2 = 0;
   	else if (type == 0)
   		tree[o].tag2 += c;
   	return;
   }

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

   update(o);
}

int ask(int o, int l, int r)
{
   spread(o);

   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(o * 2, l, r);
   else if (l > mid)
   	return ask(o * 2 + 1, l, r);
   return max(ask(o * 2, l, mid), ask(o * 2 + 1, mid + 1, r));
}

pair<int, int> rec[maxn];

void solve()
{
   scanf("%d", &n);
   for (int i = 1; i < n; i++)
   {
   	int u, v, w;
   	scanf("%d%d%d", &u, &v, &w);
   	rec[i].first = u, rec[i].second = v;

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

   dfs1(1, 1, 1, 0);
   dfs2(1, 1);
   build(1, 1, n);

   char cmd[20];
   while (scanf("%s", cmd))
   {
   	if (cmd[0] == 'S') return;

   	if (cmd[0] == 'M')
   	{
   		int u, v;
   		scanf("%d%d", &u, &v);

   		int ans = -1e9;
   		while (top[u] != top[v])
   		{
   			if (dep[top[v]] < dep[top[u]]) swap(u, v);
   			ans = max(ans, ask(1, id[top[v]], id[v]));
   			v = fa[top[v]];
   		}
   		if (id[u] > id[v]) swap(u, v);
   		if (u != v)
   			ans = max(ans, ask(1, id[u] + 1, id[v]));
   		printf("%d\n", ans);
   	}
   	else if (cmd[0] == 'A')
   	{
   		int u, v, w;
   		scanf("%d%d%d", &u, &v, &w);

   		while (top[u] != top[v])
   		{
   			if (dep[top[v]] < dep[top[u]]) swap(u, v);
   			change(1, id[top[v]], id[v], w, 0);
   			v = fa[top[v]];
   		}
   		if (id[u] > id[v]) swap(u, v);
   		if (u != v)
   			change(1, id[u] + 1, id[v], w, 0);
   	}
   	else if (cmd[0] == 'C')
   	{
   		if (cmd[1] == 'h')
   		{
   			int x, w;
   			scanf("%d%d", &x, &w);
   			int u = dep[rec[x].first] > dep[rec[x].second] ? rec[x].first : rec[x].second;
   			change(1, id[u], id[u], w, 1);
   		}
   		else if (cmd[1] == 'o')
   		{
   			int u, v, w;
   			scanf("%d%d%d", &u, &v, &w);

   			while (top[u] != top[v])
   			{
   				if (dep[top[v]] < dep[top[u]]) swap(u, v);
   				change(1, id[top[v]], id[v], w, 1);
   				v = fa[top[v]];
   			}
   			if (id[u] > id[v]) swap(u, v);
   			if (u != v)
   				change(1, id[u] + 1, id[v], w, 1);
   		}
   	}
   }
}

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

双倍经验

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值