J. Whistle's New Car
time limit per test

15 seconds

memory limit per test

512 megabytes

input

car.in

output

standard output

Whistle has bought a new car, which has an infinite fuel tank capacity.

He discovered an irregular country since it has n cities and there are exactly n - 1 roads between them, of course, all cities are connected. He is so much clever, he realized that the country is like a rooted tree of n nodes and node 1 is the root. Each city i has only one filling station by which he can fill his car's fuel tank in no more than Xi liter. Whistle liked the country very much, and he wants to know what the most attractive city in the country is. The attractiveness of the city i is defined by how much it’s reachable from other cities, in other words the attractiveness of city is the number of cities j that satisfies these condition:

  • City j is in the subtree of city i (except for city i itself).
  • Whistle will start at city j and will only fill his car’s fuel tank with Xj liters and never fill it again until he reach city i.
  • Whistle should be able to reach city i with non-negative fuel.

He knows the length of every road and that 1 Km will take exactly 1 liter on any road.

As you know, Whistle is very clever, but he is not that good at programming, so he asked you to help him. He wants to know the attractiveness of each city, so that he can decide which city to live in.

Input

The first line of input contains one integer T, the number of test cases.

The next line contains one integer (1 ≤ n ≤ 500, 000), The number of cities in the country.

The next line contains n integers (1 ≤ Xi ≤ 1, 000, 000, 000).

Each one of the next n - 1 line contains three integers ABC (1 ≤ A, B ≤ n and 1 ≤ C ≤ 1, 000, 000, 000), that means there is a road between city A and city B of length C.

Output

For each test case, output a line containing n integers, the attractiveness of each city.

Example
input
1
4
5 10 5 10
1 2 100
2 3 5
3 4 5
output
0 2 1 0
Note

Large I/O files. Please consider using fast input/output methods.

设dis[i]表示从根节点1走到第 i个节点的路径长度。

那么任意的路径u-->v(前提是u是v的祖先,不然要用lca,这里不需要)可以表示为dis[v] - dis[u]

那么如果v能走回到u,则需要a[v] >= dis[v] - dis[u],即是dis[v] - a[v] <= dis[u],其中dis[v] - a[v]是一个确定值。

那么题意转换成,对于每一个节点u,有一个值dis[u],问其子树中,有多少个节点,满足dis[sonNode] - a[sonNode] <= dis[u]的。

这里可以用dfs序,然后用区间第k大的线段树或者分块、离线BIT之类的数据结构维护。

但是注意到dis[]是关于深度递增的,就是越走到下面,dis越大,也就是单调。

那么dfs的时候,用个vector记录一下路径长度和节点id。二分找到第一个祖先,使得满足dis[sonNode] - a[sonNode] <= dis[u]

那么可以知道ans[那个祖先节点 ---- 当前节点的爸爸],这条路径上的贡献都要加1

其实也就是打标记。

和一维差不多,L....R中加上1等价于sum[L] += 1, sum[R + 1] -= 1

这里一样,sum[当前节点的爸爸] += 1, sum[第一个满足的祖先的爸爸] -= 1

不能从上到下打标记,因为儿子数目不确定,而爸爸是确定的。

#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 = + ;
struct Edge {
int u, v, tonext, len;
}e[maxn * ];
int first[maxn], num;
int a[maxn];
void addEdge(int u, int v, int w) {
++num;
e[num].u = u, e[num].v = v, e[num].len = w;
e[num].tonext = first[u];
first[u] = num;
}
int ans[maxn];
vector< pair<LL, int> > vc;
int vis[maxn], DFN;
void dfs(int cur, LL nowLen, int fa) {
ans[fa]++;
int pos = lower_bound(vc.begin(), vc.end(), make_pair(nowLen - a[cur], )) - vc.begin();
pos--; //去到第一个满足的的祖先的爸爸
if (pos >= ) {
ans[vc[pos].second]--;
}
vc.push_back(make_pair(nowLen, cur));
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (v == fa) continue;
dfs(v, nowLen + e[i].len, cur);
ans[cur] += ans[v];
}
vc.pop_back();
}
void work() {
num = ;
++DFN;
memset(first, false, sizeof first);
memset(ans, , sizeof ans);
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) cin >> a[i];
for (int i = ; i <= n - ; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w);
addEdge(v, u, w);
}
dfs(, , );
for (int i = ; i <= n; ++i) {
printf("%d ", ans[i]);
}
printf("\n");
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
freopen("car.in", "r", stdin);
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

