嘟嘟嘟

前缀和+倍增+树上差分

假设\(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. 原创:微信小程序之MaterialDesign--input组件

    作者:jeffer 来自:原文地址 主要通过input输入事件配合css的transform动态改变实现这种效果. 实际调试过程中,input组件bindinput事件触发后回调的detail对象,在 ...

  2. ssh登录实现

    工程目录 配置文件详解 Spring的applicationContext.xml文件 <span ><?xml version="1.0" encoding=& ...

  3. hdu 1011 Starship Troopers 经典的树形DP ****

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. Mybatis执行sql(insert、update、delete)返回值问题

    数据库:Mysql 在使用mybatis的过程中对执行sql的返回值产生疑问,顺手记录一下. 结论: insert:   插入n条记录,返回影响行数n.(n>=1,n为0时实际为插入失败) up ...

  5. maven配置环境

    今天初学maven,先学习一下如何在windows下面配置maven,当然你要先配置好jdk的环境. 第一步,上官网下载maven插件,网址是:点击打开链接 第二步,解压文件夹,放在某一个盘符下,我是 ...

  6. 基础架构之GitLab

    Git几乎是软件开发人员的必备工具了,关于代码管理,公司都一般都会搭建自己的仓库,关于GitLab的详细介绍参见官方网站详见 https://about.gitlab.com,这篇文章主要介绍安装及使 ...

  7. GreenDao 初体验

    GreenDao 使用 环境搭建(android studio) project的build.gradle buildscript { repositories { google() jcenter( ...

  8. sqlserver批量删除表

    --批量删除表 ) DECLARE tmpCur CURSOR FOR SELECT name FROM sys.objects WHERE TYPE='U' AND name LIKE N'%_Qu ...

  9. nginx 应用包编译及常用文件配置

    1.zlib wget http://www.zlib.net/fossils/zlib-1.2.8.tar.gz 2.openssl wget http://www.openssl.org/sour ...

  10. golang闭包

    http://blog.51cto.com/speakingbaicai/1703229 https://www.jianshu.com/p/fa21e6fada70 所谓闭包就是一个函数" ...