题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional roads, such that there is exactly one path between any two pastures. Bessie, a cow who loves her grazing time, often complains about how there is no grass on t…
题解:仍然是无脑树剖,要注意一下边权,然而这种没有初始边权的题目其实和点权也没什么区别了 代码如下: #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #define lson root<<1 #define rson root<<1|1 using namespace std; struct…
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional roads, such that there is exactly one path between any two pastures. Bessie, a cow who loves her grazing time, often complains about how there is no grass on t…
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional roads, such that there is exactly one path between any two pastures. Bessie, a cow who loves her grazing time, often complains about how there is no grass on t…
模拟题,可以用树链剖分+线段树维护. 但是学了一个厉害的..树状数组的区间修改与区间查询.. 分割线里面的是转载的: -------------------------------------------------------------------------------- [ 3 ]  上面都不是重点……重点是树状数组的区间修改+区间查询 这个很好玩 其实也挺简单 首先依旧是引入delta数组 delta[i]表示区间 [i, n] 的共同增量 于是修改区间 [l, r] 时修改 delt…
图很丑.明显的树链剖分,需要的操作只有区间修改和区间查询.不过这里是边权,我们怎么把它转成点权呢?对于E(u,v),我们选其深度大的节点,把边权扔给它.因为这是树,所以每个点只有一个父亲,所以每个边权都可以唯一地.不重复地转移到点上去(除了根节点).但是做区间操作时就要注意一下,区间两边端点的LCA的权值是不可以用的. 这么简单的模板题就直接放代码了: #include<iostream> #include<cstring> #include<cstdio> #defi…
表示看不太清. 概括题意 树上维护区间修改与区间和查询. 很明显树剖裸题,切掉,细节处错误T了好久 TAT 代码 #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #define int long long #define R register #define ls o<<1 #define rs o<<1|1 #define N 50000…
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional roads, such that there is exactly one path between any two pastures. Bessie, a cow who loves her grazing time, often complains about how there is no grass on t…
思路: 首先,这道题的翻译是有问题的(起码现在是),查询的时候应该是查询某一条路径的权值,而不是某条边(坑死我了). 与平常树链剖分题目不同的是,这道题目维护的是边权,而不是点权,那怎么办呢?好像有点棘手诶,这是一种非常经典的题型,我们可以发现,一个点最多只有一个父亲!!!那,我们显然就可以用这个点的点权去代替它与它父亲之间的边权!!!然后这道题不就成了树链剖分水题了嘛?刚开始边权都是\(0\),那我们就根据题目给的边建边权为\(0\)的边. \(nonono\),还有一个坑点就是在路径查询和修…
Grass Planting 题意 给出一棵树,树有边权.每次给出节点 (u, v) ,有两种操作:1. 把 u 到 v 路径上所有边的权值加 1.2. 查询 u 到 v 的权值之和. 分析 如果这些值不是在树上,而是在区间上,那么凭借线段树.树状数组可以很轻松的解决,但是在树上则不能直接操作. 树链剖分就是将树上的节点映射到区间上,从而实现区间操作. 学习树链剖分前需要掌握的知识点:线段树.LCA. 参考blog 认真读完这篇 blog ,跟着算法流程走一遍差不多就懂了. code #incl…