Codeforces Gym - 101147J Whistle's New Car
Discription
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 - 1roads 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 Xjliters 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 A, B, C (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
1
4
5 10 5 10
1 2 100
2 3 5
3 4 5
0 2 1 0
Note
Large I/O files. Please consider using fast input/output methods.
(为什么是文件输入标准输出hhhh,被坑了好久。。。)
每个点维护一个小根堆,往树上父亲合并的时候要先把这个堆都打个 -val_to_fa 的标记。因为涉及到合并和打标机,所以我们写一下左偏树就好啦。
最后每个点的答案就是 这个点的左偏树大小-1。
这个题还有树剖做法,,,虽然很好想(直接考虑每个点向上的影响就行了),但是因为跑的太慢而被我的可并堆艹爆hhhhh
于是我就成了GYM上第二快的人了hhhh(第一是个丧病各种写宏的毒瘤人士hhh)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=500005;
int to[maxn*2],ne[maxn*2],val[maxn*2],num;
int siz[maxn],hd[maxn],L[maxn],ans[maxn];
int n,T,f[maxn],lc[maxn],rc[maxn];
ll W[maxn],tag[maxn];
inline void add(int x,int y,int z){ to[++num]=y,ne[num]=hd[x],hd[x]=num,val[num]=z;}
inline int read(){
int x=0; char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return x;
}
void Wt(int x){ if(x>=10) Wt(x/10); putchar(x%10+'0');}
inline void init(){ num=0,memset(hd,0,sizeof(hd));}
inline void update(int x,ll y){ tag[x]+=y,W[x]+=y;}
inline void pushdown(int x){
if(tag[x]){
if(lc[x]) update(lc[x],tag[x]);
if(rc[x]) update(rc[x],tag[x]);
tag[x]=0;
}
} int merge(int x,int y){
if(!x||!y) return x+y;
pushdown(x),pushdown(y); if(W[x]>W[y]) swap(x,y);
rc[x]=merge(rc[x],y),f[rc[x]]=x;
if(L[rc[x]]>L[lc[x]]) swap(lc[x],rc[x]);
L[x]=L[rc[x]]+1,siz[x]=siz[lc[x]]+siz[rc[x]]+1; return x;
} int DEL(int x){
pushdown(x),f[lc[x]]=f[rc[x]]=0;
return merge(lc[x],rc[x]);
} int dfs(int x,int fa){
int root=x,TO;
for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa){
TO=dfs(to[i],x),update(TO,-val[i]);
while(W[TO]<0) TO=DEL(TO);
root=merge(root,TO);
} ans[x]=siz[root]-1;
return root;
} int main(){
freopen("car.in","r",stdin);
// freopen("data.out","w",stdout);
scanf("%d",&T);
while(T--){
int uu,vv,ww;
init(),scanf("%d",&n);
for(int i=1;i<=n;i++) W[i]=read(),tag[i]=lc[i]=rc[i]=L[i]=f[i]=0,siz[i]=1;
for(int i=1;i<n;i++) uu=read(),vv=read(),ww=read(),add(uu,vv,ww),add(vv,uu,ww);
dfs(1,-1);
for(int i=1;i<=n;i++) Wt(ans[i]),putchar(' ');
puts("");
}
return 0;
}
Codeforces Gym - 101147J Whistle's New Car的更多相关文章
- Gym - 101147J Whistle's New Car 树上差分
J. Whistle's New Car time limit per test 15 seconds memory limit per test 512 megabytes input car.in ...
- Gym 101147J Whistle's New Car(dfs)
https://vjudge.net/problem/Gym-101147J 题意: 有n个城市,每个城市有一个权值,表示在这个城市的加油站可以加多少油. 现在要计算每个城市i,有多少个城市j可以到达 ...
- 【树状数组】Gym - 101147J - Whistle's New Car
题意就是对每个点i,统计在其子树内(不含自身),且depj-depi<=xj的点有多少个. 把点分别按照dep-x和dep进行排序,离线处理, 每次把dep-x小于等于当前dep值的点插入树状数 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
随机推荐
- 集群环境(session多服务器共享的方案梳理)
目前业界解决session共享的几种思路,我总结如下: 第一种办法:把原来存储在服务器磁盘上的session数据存储到客户端的cookie中去. 这样子,就不需要涉及到数据共享了.a客户端请求的时候, ...
- JAVA基础篇—抽象类,抽象方法
class Shape package com.shape; public abstract class Shape { double area;// double per;// String col ...
- arm的开发工具
网上有free的ide可以开发arm cortex的芯片,可以参考List of ARM Cortex-M development tools,Wikipedia,里面有emIDE,embitz等,虽 ...
- Linux数据库忘记密码-修改方法
一.拥有原来的myql的root的密码: 方法一:在mysql系统外,使用mysqladmin# mysqladmin -u root -p password "test123"E ...
- CF878D D. Magic Breeding bitset
D. Magic Breeding time limit per test 4 seconds memory limit per test 1024 megabytes input standard ...
- oracle无参数和带参数的存储过程实例
SQL中调用存储过程语句:call procedure_name(); 注:调用时”()”是不可少的,无论是有参数还是无参数. 定义对数据库存储过程的调用时1.无参数存储过程:{call proced ...
- 【转】Unicode utf8等编码类型的原理
原文地址http://www.cnblogs.com/daxiong2014/p/4768681.html Unicode utf8等编码类型的原理 1.ASCII码 我们知道,在计算机内部,所有的 ...
- iOS--app自定义相册--创建相簿,存储图片到手机
我们在APP中点击照片,都会显示出大图,然后在大图的上面会有个保存照片的按钮,照片直接保存到了系统的相册中,但是因为公司产品的需要,让你创建和APP同名的相册保存在里面,那么就对了,可以看下具体的代码 ...
- 树状数组--前n项和;
树状数组是和线段树类似的数据结构,基本上树状数组可以做的线段树都可以做: 树状数组就是一个数组,在信息记录上有一些特点,以动态求前n项和为例:可以改变数组的某一个元素,求前n项和: 数组tree[ i ...
- C# 时间与时间戳互转 13位|13位時間戳与日期换转
这里直接上代码 懂C# 的程序猿 一看便知道如何使用的... /// <summary> /// 将Unix时间戳转换为DateTime类型时间 /// </summary> ...