洛谷P1600 天天爱跑步(差分 LCA 桶)
题意
Sol
一步一步的来考虑
\(25 \%\):直接\(O(nm)\)的暴力
链的情况:维护两个差分数组,分别表示从左向右和从右向左的贡献,
\(S_i = 1\):统计每个点的子树内有多少起点即可
\(T_i = 1\):同样还是差分的思想,由于每个点 能对其产生的点的深度是相同的(假设为\(x\)),那么访问该点时记录下\(dep[x]\)的数量,将结束时\(dep[x]\)的数量与其做差即可
满分做法和上面类似,我们考虑把每个点的贡献都转换到子树内统计
对于每次询问,拆为\(S->lca, lca -> T\)两种(从下到上 / 从上到下)
从上往下需要满足的条件:\(dep[i] - w[i] = dep[T] - len\)
从下往上需要满足的条件:\(dep[i] + w[i] = dep[s]\)
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP make_pair
#define fi first
#define se second
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, B = 20;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, ans[MAXN], dep[MAXN], top[MAXN], son[MAXN], siz[MAXN], fa[MAXN], S[MAXN],
T[MAXN], w[MAXN], tmp[MAXN], num2[MAXN], sum1[MAXN], sum2[MAXN], Lca[MAXN];
int *num1;//上 -> 下
vector<int> up[MAXN], da[MAXN], dc[MAXN];
vector<int> v[MAXN];
void dfs(int x, int _fa) {
dep[x] = dep[_fa] + 1; siz[x] = 1; fa[x] = _fa;
for(int i = 0, to; i < v[x].size(); i++) {
if((to = v[x][i]) == _fa) continue;
dfs(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
}
void dfs2(int x, int topf) {
top[x] = topf;
if(!son[x]) return ;
dfs2(son[x], topf);
for(int i = 0, to; i < v[x].size(); i++)
if(!top[to = v[x][i]]) dfs2(to, to);
}
int LCA(int x, int y) {
while(top[x] ^ top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
void Deal(int s, int t, int id) {// from s to t
int lca = LCA(s, t); Lca[id] = lca;
up[lca].push_back(s);//from down to up
int dis = dep[s] + dep[t] - 2 * dep[lca];
sum2[s]++;
da[t].push_back(dep[t] - dis);//increase
dc[lca].push_back(dep[t] - dis);//decrase
}
void Find(int x) {
int t1 = num1[dep[x] - w[x]], t2 = num2[dep[x] + w[x]];// 1: 从上往下 2:从下往上
for(int i = 0, to; i < v[x].size(); i++) {
if((to = v[x][i]) == fa[x]) continue;
Find(to);
}
num2[dep[x]] += sum2[x];
for(int i = 0; i < da[x].size(); i++) num1[da[x][i]]++;
ans[x] += num2[dep[x] + w[x]] - t2 + num1[dep[x] - w[x]] - t1;
for(int i = 0; i < up[x].size(); i++) num2[dep[up[x][i]]]--;
for(int i = 0; i < dc[x].size(); i++) num1[dc[x][i]]--;
}
int main() {
//freopen("a.in", "r", stdin); freopen("a.out", "w", stdout);
num1 = tmp + (int)3e5 + 10;
N = read(); M = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
dep[0] = -1; dfs(1, 0); dfs2(1, 1);
//for(int i = 1; i <= N; i++, puts("")) for(int j = 1; j <= N; j++) printf("%d %d %d\n", i, j, LCA(i, j));
for(int i = 1; i <= N; i++) w[i] = read();
for(int i = 1; i <= M; i++) S[i] = read(), T[i] = read(), Deal(S[i], T[i], i);
Find(1);
for(int i = 1; i <= M; i++) if(dep[S[i]] - dep[Lca[i]] == w[Lca[i]]) ans[Lca[i]]--;
for(int i = 1; i <= N; i++) printf("%d ", ans[i]);
return 0;
}
洛谷P1600 天天爱跑步(差分 LCA 桶)的更多相关文章
- 洛谷 P1600 天天爱跑步(LCA+乱搞)
传送门 我们把每一条路径拆成$u->lca$和$lca->v$的路径 先考虑$u->lca$,如果这条路径会对路径上的某一个点产生贡献,那么满足$dep[u]-dep[x]=w[x] ...
- 洛谷P1600 天天爱跑步(线段树合并)
小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 nn ...
- 洛谷P1600 天天爱跑步——树上差分
题目:https://www.luogu.org/problemnew/show/P1600 看博客:https://blog.csdn.net/clove_unique/article/detail ...
- 洛谷$P1600$ 天天爱跑步 树上差分
正解:树上差分 解题报告: 传送门$QwQ$! 这题还挺妙的,,,我想了半天才会$kk$ 首先对一条链$S-T$,考虑先将它拆成$S-LCA$和$LCA-T$,分别做.因为总体上来说差不多接下来我就只 ...
- 洛谷 P1600 天天爱跑步
https://www.luogu.org/problemnew/show/P1600 (仅做记录) 自己的假方法: 每一次跑从a到b:设l=lca(a,b)对于以下产生贡献: a到l的链上所有的点( ...
- 洛谷P1600 天天爱跑步
天天放毒... 首先介绍一个树上差分. 每次进入的时候记录贡献,跟出来的时候的差值就是子树贡献. 然后就可以做了. 发现考虑每个人的贡献有困难. 于是考虑每个观察员的答案. 把路径拆成两条,以lca分 ...
- 洛谷P1600 天天爱跑步——题解
题目传送 首先要考虑入手点.先考虑一个一个玩家处理,显然不加优化的话,时间复杂度是O(n)的.发现对于玩家路径上的点都有一个观察员,一个都不能忽视,看起来是很难优化了.在做题时,发现一个思路很难想,就 ...
- [NOIP 2016D2T2/Luogu P1600] 天天爱跑步 (LCA+差分)
待填坑 Code //Luogu P1600 天天爱跑步 //Apr,4th,2018 //树上差分+LCA #include<iostream> #include<cstdio&g ...
- 【洛谷】1600:天天爱跑步【LCA】【开桶】【容斥】【推式子】
P1600 天天爱跑步 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个 ...
随机推荐
- 拓扑排序+DP CF721C Journey
CF721C Journey 给出一个\(n\)个点\(m\)条边的有向无环图. 问从\(1\)到\(n\),在距离不超过\(k\)的情况下最多经过多少点,并输出一个方案. \(topo\)+\(DP ...
- 停不下来队 Scrum Meeting 博客汇总
停不下来队 Scrum Meeting 博客汇总 一.Alpha阶段 [Alpha]Scrum Meeting#1 [Alpha]Scrum Meeting#2 [Alpha]Scrum Meetin ...
- Sum(欧拉降幂+快速幂)
Input 2 Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cas ...
- python web开发小结
书籍 <python基础教程> <流畅的python> web框架 flask django tornado ORM sqlalchemy orator 消息队列 celery ...
- QT 相关
Qt是一个GUI框架,在GUI程序中,主线程也叫GUI线程,因为它是唯一被允许执行GUI相关操作的线程.对于一些耗时的操作,如果放在主线程中,就是出现界面无法响应的问题. 解决方法一:在处理耗时操作中 ...
- 初学SSM遇到的BUG
一.SpringMVC部分 1.参数绑定 1.1简单类型绑定 参数类型推荐使用包装数据类型,因为基础数据类型不可以为null 整形:Integer.int 字符串:String 单精度:Float.f ...
- Html checkbox全选
html中全选 <table class="data-table td-center"> <tr> <td><input type=&qu ...
- Clarke Award for Imagination in Service to Society刘慈欣演讲
刘慈欣不无批评地写道(http://cn.chinadaily.com.cn/2018-11/12/content_37243853.htm): 科幻的想象力由克拉克的广阔和深远,变成赛博朋克的狭窄和 ...
- 性能测试工具Jmeter13-Jmeter跨线程组调用token
1.正则表达式或者json提取器(我是用json提取器提取的),提取token 2.添加后置处理器BeanShell PostProcessor,然后输入以下函数 3.添加HTTP信息头管理器,写入函 ...
- Vue vs React: Javascript 框架之战
https://baijiahao.baidu.com/s?id=1608210396818353443&wfr=spider&for=pc 原文档 正如我们之前提到的,Word ...