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\ ...
随机推荐
- 每天学五分钟 Liunx 001 | 用户及用户组
Liunx 文件权限 [root@controller-0 ~]# ll -al heihei -rw-r--r--. 1 root root 0 Mar 3 07:39 heihei 第一列 -rw ...
- Kafka 面试题
1. 为什么要使用 Kafka,为什么要使用消息队列 1.使用消息队列的目的: 服务解耦 流量削峰 异步通信 在早期的 web 应用程序开发中,当请求量突然上来了时候,我们会将要处理的数据推送到一个队 ...
- pmp考试巩固知识点
1.冲刺评审会是需要相关的干系人参加的,在冲刺评审会上干系人可以审查并澄清角色.责任和管理模式2.采购中的争议,往往找合同和SOW,SOW是对需要采购的详细范围的描述,与供应商在可交付成果方面有争议时 ...
- 在线photoshop网页版工具开发
基于javascript开发的在线ps工具,打包方式webpack 在线预览 在线ps网页版 源码地址 https://github.com/geeeeeeeek 功能介绍 在线图像编辑器允许您使用H ...
- 【ThreadX-NetX】Azure RTOS NetX概述
Azure RTOS NetX是工业级TCP / IP IPv4嵌入式网络堆栈,专门针对深度嵌入式,实时和IoT应用程序而设计.Azure RTOS NetX是Microsoft最初的IPv4网络堆栈 ...
- [转帖]已整理-shell内置字符串常用操作
https://www.cnblogs.com/reachos/p/16803672.html bash 里面内置了一些常用的字符串操作: 1.字符串截取 a="abc" ${a: ...
- [转帖]PD Config Learn the PD configuration file
The PD configuration file supports more options than command-line parameters. You can find the defau ...
- [转帖]15分钟了解TiDB
https://zhuanlan.zhihu.com/p/338947811 由于目前的项目把mysql换成了TiDb,所以特意来了解下tidb.其实也不能说换,由于tidb和mysql几乎完全兼容, ...
- [转帖]linux服务之tuned
https://www.cnblogs.com/createyuan/p/5701650.html RHEL/CentOS 在 6.3 版本以后引入了一套新的系统调优工具 tuned/tuned-ad ...
- SQL注入payload学习整理
SQLserver 用的payload 0101%'and 1=(select @@version) and '%'=' GS的一个客户端参数 <add PropertyName="F ...