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 公式转换 + 分块暴力的更多相关文章

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

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

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

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

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

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

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

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

  7. Alyona and a tree

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

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

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

随机推荐

  1. 为经典版eclipse添加web and JavaEE插件

    地址:http://download.eclipse.org/releases/juno. 选择Web,XML,Java EE and OSGI Enterprise Development,之后ne ...

  2. Android Material Design-Maintaining Compatibility(保持兼容性)-(七)

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/40634829 翻译自: http://developer.android.com/traini ...

  3. 前台传JSON到后台

    现在,有一个需求,我需要将表格中选中行的数据中的一部分传直接传到控制器中,然后保存到另外一张表中.一开始,我就想到在前台使用ajax构造json数据,然后控制器直接通过list接收. 选中界面中的行, ...

  4. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  5. mac toad下建表问题

    mac toad下创建表,表名会自动多一个双引号,如:tb_test => "tb_test",这个应该是mac系统智能引号问题导致的,目前还没找到解决办法,只能手动用sql ...

  6. 2018.10.20 XMYZ Day1总结

    上周的忘写了……题目没有作者…… T1.backpack 期望得分100,实际得分100. 感觉我自己真是不如以前了……以前做这种题都是秒掉的,现在怎么想了10分钟啊…… 因为物品的体积和价值都非常小 ...

  7. linux WEB服务器***

    Apache sudo apt-get install apache2 PHP sudo apt-get install php5 sudo apt-get install php5-gd     / ...

  8. r.json()

    requests模块中,r.json()为Requests中内置的JSON解码器 其中只有response返回为json格式时,用r.json()打印出响应的内容, 如果response返回不为jso ...

  9. zoom和transform scale

    一.zoom zoom的字面意思是“变焦”,摄影的时候常用到的一个概念.对于web上的zoom效果,你也可以按照此概念理解.可以改变页面上元素的尺寸,属于真实尺寸. 在旧的web时代.*zoom: 1 ...

  10. HTML标签防XSS攻击过滤模块--待优化

    HTML标签防XSS攻击过滤模块 http://cnodejs.org/topic/5058962f8ea56b5e7806b2a3