题目链接

题目

题目描述

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. 项目使用 GlobalExceptionHandler 自定义异常 一

    博主原创,未经允许不得转载: 每个项目都有自己的一套异常类的定义.总结一下,项目中使用自定义异常比较好的封装. 1.定义项目中统一使用的异常类,用于捕获项目中的自定义异常等: package com. ...

  2. ASIC 功能验证SVTB

    System Verilog进行验证是可以不综合的 发现DUT中的功能问题 预备知识:Linux/verilog/gvim System Verilog学习目录 System Verilog Test ...

  3. MySQL本地服务器与MySQL57网络服务器区别

    MySQL服务器与MySQL57服务器区别与不同处在哪里,他们各自的领域范围,能不能同时启动服务? 安装了MySQL-5.7.18.0版本数据库,版本中包含了MySQL Workbench可视化试图工 ...

  4. 【C/C++】sscanf函数和正则表达式

    此文所有的实验都是基于下面的程序:char str[10];for (int i = 0; i < 10; i++) str[i] = '!';执行完后str的值为str = "!!! ...

  5. 2023第十四届极客大挑战 — RE WP

    RE方向出自:队友. Shiftjmp 去花后按p然后再反编译 最后flag为SYC{W3lc0me_tO_th3_r3veR5e_w0r1d~} 点击就送的逆向题 gcc 1.s -o 1` 生成e ...

  6. Kubeadm 安装支持IPV6 K8S1.28.x的简单过程

    Kubeadm 安装支持IPV6 K8S的简单过程 背景 手贱 找了一个晚上想尝试安装一个K8S集群 并且可以支持IPV6 协议栈的 然后就开始各种百度. 各种处理 找到了一堆歪门邪道. 但是还不知道 ...

  7. tikv-ctl的简单学习

    tikv-ctl的简单学习 摘要 最近在学习使用 tidb. 有一个场景,单独使用了tikv作为键值对的数据库. 但是比较不幸.总是出现宕机的情况 因为这个环境是单独使用tikv 二进制进行安装的 没 ...

  8. [转帖]SQL Server JDBC – Set sendStringParametersAsUnicode to false

    https://vladmihalcea.com/sql-server-jdbc-sendstringparametersasunicode/ https://learn.microsoft.com/ ...

  9. React Hooks 指北

    前言 这篇文章旨在总结 React Hooks 的使用技巧以及在使用过程中需要注意的问题,其中会附加一些问题产生的原因以及解决方式.但是请注意,文章中所给出的解决方式并不一定完全适用,解决问题的方案有 ...

  10. 从好玩到好用:程序员用AI提效的那些事儿

    本片内容是[AI思维空间]ChatGPT纵横编程世界,点亮智慧火花的续作,主要记录组内开发小伙伴儿们在开发过程中的实际应用案例,记录典型案例,尽量不要和其他人重复,以解决开发过程中的实际问题为主,设计 ...