D. Alyona and a tree 公式转换 + 分块暴力
http://codeforces.com/problemset/problem/740/D
对于每一对<u, v>。设dis[u]表示root到点u的距离,那么dis<u去v>就是dis[v] - dis[u],
就是在它的儿子中找出有多少个v使得dis[v] - dis[u] <= a[v]。然后,因为如果v确定了,那么dis[v]和a[v]就确定了。
所以把公式转换为dis[v] - a[v] <= dis[u]。
那么可以暴力枚举每一个u,然后在其儿子中找有多少个数小于等于它,这个可以直接暴力分块。
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <assert.h>
- #define IOS ios::sync_with_stdio(false)
- using namespace std;
- #define inf (0x3f3f3f3f)
- typedef long long int LL;
- #include <iostream>
- #include <sstream>
- #include <vector>
- #include <set>
- #include <map>
- #include <queue>
- #include <string>
- const int maxn = 2e5 + ;
- struct node {
- int u, v, w;
- int tonext;
- }e[maxn];
- int first[maxn];
- int num;
- void add(int u, int v, int w) {
- ++num;
- e[num].u = u;
- e[num].v = v;
- e[num].w = w;
- e[num].tonext = first[u];
- first[u] = num;
- }
- int a[maxn];
- int ans[maxn];
- LL dp[maxn];
- struct LIST {
- int id;
- LL val;
- }List[maxn];
- int lenList;
- int DFN;
- int L[maxn];
- int R[maxn];
- void dfs(int cur) {
- List[++lenList].id = cur;
- // List[lenList].val = a[cur];
- ++DFN;
- L[cur] = DFN;
- for (int i = first[cur]; i; i = e[i].tonext) {
- int v = e[i].v;
- dp[v] = dp[e[i].u] + e[i].w;
- dfs(v);
- }
- R[cur] = DFN;
- }
- LL tosort[maxn];
- void work() {
- int n;
- scanf("%d", &n);
- for (int i = ; i <= n; ++i) {
- scanf("%d", &a[i]);
- }
- for (int i = ; i <= n - ; ++i) {
- int fa, w;
- scanf("%d%d", &fa, &w);
- add(fa, i + , w);
- }
- dfs();
- for (int i = ; i <= lenList; ++i) {
- List[i].val = dp[List[i].id] - a[List[i].id];
- tosort[i] = List[i].val;
- // printf("%d ", List[i].id);
- // printf("%d %d\n", L[List[i].id], R[List[i].id]);
- }
- int magic = (int)sqrt(lenList);
- for (int i = ; i <= lenList;) {
- if (i + magic - <= lenList) {
- sort(tosort + i, tosort + i + magic);
- } else break;
- i += magic;
- }
- for (int i = ; i <= n; ++i) {
- for (int j = L[i] + ; j <= R[i];) {
- if (j % magic == && j + magic - <= R[i]) {
- int pos = upper_bound(tosort + j, tosort + j + magic, dp[i]) - (tosort + j - );
- ans[i] += pos - ;
- j += magic;
- } else {
- if (dp[i] >= List[j].val) {
- ans[i]++;
- }
- j++;
- }
- }
- }
- for (int i = ; i <= n; ++i) {
- printf("%d ", ans[i]);
- }
- }
- int main() {
- #ifdef local
- freopen("data.txt", "r", stdin);
- // freopen("data.txt", "w", stdout);
- #endif
- work();
- return ;
- }
D. Alyona and a tree 公式转换 + 分块暴力的更多相关文章
- 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(树+二分+dfs)
D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...
- codeforces 381 D Alyona and a tree(倍增)(前缀数组)
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和
B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...
- 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 ...
- Alyona and a tree
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree 水题
C. Alyona and the Tree 题目连接: http://www.codeforces.com/contest/682/problem/C Description Alyona deci ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- 纯C语言实现简单封装继承机制
0 继承是OO设计的基础 继承是OO设计中的基本部分,也是实现多态的基础,C++,C#,Objective-C.Java.PHP.JavaScript等为OO而设计的语言,其语言本身对实现继承提供了直 ...
- httpclient发送get请求
/** * 获取httpclient的请求url地址 */ public static String getUrl(){ String url = "http://"+map.ge ...
- hadoop生态系统学习之路(十)MR将结果输出到hbase
之前讲了MR将结果输出到hdfs.hive.db,今天再给大家分享一下,怎样将结果输出到hbase. 首先,提一句,笔者在hadoop集群运行此MR的时候报了一个错误.是一个jar包的缘故,这个错误是 ...
- 使用JS对select标签进行联动选择
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- centos 安装mysql时错误unknown variable 'defaults-file=/opt/redmine-2.6.0-2/mysql/my.cnf'
找到my.cnf所在目录.运行 chmod 664 my.cnf,再启动mysql成功
- 【iOS系列】-UIImageView帧动画相关属性介绍
UIImageView帧动画相关属性介绍 1:相关属性: //An array of UIImage objects to use for an animation.存放UIImage对象,会按顺序显 ...
- 在webkit中如何避免触发layout(重排)
很多web开发者都已经意识到,在脚本执行中,DOM操作的用时可能比js本身执行时间要长很多,其中潜在的消耗基本上是由于触发了layout(即重排reflow:由DOM树构建为Render渲染树的过程) ...
- 查看android-support-v4.jar引出的问题
1.前面博文里也写过如何关联android-support-v4.jar的源码 今天新项目用上述方法的时候,竟然不成功..来回反复试了很长时间,最后发现 新建项目,会自动引用一个类库(自动新建的..) ...
- Django模板语言(一)
1,Django模板语言 1.1>基础语法:1.1.1,变量相关:{{ 变量名 }},1.1.2,逻辑相关{% ... %} 在Django模板语言中按此语法使用:{{ 变量名 }},当模板引擎 ...
- bean的scope属性
1.singleton (默认属性) Spring将Bean放入Spring IOC容器的缓存池中,并将Bean引用返回给调用者,spring IOC继续对这些Bean进行后续的生命管理.Bean ...