题目链接 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(树上路径倍增及差分)的更多相关文章

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

  2. CodeForces 739B Alyona and a tree (二分+树上差分)

    <题目链接> 题目大意: 给定一颗带权树,树的根是1,树上每个点都有点权,并且还有边权.现在给出“控制”的定义:对一个点u,设v为其子树上的节点,且$dis(u,v)≤val[v]$,则称 ...

  3. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

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

  5. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

  6. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  7. CodeForces 682C Alyona and the Tree (树上DFS)

    题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...

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

  9. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

随机推荐

  1. HDU_6194 后缀数组+RMQ

    好绝望的..想了五个多小时,最后还是没A...赛后看了下后缀数组瞬间就有了思路...不过因为太菜,想了将近两个小时才吧这个题干掉. 首先,应当认为,后缀数组的定义是,某字符串S的所有后缀按照字典序有小 ...

  2. 利用本地SQL Server维护计划来维护SQL Database

    On-Premise的SQL Server提供了维护计划来定期.定时的维护SQL Server.一般的做法是:定义SQL Server Agent Jobs,而后维护计划帮助我们定期.定时执行SQL ...

  3. Spring---基于Spring IOC的小程序

    实现的功能以及各文件间的关系 IHelloMessage:一个接口,用于定义输出问候信息. HelloWorld.HelloChina:接口的实现类.在这里表示人在不同的地方 Person:一个人物类 ...

  4. MySQL之架构与历史(一)

    MySQL架构与历史 和其他数据库系统相比,MySQL有点与众不同,它的架构可以在多种不同的场景中应用并发挥好的作用,但同时也会带来一点选择上的困难.MySQL并不完美,却足够灵活,它的灵活性体现在很 ...

  5. 倍增 - 强制在线的LCA

    LCA 描述 给一棵有根树,以及一些询问,每次询问树上的 2 个节点 A.B,求它们的最近公共祖先. !强制在线! 输入 第一行一个整数 N. 接下来 N 个数,第 i 个数 F i 表示 i 的父亲 ...

  6. Solr安装 win系统

    安装之前需查看:https://lucene.apache.org/solr/guide/7_6/solr-system-requirements.html#solr-system-requireme ...

  7. springboot集成pagehelper插件

    1.在pom.xml中引入依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifact ...

  8. koa2 + webpack 热更新

    网上有很多express+webpack的热更新,但是koa2的很少,这两天研究了一下子,写一个简单的教程. 1.需要的包 webpack:用于构建项目 webpack-dev-middleware: ...

  9. 【Luogu】P3320寻宝游戏(Splay)

    题目链接 其实这题用Set就完事了但我不会Set 智商-=inf 求虚树上所有边权和的两倍. 具体方式就是splay把所有在虚树上的点存一下,(按照DFS序排序的)每次插入/删除会更新前驱和它.后继和 ...

  10. openssl RSA 内存读取密钥

    主要注意一下密钥的格式 #include <openssl/pem.h> #include <openssl/err.h> bool CEncipher::CreatePubK ...