【洛谷P4556】 雨天的尾巴
题面
题解
线段树合并
我们看到这道题目首先可以想到树上差分,然后\(dfs\)合并
发现题目让我们求的东西很好用线段树维护
于是可以想到线段树合并
全世界只有我写指针版动态开点线段树(大雾
如果你要写指针版,请开内存池,new又耗时又浪费空间
代码
#include<cstdio>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
inline int read()
{
int data=0, w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1, ch=getchar();
while(ch>='0'&&ch<='9') data=data*10+(ch^48), ch=getchar();
return data*w;
}
const int maxn(100010);
struct node { node *son[2]; int max, id; } *root[maxn], pool[maxn * 50], *pos;
struct edge { int next, to; } e[maxn << 1];
struct query { int next, to, id; } q[maxn << 1];
struct answer { int a, b, v, lca; } ans[maxn];
bool vis[maxn];
int head[maxn], e_num, fa[maxn], n, m, s, qhead[maxn], q_num, F[maxn], Ans[maxn];
inline void add_edge(int from, int to) { e[++e_num] = (edge) {head[from], to}; head[from] = e_num; }
inline void add_query(int from, int to, int id) { q[++q_num] = (query) {qhead[from], to, id}; qhead[from] = q_num; }
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void dfs(int x)
{
vis[x] = true;
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(to == F[x]) continue;
F[to] = x; dfs(to); fa[find(to)] = find(x);
}
for(RG int i = qhead[x]; i; i = q[i].next)
{
int to = q[i].to; if(!vis[to]) continue;
ans[q[i].id].lca = find(to);
}
}
inline int Max(node *x) { return x ? x -> max : 0; }
inline int Id(node *x) { return x ? x -> id : 0; }
inline void pushup(node *x)
{
if(Max(x -> son[0]) >= Max(x -> son[1])) x -> max = Max(x -> son[0]), x -> id = Id(x -> son[0]);
else x -> max = Max(x -> son[1]), x -> id = Id(x -> son[1]);
if(!x -> max) x -> id = 0;
}
inline void Insert(node *&x, int pos, int val, int l = 1, int r = maxn - 10)
{
if(!x) x = ::pos++; if(l == r) { x -> max += val; x -> id = l; if(!x -> max) x -> id = 0; return; }
int mid = (l + r) >> 1;
if(pos <= mid) Insert(x -> son[0], pos, val, l, mid);
else Insert(x -> son[1], pos, val, mid + 1, r);
pushup(x); if(!x -> max) x -> id = 0;
}
inline node *Merge(node *x, node *&y, int l = 1, int r = maxn - 10)
{
if(!x) return y; if(!y) return x;
int mid = (l + r) >> 1; if(l == r) { x -> max += y -> max; x -> id = l; return x; }
x -> son[0] = Merge(x -> son[0], y -> son[0], l, mid);
x -> son[1] = Merge(x -> son[1], y -> son[1], mid + 1, r);
pushup(x); return x;
}
void solve(int x)
{
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(to == F[x]) continue;
solve(to); root[x] = Merge(root[x], root[to]);
}
Ans[x] = Id(root[x]);
}
int main()
{
#ifndef ONLINE_JUDGE
file(cpp);
#endif
pos = pool; n = read(); m = read();
for(RG int i = 1, a, b; i < n; i++) a = read(), b = read(), add_edge(a, b), add_edge(b, a);
for(RG int i = 1; i <= n; i++) fa[i] = i;
for(RG int i = 1, a, b, c; i <= m; i++)
a = read(), b = read(), c = read(), ans[i] = (answer) {a, b, c, 0}, add_query(a, b, i), add_query(b, a, i);
dfs(1);
for(RG int i = 1; i <= m; i++)
Insert(root[ans[i].a], ans[i].v, 1), Insert(root[ans[i].b], ans[i].v, 1), Insert(root[ans[i].lca], ans[i].v, -1),
Insert(root[F[ans[i].lca]], ans[i].v, -1);
solve(1); for(RG int i = 1; i <= n; i++) printf("%d\n", Ans[i]);
return 0;
}
【洛谷P4556】 雨天的尾巴的更多相关文章
- 洛谷P4556 雨天的尾巴(线段树合并)
洛谷P4556 雨天的尾巴 题目链接 题解: 因为一个点可能存放多种物品,直接开二维数组进行统计时间.空间复杂度都不能承受.因为每一个点所拥有的物品只与其子树中的点有关,所以可以考虑对每一个点来建立一 ...
- [洛谷P4556] 雨天的尾巴
这道题可以用线段树合并做,网上的题解基本上都是线段树合并的. 但是为什么我就偏偏要用dsu on tree...... 题目传送门 dsu on tree的方法类似[CF1009F] Dominant ...
- 洛谷P4556 雨天的尾巴 线段树
正解:线段树合并 解题报告: 传送门! 考虑对树上的每个节点开一棵权值线段树,动态开点,记录一个max(num,id)(这儿的id,define了一下,,,指的是从小到大排QAQ 然后修改操作可以考虑 ...
- [洛谷 P4556] 雨天的尾巴
传送门 Solution 线段树合并的入门题 lca可以在dfs的时候离线求(用并查集) 更新的点有每条链的两个端点,它们的lca和dad[lca] 为了节省空间,lca和dad[lca]的更新可以先 ...
- 洛谷 P4556 [Vani有约会]雨天的尾巴 解题报告
P4556 [Vani有约会]雨天的尾巴 题目背景 深绘里一直很讨厌雨天. 灼热的天气穿透了前半个夏天,后来一场大雨和随之而来的洪水,浇灭了一切. 虽然深绘里家乡的小村落对洪水有着顽固的抵抗力,但也倒 ...
- 洛谷P4556 [Vani有约会]雨天的尾巴(线段树合并)
题目背景 深绘里一直很讨厌雨天. 灼热的天气穿透了前半个夏天,后来一场大雨和随之而来的洪水,浇灭了一切. 虽然深绘里家乡的小村落对洪水有着顽固的抵抗力,但也倒了几座老房子,几棵老树被连根拔起,以及田地 ...
- 2018.08.28 洛谷P4556 [Vani有约会]雨天的尾巴(树上差分+线段树合并)
传送门 要求维护每个点上出现次数最多的颜色. 对于每次修改,我们用树上差分的思想,然后线段树合并统计答案就行了. 注意颜色很大需要离散化. 代码: #include<bits/stdc++.h& ...
- [洛谷P4556][BZOJ3307]雨天的尾巴-T3订正
线段树合并+树上差分 题目链接(···) 「简单」「一般」——其实「一般」也只多一个离散化 考试时想法看>这里< 总思路:先存所有的操作,离散化,然后用树上差分解决修改,用权值线段树维护每 ...
- P4556 雨天的尾巴 线段树合并
使用线段树合并,每个节点维护一棵权值线段树,下标为救济粮种类,区间维护数量最多的救济粮编号(下标).所以每个节点答案即为\(tre[rot[x]]\). 然后运用树上点的差分思想,对于分发路径\(u, ...
随机推荐
- Django 发送html邮件
转载于: http://blog.sina.com.cn/s/blog_76e94d2101011bxd.html django中发送html邮件: #mailer.py # -*- co ...
- MyBatis(3)-映射文件
本次博文有疑问,请先看MyBatis(1)-简单入门 和 MyBatis(2)-全局配置文件! 如在有疑问,请留言或者咨询博主,博主每天都在!谢谢! 映射文件: 主要是在xxxmapper.xml文件 ...
- 2019.2.27 Eclipse中的Tomcat设置Tomcat服务器手动重启
1.打开Tomcat的设置界面 2.找到Modules界面 3.去掉,就改为手动了
- ascll方便查询
- webpack4配置
一.安装webpack 需要先在项目中npm init初始化一下,生成package.json 建议node版本安装到8.2以上 // webpack4中除了正常安装webpack之外,需要再单独安一 ...
- [Python]json对象转换出错expected string or buffer python
[问题] 今天在使用python中的json转换碰到一个问题: 错误显示: expected string or buffer json内容如下: {u'err_no': 0, u'corpus_no ...
- [转]谈谈关于MVP模式中V-P交互问题
在差不多两年的时间内,我们项目组几十来号人都扑在一个项目上面.这是一个基于微软SCSF(Smart Client Software Factory)的项目,客户端是墨尔本一家事业单位.前两周,我奉命负 ...
- XMPP备忘笔记
xmpp中文翻译计划: http://wiki.jabbercn.org/%E9%A6%96%E9%A1%B5 XEP-0004: 数据表单 摘要: 本文定义了一个XMPP扩展协议用于数据表单,可以用 ...
- SpringMVC找不到对应的页面
确认springmvc配置文件视图解析器配置正确. <!-- 视图解析器 --> <bean class="org.springframework.web.servlet. ...
- 处理HTML表单(11)
PHP和Web表单 <?php if(isset($_POST["name"])){//isset()函数设置变量是否设置,并且不能为空 $name = $_POST[&qu ...