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 ...
随机推荐
- [经典面试题]在O(1)时间删除链表结点
[题目] 给定链表的头指针和一个结点指针.在O(1)时间删除该结点.链表结点的定义例如以下: struct ListNode { int value; struct ListNode* ...
- APUE 线程 - 程序清单
APUE 线程 - 程序清单 程序清单11-1 打印线程ID #include "util.h" #include<pthread.h> pthread_t ntid; ...
- Pierce振荡器设计
一.Pierce振荡器电路 Inv:内部反相器,作用等同于放大器: Q:石英晶体或陶瓷晶振: RF:内部反馈电阻(使反相器工作在线性区): RExt:外部限流电阻(防止石英晶体被过分驱动): CL1. ...
- ImageLoader实现图片异步载入
ImageLoader是一个广泛使用的图片库,在向网络请求图片时.使用imageView和smartView常会产生outofmemory错误,这时ImageLoader能够起到非常大的作用.主要有例 ...
- 【iOS系列】-autorelease的作用
内存管理原则(配对原则):只要出现了new,alloc,retain方法,就要配对出现release,autorelease 1:对象存入到自动释放池中,当这个池子被销毁的时候他会对池子中所有的对 ...
- github远程仓储和本地仓储进行关联
我们使用github作为远程仓储,需要提前注册号一个github账号 由于github仓储和本地仓储之间传输是使用ssh加密的,所以需要一点设置, 1.创建sshkey gitbash执行: ssh ...
- the JSON object must be str, not 'bytes'
{ "ErrorDump": "the JSON object must be str, not 'bytes'", "StatusCode" ...
- HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...
- Mybatis拦截器(插件实现原理)
在mybatis的mybatis.cfg.xml中插入: <plugins> <plugin interceptor="cn.sxt.util.PageIntercepto ...
- [Usaco2013 DEC] Vacation Planning
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4093 [算法] 对于k个枢纽 , 分别在正向图和反向图上跑dijkstra最短路 , ...