Gym - 101147J Whistle's New Car 树上差分的更多相关文章

  1. Codeforces Gym - 101147J Whistle's New Car

    Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He di ...

  2. Gym 101147J Whistle's New Car(dfs)

    https://vjudge.net/problem/Gym-101147J 题意: 有n个城市,每个城市有一个权值,表示在这个城市的加油站可以加多少油. 现在要计算每个城市i,有多少个城市j可以到达 ...

  3. 【树状数组】Gym - 101147J - Whistle's New Car

    题意就是对每个点i,统计在其子树内(不含自身),且depj-depi<=xj的点有多少个. 把点分别按照dep-x和dep进行排序,离线处理, 每次把dep-x小于等于当前dep值的点插入树状数 ...

  4. 【BZOJ-4326】运输计划 树链剖分 + 树上差分 + 二分

    4326: NOIP2015 运输计划 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 703  Solved: 461[Submit][Status] ...

  5. [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  6. 树上差分 (瞎bb) [树上差分][LCA]

    做noip2015的运输计划写了好久好久写不出来   QwQ 于是先来瞎bb一下树上差分    混积分 树上差分有2个常用的功能: (1)记录从点i到i的父亲这条路径走过几次 (2)将每条路径(s,t ...

  7. [填坑]树上差分 例题:[JLOI2014]松鼠的新家(LCA)

    今天算是把LCA这个坑填上了一点点,又复习(其实是预习)了一下树上差分.其实普通的差分我还是会的,树上的嘛,也是懂原理的就是没怎么打过. 我们先来把树上差分能做到的看一下: 1.找所有路径公共覆盖的边 ...

  8. 【NOIP2016】【LCA】【树上差分】【史诗级难度】天天爱跑步

    学弟不是说要出丧题吗>>所以我就研究了1天lca又研究了1天tj然后研究了一天天天爱跑步,终于写了出来.(最后的平均用时为240ms...比学弟快了1倍...) 题意:给你颗树,然后有m个 ...

  9. BZOJ_4238_电压_树上差分+dfs树

    BZOJ_4238_电压_树上差分+dfs树 Description 你知道Just Odd Inventions社吗?这个公司的业务是“只不过是奇妙的发明(Just Odd Inventions)” ...

随机推荐

  1. 基于BASYS2的VHDL程序——数字钟(最终版)

    转载请注明原地址:http://www.cnblogs.com/connorzx/p/3674178.html 调时电路正常工作.一切正常.发现做FPGA还是得从数电的思路思考,设置一个预置使能端,预 ...

  2. 网页兼容性测试(工具使用IETESTER、Firefox、360安全浏览器)

    网页兼容性测试主要是针对不同的浏览器进行的测试.由于用户浏览器的不同,往往都会使我们的网页发生页面样式错乱,图片无法显示等问题.对于前端开发工程师来说,确保代码在各种主流浏览器的各个版本中都能正常显示 ...

  3. hdu-5748 Bellovin(LIS)

    题目链接: Bellovin Time Limit: 6000/3000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others ...

  4. Mac的环境变量

    在终端输入: vim ~/.bash_profile 按i切换到INSERT模式.然后把路径按照下面的格式写进去.然后输入:wq保存退出. export PATH=${PATH}:/Users/Dru ...

  5. Ubuntu 安装mysql和 简单命令操作

    ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server   2. apt-get isntall mysql-clie ...

  6. C#中,当鼠标移动到控件上,动态显示提示内容 ToolTip

    [背景]写了个C#的小程序,其中在Settings窗体中有些配置参数,希望在鼠标移动到对应的配置选项上,动态显示相关的说明解释内容. [解决过程]1.原先就已经知道,有些控件,是带ToolTipTex ...

  7. 二维码扫描极速版2.0.apk

    二维码扫描极速版2.0.apk 百度网盘下载地址: http://pan.baidu.com/s/1o686bGI 二维码扫描极速版 描述 二维码扫描极速版,快速识别二维码中的信息. 简单易用. 提高 ...

  8. How Many Boyfriends

    知乎上看到一个问题,如果一个女人说自己集齐了12个星座的男朋友,那么她已经搞过多少男人了. 先考虑这个问题的最简单版本,如果说该女人每一次和12个星座的男人交往的概率相同. 考虑$dp$ 注意到这个问 ...

  9. 使用Swing组件实现简单的进制转换

    请编写图像界面程序, 用户在第一文本行输入数字, 有三个按钮,分别是计算2进制,8进制,16进制, 点击其中一个按钮,第一个文本行中的数据转换为相应进制的数显示在第二个文本行中. import jav ...

  10. wmware7安装xp错误:虚拟CPU已经入关闭状态

    wm7安装xp错误提示: 虚拟CPU已经入关闭状态.这会造成物理计算机重新启动.这可能是虚拟机操作系统的错误或VMware Workstation软件中的一个配置不正确. 这种情是硬盘模式为AHCI模 ...