嘟嘟嘟

前缀和+倍增+树上差分

假设\(v\)是\(u\)子树中的一个点,那么\(u\)能控制\(v\)的条件是受\(v\)的权值的限制,而并非\(u\)。因此我们就能想到计算每一个点的贡献,即\(v\)有多少个祖先能控制它。这样就能想到暴力的做法:枚举每一个点\(i\),向上爬直到两点间距离大于\(a_i\)为止。然后树上差分(准确说是链上差分)即可。至于两点间距离,采用前缀和相减。

但这样的复杂度能达到\(O(n^2)\),因此我们可以用倍增优化一步步向上跳,达到\(O(nlogn)\)。

总结一下,先\(dfs\)一遍求出每一个点到根节点的距离和差分数组,复杂度\(O(nlogn)\);然后对于每一个点倍增向上跳,并修改差分数组,复杂度也是\(O(nlogn)\);最后\(O(n)\) \(dfs\)一遍求查差分组的树上前缀和。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2e5 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n;
ll a[maxn];
struct Edge
{
int nxt, to, w;
}e[maxn];
int head[maxn], ecnt = -1;
void addEdge(int x, int y, int w)
{
e[++ecnt] = (Edge){head[x], y, w};
head[x] = ecnt;
} int fa[21][maxn];
ll dis[maxn];
void dfs(int now)
{
for(int i = 1; i <= 20; ++i)
fa[i][now] = fa[i - 1][fa[i - 1][now]];
for(int i = head[now], v; i != -1; i = e[i].nxt)
{
v = e[i].to;
fa[0][v] = now;
dis[v] = dis[now] + e[i].w;
dfs(v);
}
} int dif[maxn];
void solve(int now)
{
int x = now;
for(int i = 20; i >= 0; --i)
if(fa[i][x] && dis[now] - dis[fa[i][x]] <= a[now]) x = fa[i][x];
if(x != 1) dif[fa[0][x]]--;
if(now != 1) dif[fa[0][now]]++;
} void dfs2(int now)
{
for(int i = head[now], v; i != -1; i = e[i].nxt)
{
v = e[i].to;
dfs2(v);
dif[now] += dif[v];
}
} int main()
{
Mem(head, -1);
n = read();
for(int i = 1; i <= n; ++i) a[i] = read();
for(int i = 2; i <= n; ++i)
{
int x = read(), w = read();
addEdge(x, i, w);
}
dfs(1);
for(int i = 1; i <= n; ++i) solve(i);
dfs2(1);
for(int i = 1; i <= n; ++i) write(dif[i]), space; enter;
return 0;
}

CF739B Alyona and a tree的更多相关文章

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

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

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

  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. Alyona and a tree

    Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

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

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

  9. Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. [转]Entity FrameWork利用Database.SqlQuery<T>执行存储过程并返回参数

    本文转自:http://www.cnblogs.com/xchit/p/3334782.html 目前,EF对存储过程的支持并不完善.存在以下问题:        EF不支持存储过程返回多表联合查询的 ...

  2. vue中echarts引入中国地图

    <div id="myChartChina" :style="{width: '100%', height: '500px'}"></div& ...

  3. NetMQ:.NET轻量级消息队列

    前言 首先我现在是在一家游戏工作做服务端的,这几天我们服务端游戏做了整个底层框架的替换,想必做过游戏的也都知道,在游戏里面会有很多的日志需要记录,量也是比较大的:在没有换框架之前我们存日志和游戏运行都 ...

  4. C#泛型List的介绍

    一.List<T>描述 1).表示可通过索引访问的对象的强类型列表:提供用于对列表进行搜索.排序和操作的方法.2).是ArrayList类的泛型等效类.3).可以使用一个整数索引访问此集合 ...

  5. UVA1339(字母映射)

    memcmp(const void *buf1, const void *buf2, unsigned int count)可以比较两个串相等 http://baike.baidu.com/link? ...

  6. CSS的伪类 :before 和 :after

    CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是它们的功用不仅于此.前几天发现了 Creative Link Effects 这个非常有意思 ...

  7. Centos 7 系统安装(简单步骤)

    前面步骤忽略.进入安装步骤. 运行安装 到选择语言的时候最好选英文版,这里做模板,用的中文版 接着下一步到安装选项 在日期和时间里,选择上海时区 紧接着进行软件安装选择,如图安装就好 接着进行分区,也 ...

  8. HTML5 : 文件上传下载

    网站建设中,文件上传与下载在所难免,HTML5中提供的API在前端有着丰富的应用,完美的解决了各个浏览器的兼容性问题,所以赶紧get吧! FileList 对象和 file 对象 HTML 中的 in ...

  9. DLL调用的两种方式(IDE:VC6.0,C++)

    原文:http://www.cnblogs.com/Pickuper/articles/2050409.html DLL调用有两种方式,一种是静态调用,另外一种是动态调用 (一)静态调用 静态调用是一 ...

  10. Android SQLite与AutoCompleteTextView

    读取SQLite中的数据显示在AutoCompleteTextView中,支持动态加入SQLite中不存在的数据. package zhang.ya; import java.io.File; imp ...