Codeforces 739B Alyona and a tree(树上路径倍增及差分)
题目链接 Alyona and a tree
比较考验我思维的一道好题。
首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$。$t[i][j]$表示从第$i$个节点到离他第$2^{j}$近的祖先,$d[i][j]$表示从$i$开始到$t[i][j]$的路径上的路径权值总和。
在第一次DFS的同时,对节点$x$进行定位(结果为$dist(x, y)<=a(y)$)的离$x$最远的$x$的某个祖先,然后进行$O(1)$的差分。
第一次DFS完成后,做第二次DFS统计答案(统计差分后的结果)
时间复杂度$O(NlogN)$
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for(int i(0); i < (n); ++i)
#define rep(i, a, b) for(int i(a); i <= (b); ++i)
#define dec(i, a, b) for(int i(a); i >= (b); --i)
#define LL long long
#define sz(x) (int)x.size() const int N = 200000 + 10;
const int A = 30 + 1; vector <int> v[N], c[N];
LL a[N], deep[N];
LL x, y;
int n, cnt;
LL t[N][A], d[N][A];
LL g[N], value[N];
LL s[N];
LL ans[N]; void dfs(int x, int fa){ if (g[x]){
t[x][0] = g[x];
d[x][0] = value[x];
for (int i = 0; t[t[x][i]][i]; ++i){
t[x][i + 1] = t[t[x][i]][i];
d[x][i + 1] = d[t[x][i]][i] + d[x][i];
}
int now = x, noww = 0;
bool flag = false;
dec(i, 20, 0){
if (t[now][i] && d[now][i] + noww <= a[x]){
noww += d[now][i];
now = t[now][i];
flag = true;
}
}
if (flag){
--s[g[now]]; ++s[g[x]];
}
} REP(i, sz(v[x])){
int u = v[x][i];
deep[u] = deep[x] + 1;
dfs(u, x);
}
} void dfs2(int x){
ans[x] += s[x];
REP(i, sz(v[x])){
dfs2(v[x][i]);
ans[x] += ans[v[x][i]];
}
} int main(){ scanf("%d", &n);
rep(i, 1, n) scanf("%lld", a + i);
rep(i, 2, n){
scanf("%lld%lld", &x, &y);
g[i] = x; value[i] = y;
v[x].push_back(i), c[x].push_back(y);
} memset(s, 0, sizeof s);
cnt = 0;
deep[1] = 0;
dfs(1, 0);
memset(ans, 0, sizeof ans);
dfs2(1);
rep(i, 1, n - 1) printf("%lld ", ans[i]);
printf("%lld\n", ans[n]);
return 0; }
Codeforces 739B Alyona and a tree(树上路径倍增及差分)的更多相关文章
- XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 739B Alyona and a tree (二分+树上差分)
<题目链接> 题目大意: 给定一颗带权树,树的根是1,树上每个点都有点权,并且还有边权.现在给出“控制”的定义:对一个点u,设v为其子树上的节点,且$dis(u,v)≤val[v]$,则称 ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...
- Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- CodeForces 682C Alyona and the Tree (树上DFS)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...
- XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)
Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly fou ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
随机推荐
- ACM模板
#include <iostream> //万能头文件#include<bits/stdc++.h> 方便时用 #include <algorithm> #incl ...
- kettle - Linux下定时执行kettle作业
Linux下实现kettle 自动同步数据 1.安装jdk tar -zxvf jdk-7u25-linux-x64.tar.gz -C /usr/share 2.配置java环境 vim /etc/ ...
- Redis实现之RDB持久化(二)
RDB文件结构 在Redis实现之RDB持久化(一)这一章中,我们介绍了Redis服务器保存和载入RDB文件的方法,在这一节,我们将对RDB文件本身进行介绍,并详细说明文件各个部分的结构和意义.图1- ...
- Python-S9——Day84-ORM项目实战之权限、form以及modelform
01 权限菜单显示 02 Django路径的自动添加问题 03 原生form实现增删改查 04 modelform实现增删改查 01 权限菜单显示 1.1 优先查找项目中的templates,如果没有 ...
- [LOJ#2326]「清华集训 2017」简单数据结构
[LOJ#2326]「清华集训 2017」简单数据结构 试题描述 参加完IOI2018之后就是姚班面试.而你,由于讨厌物理.并且想成为乔布斯一样的创业家,被成功踢回贵系. 转眼,时间的指针被指向201 ...
- Uva 10590 Boxes of Chocolates Again
题面戳这里 dp的姿势有两种(都保证了拆分的有序): \(f_{i,j}\)表示拆分中最大数为\(j\),和为\(i\)的方案数.转移\[f_{i,j} = \sum_{k = 1}^j f_{i-j ...
- serviceImpl中,方法加@Override注释后报错
@Override public List<SysAdminMenu> getAdminMenusAll() { return sysAdminMenuMapper.getAdminMen ...
- Python之数据结构:字符串中的方法
一.过滤字符串 1.strip() (1)去掉行收尾不可见字符 a = ' wejifrow ' print a print a.strip() 结果: wejifrow wejifrow (2)st ...
- Java语法糖(一)
概述 语法糖(Syntactic Sugar):主要作用是提高编码效率,减少编码出错的机会. 解语法糖发生在Java源码被编译成Class字节码的过程中,还原回简单的基础语法结构. 语法糖之一:泛型( ...
- 【BZOJ1030】文本生成器(容斥原理,AC自动机,计数DP)
题意:给出n个字符串,求长为m至少包含n个里其中一个的串的字符串一共有多少个,字符集为A到Z,答案对10007取模 n<=60,len<=100 思路:将至少一个转化为所有个数减去没有出现 ...