题目链接

题目

题目描述

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的更多相关文章

  1. [USACO 2011 Dec Gold] Threatening Letter【后缀】

    Problem 3: Threatening Letter [J. Kuipers, 2002] FJ has had a terrible fight with his neighbor and w ...

  2. [USACO 2011 Dec Gold] Cow Calisthenics【二分】

    Problem 1: Cow Calisthenics [Michael Cohen, 2010] Farmer John continues his never-ending quest to ke ...

  3. USACO Grass Planting

    洛谷 P3038 [USACO11DEC]牧草种植Grass Planting 洛谷传送门 JDOJ 2282: USACO 2011 Dec Gold 3.Grass Planting JDOJ传送 ...

  4. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  5. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  6. [USACO 2017 Dec Gold] Tutorial

    Link: USACO 2017 Dec Gold 传送门 A: 为了保证复杂度明显是从终结点往回退 结果一开始全在想优化建边$dfs$……其实可以不用建边直接$multiset$找可行边跑$bfs$ ...

  7. spoj - Grass Planting(树链剖分模板题)

    Grass Planting 题意 给出一棵树,树有边权.每次给出节点 (u, v) ,有两种操作:1. 把 u 到 v 路径上所有边的权值加 1.2. 查询 u 到 v 的权值之和. 分析 如果这些 ...

  8. 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 ...

  9. 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 ...

  10. NC25025 [USACO 2007 Nov G]Sunscreen

    NC25025 [USACO 2007 Nov G]Sunscreen 题目 题目描述 To avoid unsightly burns while tanning, each of the \(C\ ...

随机推荐

  1. python · ssh · SQL | python 连接远程 SQL 数据库

    python 连接本地 SQL 的 教程存档. 如果要连接远程的 SQL 数据库,需要先开一个 ssh 连接,在 ssh 连接里写 pymysql 的 connect 代码. 代码如下: ''' pi ...

  2. React技巧之设置input值

    原文链接:https://bobbyhadz.com/blog/react-set-input-value-on-button-click 作者:Borislav Hadzhiev 正文从这开始~ 总 ...

  3. SD Host控制器的系统集成

    控制器集成需求 SD Host可以从外部读取数据存储到SRAM和eFlash或者可以从内部读取数据输出到外部存储 AHB Bus slave接口用于配置 master接口作为主设备,DMA可以控制总线 ...

  4. 你和时间管理大师,就差一个开源工具「GitHub 热点速览」

    在这个快节奏的生活中,我们努力地在平衡工作.生活和个人发展,但常常感到时间不够用.如何在繁忙的日程中找到一丝丝"喘息"的机会,这个名叫 cal.com 开源项目能让你更轻松地管理日 ...

  5. SqlSugar DbContext

    title: SqlSugar DbContext date: 2023-02-16 20:01:41 tags: SqlSugar categories: ORM description: 总结整理 ...

  6. [转帖]Oracle11g实现只读表方法

    1.1 ALTER TABLE tab_name READ ONLY 参考:https://www.cnblogs.com/chinas/p/8440460.html Oracle 11g开始支持设置 ...

  7. [转帖]浅谈Armv8-A处理器

    https://www.elecfans.com/emb/dsp/202208291886182.html 众所周知,ARM是一家设计并授权处理器和相应IP(比如互连总线,中断处理器,图像处理器等等) ...

  8. [转帖]如何优雅的使用 Systemd 管理服务

    https://zhuanlan.zhihu.com/p/271071439 背景:我们在构建 Kubernetes 容器化平台时,会在节点上部署各种 agent ,虽然容器化当道的今天很多程序可以直 ...

  9. [转帖]springboot指定端口的三种方式

    https://blog.51cto.com/feirenraoyuan/5504099 第一配置文件中添加server.port=9090 第二在命令行中指定启动端口,比如传入参数 java -ja ...

  10. F5内核参数的简要学习

    前言 最近学习了很长时间的Linux内核参数 但是大部分是纸上谈兵. 也没有一个好的系统用于学习和参照 晚上搜索F5资料时发现F5有一些iso和ova文件 就想着下载学习一下. 看看F5系统默认的参数 ...