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 ...
随机推荐
- API Copy Big FIles
public class ApiCopyFile { private const int FO_COPY = 0x0002; private const int FOF_ALLOWUNDO = 0x0 ...
- Android网络编程(三)Volley使用方法全解析
相关文章 Android网络编程(一)HTTP协议原理 Android网络编程(二)HttpClient与HttpURLConnection 前言 Volley想必许多人都用过,为了建立网络编程的知识 ...
- Linux IO 多路复用是什么意思?
写在前面:本文整理于知乎,原文链接为http://www.zhihu.com/question/32163005/answer/55772739,作者:罗志宇 再次向作者表示感谢~~ 假设你是一个机场 ...
- anaconda中新rdkit安装
1. 执行 conda create -c rdkit -n my-rdkit-env rdkit 该步骤经测试发现需FQ,而模拟器无法完成FQ(至少我不知道方法), 因此在本机上配置好环境后复制粘贴 ...
- jQuery 1.x and 2.x , which is better?
1. jQuery 1.x和2.x的区别 或者可以说是jQuery 2.x有什么新特征? jQuery官方发布2.x原话 不再支持IE6/7/8,如果在IE9/10里只用“兼容性视图”模式也将会受到影 ...
- POJ2533 Longest Ordered Subsequence —— DP 最长上升子序列(LIS)
题目链接:http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 6 ...
- YTU 1099: Minesweeper
1099: Minesweeper 时间限制: 1 Sec 内存限制: 64 MB 提交: 180 解决: 98 题目描述 Minesweeper Have you ever played Min ...
- 【SCOI 2005】 扫雷
[题目链接] 点击打开链接 [算法] 只要第一行第一个数确定了,后面的数也都确定了 递推两遍即可 [代码] #include<bits/stdc++.h> using namespace ...
- c#截屏功能的实现
using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Ru ...
- 使用css borer实现图层蒙版效果
需要js 思路:假设目标元素是target.在外层定义元素宽高等于target,通过border设置元素铺满整个文档,设置border的透明图,实现蒙版,在元素的内部设置子元素,宽高100%;设置圆角 ...