NC24141 [USACO 2011 Dec G]Grass Planting
题目
题目描述
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 the roads between pastures. Farmer John loves Bessie very much, and today he is finally going to plant grass on the roads. He will do so using a procedure consisting of M steps (1 <= M <= 100,000).
At each step one of two things will happen:
- FJ will choose two pastures, and plant a patch of grass along each road in between the two pastures, or,
- Bessie will ask about how many patches of grass on a particular road, and Farmer John must answer her question.
Farmer John is a very poor counter -- help him answer Bessie's questions!
输入描述
- Line 1: Two space-separated integers N and M
- Lines 2..N: Two space-separated integers describing the endpoints of a road.
- Lines N+1..N+M: Line i+1 describes step i. The first character of the line is either P or Q, which describes whether or not FJ is planting grass or simply querying. This is followed by two space-separated integers Ai and Bi (1 <= Ai, Bi <= N) which describe FJ's action or query.
输出描述
- Lines 1..???: Each line has the answer to a query, appearing in the
same order as the queries appear in the input.
示例1
输入
4 6
1 4
2 4
3 4
P 2 3
P 1 3
Q 3 4
P 1 4
Q 2 4
Q 1 4
输出
2
1
2
题解
知识点:树链剖分,线段树。
通常树链剖分维护的是点权,但这里需要维护边权,因此我们考虑将边权映射到点权上,考虑用边的下端点代替一条边(上端点会导致非一对一映射)。
需要注意,更新操作时,对最后一段 \((u,v)\) 更新时,\(u\) 对应的那条边是不需要的,所以更新 \([L[u]+1,L[v]]\) 的点权。
时间复杂度 \(O((n+m)\log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct HLD {
vector<int> siz, dep, fat, son, top, dfn, L, R;
HLD() {}
HLD(int rt, const vector<vector<int>> &g) { init(rt, g); }
void init(int rt, const vector<vector<int>> &g) {
assert(g.size() >= rt + 1);
int n = g.size() - 1;
siz.assign(n + 1, 0);
dep.assign(n + 1, 0);
fat.assign(n + 1, 0);
son.assign(n + 1, 0);
top.assign(n + 1, 0);
dfn.assign(n + 1, 0);
L.assign(n + 1, 0);
R.assign(n + 1, 0);
function<void(int, int)> dfsA = [&](int u, int fa) {
siz[u] = 1;
dep[u] = dep[fa] + 1;
fat[u] = fa;
for (auto v : g[u]) {
if (v == fa) continue;
dfsA(v, u);
siz[u] += siz[v];
if (siz[v] > siz[son[u]]) son[u] = v;
}
};
dfsA(rt, 0);
int dfncnt = 0;
function<void(int, int)> dfsB = [&](int u, int tp) {
top[u] = tp;
dfn[++dfncnt] = u;
L[u] = dfncnt;
if (son[u]) dfsB(son[u], tp);
for (auto v : g[u]) {
if (v == fat[u] || v == son[u]) continue;
dfsB(v, v);
}
R[u] = dfncnt;
};
dfsB(rt, rt);
}
};
template <class T>
class Fenwick {
int n;
vector<T> node;
public:
Fenwick(int _n = 0) { init(_n); }
void init(int _n) {
n = _n;
node.assign(n + 1, T());
}
void update(int x, T val) { for (int i = x;i <= n;i += i & -i) node[i] += val; }
T query(int x) {
T ans = T();
for (int i = x;i >= 1;i -= i & -i) ans += node[i];
return ans;
}
};
struct T {
int sum = 0;
T &operator+=(const T &x) { return sum += x.sum, *this; }
};
const int N = 1e5 + 7;
vector<int> g[N];
HLD hld;
Fenwick<T> fw;
void path_update(int u, int v) {
auto &top = hld.top;
auto &dep = hld.dep;
auto &fat = hld.fat;
auto &L = hld.L;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
fw.update(L[top[u]], { 1 });
fw.update(L[u] + 1, { -1 });
u = fat[top[u]];
}
if (dep[u] > dep[v]) swap(u, v);
fw.update(L[u] + 1, { 1 });
fw.update(L[v] + 1, { -1 });
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1;i <= n - 1;i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
hld.init(1, vector<vector<int>>(g, g + n + 1));
fw.init(n);
while (m--) {
char op;
cin >> op;
if (op == 'P') {
int u, v;
cin >> u >> v;
path_update(u, v);
}
else {
int u, v;
cin >> u >> v;
if (hld.dep[u] < hld.dep[v]) swap(u, v);
cout << fw.query(hld.L[u]).sum << '\n';
}
}
return 0;
}
NC24141 [USACO 2011 Dec G]Grass Planting的更多相关文章
- [USACO 2011 Dec Gold] Threatening Letter【后缀】
Problem 3: Threatening Letter [J. Kuipers, 2002] FJ has had a terrible fight with his neighbor and w ...
- [USACO 2011 Dec Gold] Cow Calisthenics【二分】
Problem 1: Cow Calisthenics [Michael Cohen, 2010] Farmer John continues his never-ending quest to ke ...
- USACO Grass Planting
洛谷 P3038 [USACO11DEC]牧草种植Grass Planting 洛谷传送门 JDOJ 2282: USACO 2011 Dec Gold 3.Grass Planting JDOJ传送 ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- [USACO 2017 Dec Gold] Tutorial
Link: USACO 2017 Dec Gold 传送门 A: 为了保证复杂度明显是从终结点往回退 结果一开始全在想优化建边$dfs$……其实可以不用建边直接$multiset$找可行边跑$bfs$ ...
- spoj - Grass Planting(树链剖分模板题)
Grass Planting 题意 给出一棵树,树有边权.每次给出节点 (u, v) ,有两种操作:1. 把 u 到 v 路径上所有边的权值加 1.2. 查询 u 到 v 的权值之和. 分析 如果这些 ...
- NC24083 [USACO 2017 Dec P]Greedy Gift Takers
NC24083 [USACO 2017 Dec P]Greedy Gift Takers 题目 题目描述 Farmer John's nemesis, Farmer Nhoj, has N cows ...
- NC24866 [USACO 2009 Dec S]Music Notes
NC24866 [USACO 2009 Dec S]Music Notes 题目 题目描述 FJ is going to teach his cows how to play a song. The ...
- NC25025 [USACO 2007 Nov G]Sunscreen
NC25025 [USACO 2007 Nov G]Sunscreen 题目 题目描述 To avoid unsightly burns while tanning, each of the \(C\ ...
随机推荐
- 基于AHB_BUS的eFlash控制器的微架构设计
eFlash微架构设计 1.回顾架构设计 2.Flash时序仿真 2.1 ahb_flashc项目目录 docs rtl sim tb model 2.2 docs 架构设计文档 微架构设计文档 集成 ...
- AHB-SRAMC Design-01
AHB-SRAMC Design 1.AHB-SoC芯片架构图 CPU赋予了SoC的可编程性 SRAM可以存储数据和代码 2.AHB-SRAMC Features 总线版本号 在进行设计的时候可以将地 ...
- 百度网盘(百度云)SVIP超级会员共享账号每日更新(2023.11.25)
来给大家伙送福利了! 一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘, ...
- [转帖]python print如何格式化输出变量长度固定某个长度
https://zhuanlan.zhihu.com/p/595778735 在 Python 中,可以使用格式化字符串的方法来格式化输出变量. 例如,要将一个字符串变量 s 输出为 10 个字符长度 ...
- [转帖]linux中的set -e 与set -o pipefail
https://www.cnblogs.com/xingmuxin/p/8431970.html 1.set -e "Exit immediately if a simple command ...
- [转帖]TiDB调优小结
https://www.jianshu.com/p/d5ee4dca66d8 TiDB概览 先来一段官网的描述 TiDB server:无状态SQL解析层,支持二级索引,在线ddl,兼容MyS ...
- [转帖]谨慎调整内核参数:vm.min_free_kbytes
https://www.cnblogs.com/muahao/p/8082997.html 内核参数:内存相关 内存管理从三个层次管理内存,分别是node, zone ,page; 64位的x86物理 ...
- webpack配置scss
安装依赖: cnpm i sass-loader -D cnpm i node-sass -D node-sass尽量去使用cnpm去安装 创建index2.scss文件 div { h2 { bac ...
- Machine Learning | Coursera 课后代码
Machine Learning | Coursera GitHub地址:https://github.com/hanlulu1998/Coursera-Machine-Learning Machin ...
- Dto中使用正则校验规则,保证传入数据的正确性
使用RegularExpression