【线段树合并/树上差分】[P4556 [Vani有约会] 雨天的尾巴 /【模板】线段树合并
【线段树合并/树上差分】P4556 [Vani有约会] 雨天的尾巴 /【模板】线段树合并
思路
对 \(x,y,lca(u,v),fa_{lca(u,v)}\) 四个点进行树上差分,然后用线段树合并动态权值线段树。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template<class Node>
struct PersidentSegmentTree {
#define lc(u) tr[u].l
#define rc(u) tr[u].r
const int n;
int tot = 0;
vector<Node> tr;
vector<int> root;
PersidentSegmentTree(): n(0) {}
PersidentSegmentTree(int n_): n(n_) {
int N = (n << 6) + 10;
tr.reserve(N); root.reserve(N);
tr.resize(N); root.resize(N);
}
PersidentSegmentTree(vector<int>& a): PersidentSegmentTree(a.size() - 1) {
function<void(int&, int, int)> build = [&](int& now, int l, int r) {
now = ++ tot;
if (l == r) {
return ;
}
int m = (l + r) >> 1;
build(lc(now), l, m);
build(rc(now), m + 1, r);
};
build(root[0], 1, n);
}
//上传
void pushup(int u) {
if (tr[lc(u)].Sum >= tr[rc(u)].Sum) {
tr[u].Sum = tr[lc(u)].Sum;
tr[u].pos = tr[lc(u)].pos;
} else {
tr[u].Sum = tr[rc(u)].Sum;
tr[u].pos = tr[rc(u)].pos;
}
}
//动态开点/更新树结点
void insert(int& now, int l, int r, int pos, int w) {
if (!now)
now = ++ tot;
if (l == r) {
tr[now].Sum += w;
tr[now].pos = pos;
return;
}
int m = l + r >> 1;
if (pos <= m)
insert(lc(now), l, m, pos, w);
else
insert(rc(now), m + 1, r, pos, w);
pushup(now);
}
//开新前缀树,last代表前缀,now代表新树根
void insert(int& now, int last, int l, int r, int pos, int w) {
now = ++ tot;
tr[now] = tr[last];
if (l == r) {
return;
}
int m = l + r >> 1;
if (pos <= m)
insert(lc(now), lc(last), l, m, pos, w);
else
insert(rc(now), rc(last), m + 1, r, pos, w);
}
//单点
int query(int now, int l, int r, int pos) {
if (l == r) {
return tr[now].Sum;
}
int m = l + r >> 1;
if (pos <= m)
return query(lc(now), l, m, pos);
else
return query(rc(now), m + 1, r, pos);
}
//区间查询 [u,v]->[root[l-1],root[r]]
int query(int u, int v, int l, int r, int pos) {
if (l == r) {
return tr[u].Sum;
}
int m = l + r >> 1;
if (pos <= m)
return query(lc(u), lc(v), l, m, pos);
else
return query(rc(u), rc(v), m + 1, r, pos);
}
void merge(int &u, int v, int l, int r) {
if (!u || !v) {
u = u + v;
return;
}
if (l == r) {
tr[u].Sum += tr[v].Sum;
return ;
}
int m = l + r >> 1;
merge(lc(u), lc(v), l, m);
merge(rc(u), rc(v), m + 1, r);
pushup(u);
}
};
struct Node {
int l, r;
int Sum = 0, pos = 0;
};
constexpr int N = 1e5 + 10, M = N - 10, logn = 20;
PersidentSegmentTree<Node> pst(N);
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector g(n + 1, vector<int>());
for (int i = 1; i < n; i ++) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
vector dep(n + 1, 0);
vector f(n + 1, vector<int>(logn + 1));
auto dfs = [&](auto && self, int u, int fa)->void{
f[u][0] = fa;
dep[u] = dep[fa] + 1;
for (int i = 1; i <= logn; i ++) {
f[u][i] = f[f[u][i - 1]][i - 1];
}
for (auto v : g[u]) {
if (v == fa)continue;
self(self, v, u);
}
};
dfs(dfs, 1, 0);
auto lca = [&](int u, int v)->int{
if (dep[u] < dep[v])
swap(u, v);
for (int i = logn; i >= 0 ; i --) {
if (dep[f[u][i]] >= dep[v]) {
u = f[u][i];
}
}
if (u == v)
return u;
for (int i = logn; i >= 0; i --) {
if (f[u][i] != f[v][i]) {
u = f[u][i], v = f[v][i];
}
}
return f[u][0];
};
while (m --) {
int x, y, z;
cin >> x >> y >> z;
int k = lca(x, y);
pst.insert(pst.root[x], 1, M, z, 1);
pst.insert(pst.root[y], 1, M, z, 1);
pst.insert(pst.root[k], 1, M, z, -1);
pst.insert(pst.root[f[k][0]], 1, M, z, -1);
}
vector<int> ans(n + 1);
auto calc = [&](auto && self, int u, int fa)->void{
for (auto v : g[u]) {
if (v == fa) continue;
self(self, v, u);
pst.merge(pst.root[u], pst.root[v], 1, M);
}
ans[u] = pst.tr[pst.root[u]].pos;
if (!pst.tr[pst.root[u]].Sum) ans[u] = 0;
};
calc(calc, 1, 0);
for (int i = 1; i <= n; i ++)
cout << ans[i] << "\n";
return 0;
}
【线段树合并/树上差分】[P4556 [Vani有约会] 雨天的尾巴 /【模板】线段树合并的更多相关文章
- P4556 [Vani有约会]雨天的尾巴(线段树合并+lca)
P4556 [Vani有约会]雨天的尾巴 每个操作拆成4个进行树上差分,动态开点线段树维护每个点的操作. 离线处理完向上合并就好了 luogu倍增lca被卡了5分.....于是用rmq维护.... 常 ...
- P4556 [Vani有约会]雨天的尾巴 (线段树合并)
P4556 [Vani有约会]雨天的尾巴 题意: 首先村落里的一共有n座房屋,并形成一个树状结构.然后救济粮分m次发放,每次选择两个房屋(x,y),然后对于x到y的路径上(含x和y)每座房子里发放一袋 ...
- P4556 [Vani有约会]雨天的尾巴(线段树合并)
传送门 一道线段树合并 首先不难看出树上差分 我们把每一次修改拆成四个,在\(u,v\)分别放上一个,在\(lca\)和\(fa[lca]\)各减去一个,那么只要统计一下子树里的总数即可 然而问题就在 ...
- [题解] P4556 [Vani有约会]雨天的尾巴
[题解] P4556 [Vani有约会]雨天的尾巴 ·题目大意 给定一棵树,有m次修改操作,每次修改 \(( x\) \(y\) \(z )\) 表示 \((x,y)\) 之间的路径上数值 \(z\) ...
- 洛谷 P4556 [Vani有约会]雨天的尾巴 解题报告
P4556 [Vani有约会]雨天的尾巴 题目背景 深绘里一直很讨厌雨天. 灼热的天气穿透了前半个夏天,后来一场大雨和随之而来的洪水,浇灭了一切. 虽然深绘里家乡的小村落对洪水有着顽固的抵抗力,但也倒 ...
- 2018.08.28 洛谷P4556 [Vani有约会]雨天的尾巴(树上差分+线段树合并)
传送门 要求维护每个点上出现次数最多的颜色. 对于每次修改,我们用树上差分的思想,然后线段树合并统计答案就行了. 注意颜色很大需要离散化. 代码: #include<bits/stdc++.h& ...
- P4556 [Vani有约会]雨天的尾巴 /【模板】线段树合并 (树上差分+线段树合并)
显然的树上差分问题,最后要我们求每个点数量最多的物品,考虑对每个点建议线段树,查询子树时将线段树合并可以得到答案. 用动态开点的方式建立线段树,注意离散化. 1 #include<bits/st ...
- P4556 [Vani有约会]雨天的尾巴
目录 思路 优化 过程中的问题/疑问 错误 代码 思路 每个节点维护一课线段树(当然是动态开点) 线段树的作用是统计这个节点有多少种粮食型号,以及最多的粮食型号 然后树上差分,u和v点 +1,lca( ...
- 洛咕 P4556 [Vani有约会]雨天的尾巴
终于把考试题清完了...又复活了... 树上差分,合并用线段树合并,但是空间会炸. 某大佬:lca和fa[lca]减得时候一定已经存在这个节点了,所以放进vector里,合并完之后减掉就好了... 玄 ...
- 洛谷P4556 [Vani有约会]雨天的尾巴(线段树合并)
题目背景 深绘里一直很讨厌雨天. 灼热的天气穿透了前半个夏天,后来一场大雨和随之而来的洪水,浇灭了一切. 虽然深绘里家乡的小村落对洪水有着顽固的抵抗力,但也倒了几座老房子,几棵老树被连根拔起,以及田地 ...
随机推荐
- Python优雅遍历字典删除元素的方法
在Python中,直接遍历字典并在遍历过程中删除元素可能会导致运行时错误,因为字典在迭代时并不支持修改其大小.但是,我们可以通过一些方法间接地达到这个目的. 1.方法一:字典推导式创建新字典(推荐) ...
- 10-Python进程与线程
Python进程 创建新进程 from multiprocessing import Process import time def run_proc(name): #子进程要执行的代码 for i ...
- 为什么不推荐使用Linq?
相信很多.NETer看了标题,都会忍不住好奇,点进来看看,并且顺便准备要喷作者! 这里,首先要申明一下,作者本人也非常喜欢Linq,也在各个项目中常用Linq. 我爱Linq,Linq优雅万岁!!!( ...
- Freertos学习:04-任务的调试函数
--- title: rtos-freertos-04-任务的调试函数 EntryName: rtos-freertos-04-task-debug date: 2020-06-22 08:49:06 ...
- 你要的AI Agent工具都在这里
只有让LLM(大模型)学会使用工具,才能做出一系列实用的AI Agent,才能发挥出LLM真正的实力.本篇,我们让AI Agent使用更多的工具,比如:外部搜索.分析CSV.文生图.执行代码等. 1. ...
- tcp_tw_reuse、tcp_tw_recycle、tcp_fin_timeout参数介绍
参数介绍 net.ipv4.tcp_tw_reuse = 1 表示开启重用.允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭: net.ipv4.tcp_tw_rec ...
- 使用Stream流实现以List<Map<String, Object>>集合中Map的key值进行排序
使用Stream流实现以List<Map<String, Object>>集合中Map的key值进行排序 创建一个list存入数据 List<Map<String, ...
- 新知识get,vue3是如何实现在style中使用响应式变量?
前言 vue2的时候想必大家有遇到需要在style模块中访问script模块中的响应式变量,为此我们不得不使用css变量去实现.现在vue3已经内置了这个功能啦,可以在style中使用v-bind指令 ...
- 教你基于MindSpore用DCGAN生成漫画头像
本文分享自华为云社区<[昇思25天学习打卡营打卡指南-第二十天]DCGAN生成漫画头像>,作者:JeffDing. DCGAN生成漫画头像 在下面的教程中,我们将通过示例代码说明DCGAN ...
- 使用中台 Admin.Core 实现了一个Razor模板的通用代码生成器
前言 前面使用 Admin.Core 的代码生成器生成了通用代码生成器的基础模块 分组,模板,项目,项目模型,项目字段的基础功能,本篇继续完善,实现最核心的模板生成功能,并提供生成预览及代码文件压缩下 ...