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和其整个子树都将被删去,求被 ...
随机推荐
- 14,UA池和代理池
今日概要 scrapy下载中间件 UA池 代理池 一,下载中间件(Downloader Middlewares) 位于scrapy引擎和下载器之间的一层组件. - 作用: (1)引擎将请求传递给下载器 ...
- Oracle 分析函数--Row_Number()
row_number() over ([partition by col1] order by col2) ) as 别名 表示根据col1分组,在分组内部根据 col2排序 而这个“别名”的值就表示 ...
- jquery ajax return jsonresult pattern
//javascriptvar queryParams = { "userId": userId, "factoryId": factoryId } ...
- CSU-2116 Polyline Simplification
CSU-2116 Polyline Simplification Description Mapping applications often represent the boundaries of ...
- Atom-无懈可击的Markdown编辑器
备战美赛期间,向岳神学习,搞了Atom玩协作开发,第一次没有自动补全的手撸了遗传算法.今天发现Atom还有写Markdown的妙用,遂拿来练手. 1. 安装Atom 下载安装Atom:https:// ...
- mac最新系统安装beego出现kiil 9
(内容来自:http://www.oschina.net/question/2626413_2237311) 应该是最新mac OS 12.04的锅. 现在的解决办法是回退bee到以前版本. cd $ ...
- BZOJ-1038 [ZJOI2008]瞭望塔
先求半平面交,然后建塔的地方肯定是在半平面交的交点上或者是在地面线段的交点上. #include <cstdlib> #include <cstdio> #include &l ...
- BZOJ3295 [Cqoi2011]动态逆序对 【CDQ分治】
题目 对于序列A,它的逆序对数定义为满足i 输入格式 输入第一行包含两个整数n和m,即初始元素的个数和删除的元素个数.以下n行每行包含一个1到n之间的正整数,即初始排列.以下m行每行一个正整数,依次为 ...
- FZU 2168 前缀和+dp递推
Description 部队中共有N个士兵,每个士兵有各自的能力指数Xi,在一次演练中,指挥部确定了M个需要防守的地点,按重要程度从低到高排序,依次以数字1到M标注每个地点的重要程度,指挥部将选择 ...
- 【HDOJ5532】Almost Sorted Array(签到)
题意:给定一个n个数的数列,问删掉一个数之后剩余部分是否可以单调不增或单调不减 n<=1e5,a[i]<=1e5 思路:预处理一下前后缀是否合法 #include<cstdio> ...