题面

题解

线段树合并

我们看到这道题目首先可以想到树上差分,然后\(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】 雨天的尾巴的更多相关文章

  1. 洛谷P4556 雨天的尾巴(线段树合并)

    洛谷P4556 雨天的尾巴 题目链接 题解: 因为一个点可能存放多种物品,直接开二维数组进行统计时间.空间复杂度都不能承受.因为每一个点所拥有的物品只与其子树中的点有关,所以可以考虑对每一个点来建立一 ...

  2. [洛谷P4556] 雨天的尾巴

    这道题可以用线段树合并做,网上的题解基本上都是线段树合并的. 但是为什么我就偏偏要用dsu on tree...... 题目传送门 dsu on tree的方法类似[CF1009F] Dominant ...

  3. 洛谷P4556 雨天的尾巴 线段树

    正解:线段树合并 解题报告: 传送门! 考虑对树上的每个节点开一棵权值线段树,动态开点,记录一个max(num,id)(这儿的id,define了一下,,,指的是从小到大排QAQ 然后修改操作可以考虑 ...

  4. [洛谷 P4556] 雨天的尾巴

    传送门 Solution 线段树合并的入门题 lca可以在dfs的时候离线求(用并查集) 更新的点有每条链的两个端点,它们的lca和dad[lca] 为了节省空间,lca和dad[lca]的更新可以先 ...

  5. 洛谷 P4556 [Vani有约会]雨天的尾巴 解题报告

    P4556 [Vani有约会]雨天的尾巴 题目背景 深绘里一直很讨厌雨天. 灼热的天气穿透了前半个夏天,后来一场大雨和随之而来的洪水,浇灭了一切. 虽然深绘里家乡的小村落对洪水有着顽固的抵抗力,但也倒 ...

  6. 洛谷P4556 [Vani有约会]雨天的尾巴(线段树合并)

    题目背景 深绘里一直很讨厌雨天. 灼热的天气穿透了前半个夏天,后来一场大雨和随之而来的洪水,浇灭了一切. 虽然深绘里家乡的小村落对洪水有着顽固的抵抗力,但也倒了几座老房子,几棵老树被连根拔起,以及田地 ...

  7. 2018.08.28 洛谷P4556 [Vani有约会]雨天的尾巴(树上差分+线段树合并)

    传送门 要求维护每个点上出现次数最多的颜色. 对于每次修改,我们用树上差分的思想,然后线段树合并统计答案就行了. 注意颜色很大需要离散化. 代码: #include<bits/stdc++.h& ...

  8. [洛谷P4556][BZOJ3307]雨天的尾巴-T3订正

    线段树合并+树上差分 题目链接(···) 「简单」「一般」——其实「一般」也只多一个离散化 考试时想法看>这里< 总思路:先存所有的操作,离散化,然后用树上差分解决修改,用权值线段树维护每 ...

  9. P4556 雨天的尾巴 线段树合并

    使用线段树合并,每个节点维护一棵权值线段树,下标为救济粮种类,区间维护数量最多的救济粮编号(下标).所以每个节点答案即为\(tre[rot[x]]\). 然后运用树上点的差分思想,对于分发路径\(u, ...

随机推荐

  1. Guava包学习---Bimap

    Bimap也是Guava中提供的新集合类,别名叫做双向map,就是key->value,value->key,也就是你可以通过key定位value,也可以用value定位key. 这个场景 ...

  2. cocos2d-x中关于打包成APK的问题

    转载自:http://blog.csdn.net/u013315178/article/details/51254630 之前在网上看了很多的帖子大多数用ide 来打包 太麻烦了 而且一般没有人现场指 ...

  3. 贪心——HDU-5969 最大的位或

    HDU-5969:http://acm.hdu.edu.cn/showproblem.php?pid=5969 一开始也是分了类,觉得要两种情况,l 与 r 位数相同与不同的情况,仔细想一下,可以一起 ...

  4. Appium基础篇(一)——启动emulator

    1. Appium API文档:链接参考 http://appium.io/slate/cn/v/?ruby#appium-介绍. 2. Appium 安装篇:http://www.cnblogs.c ...

  5. array详解

    array和vector大致是相同的,区别在于array的大小是固定的.不能增加和缩小.另外array的swap()函数和vector的swap()函数在算法复杂度上是有区别的,array.swap( ...

  6. 如何在ubuntu中安装CP-ABE

    cpabe依赖pbc,pbc依赖gmp,gmp依赖M4.bison.flex,所以先安装后面的 安装m4 $   sudo apt-get install m4 安装 flex 安装 bison 1 ...

  7. CocoaPods安装指定版本

    Cocoapods目前最新的正式版本是0.35.0,如果升级到这个版本,并且在project中使用XMPPFramework,在pod install之后会出现如下循环依赖的问题 There is a ...

  8. HDU 1233 还是畅通工程(Kruskal算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)   ...

  9. .gitignore设置不生效

    .gitignore git中,如果想要让git忽略某些文件,或不想push到远程库,不让其受版本的控制.可以使用git提供的.gitignore文件进行配置.像这样: 一般情况下,在文件还未修改前, ...

  10. Oracle 存储结构一

    了解块中表行数据的存储 Oracle数据存储模型 逻辑结构在左,物理结构在右 有一个关系使用虚线绘制,表示段与数据文件的多对多关系.之所以使用虚线表示关系,是因为这种多对多关系不应存在. 表空间实体消 ...