题目链接

Problem Description
> Stormtroopers were the assault/policing troops of the Galactic Empire. Dissenting citizens referred to them as bucketheads, a derogatory nickname inspired by the bucket-shaped helmets of stormtroopers. They wore white armor made from plastoid over a black body glove which, in addition to creating an imposing image, was outfitted with a wide array of survival equipment and temperature controls that allowed its wearer to survive in most environments, and were designed to disperse blaster bolt energy. As members of the Stormtrooper Corps, an independent branch that operated under the Imperial Army, stormtroopers represented the backbone of the Imperial Military—trained for total obedience to the command hierarchy, as well as absolute loyalty to Emperor Sheev Palpatine and the Imperial regime. Stormtroopers were trained at Imperial Academies, and used a variety of weapons.
>
> --- Wookieepedia

Though being cruel and merciless in the battlefields, the total obedience to the command hierarchy makes message delivering between Stormtroopers quite inefficient, which finally caused the escape of Luke Skywalker, the destroy of the Death Star, and the collapse of the Galactic Empire.

In particular, the hierarchy of Stormtroopers is defined by a *binary tree*. Everyone in the tree has at most two direct subordinates and exactly one direct leader, except the first (numbered 1) Stormtrooper, who only obey the order of the Emperor.

It has been a time-consuming task for the Stormtroopers to input messages into his own log system. Suppose that the i-th Stormtrooper has a message of length ai, which means that it costs ai time to input this message into a log system. Everyone in the hierarchy has the mission of collecting all messages from his subordinates (a direct or indirect children in the tree) and input thses messages and his own message into his own log system.

Everyone in the hierarchy wants to optimize the process of inputting. First of all, everyone collects the messages from all his subordinates. For a Stormtrooper, starting from time 0, choose a message and input it into the log system. This process proceeds until all messages from his subordinates and his own message have been input into the log system. If a message is input at time t, it will induce t units of penalty.

For every Stormtrooper in the tree, you should find the minimum penalty.

 
Input
The first line of the input contains an integer T, denoting the number of test cases.

In each case, there are a number n (1≤n≤105) in the first line, denoting the size of the tree.

The next line contains n integers, the i-th integer denotes ai (0≤ai≤108), the i-th Stormtrooper’s message length.

The following n−1 lines describe the edges of the tree. Each line contains two integers u,v (1≤u,v≤n), denoting there is an edge connecting u and v.

 
 
Output
For each test case, output n space-separated integers in a line representing the answer. i-th number is the minimum penalty of gathering messages for i-th Stormtrooper.
 
 
Sample Input
1
3
1 2 3
1 2
2 3
 
 
Sample Output
10 7 3
 
 
题意:该校出题题目太长了,而且表达的不是很清楚,太难理解了。 该题简而言之:有一个由 n 个节点构成的二叉树,每个节点上有一个权值wi,求所有节点的一个计算值。对于某个节点来说,以它为根节点的子树的所有的节点的值构成的序列的前缀和的和,即为这个节点的计算值,对于每个节点求最小的计算值,那么很明显贪心,把这个序列从小到大排序,然后再就算前缀和的和。
 
思路:官方题解说是启发式合并,我不懂什么是启发式,我看了别人写的代码后,我的理解是对于左右子树合并时,把小的子树合并到大的子树中去,这样复杂度较小,我觉得这个过程就是应用了启发式思想。 本题思路没什么好说的,只想简单说一下合并,本题中的计算是由树状数组完成的,对于每次在已有节点上添加一个节点x时,这些数的前缀和的和变化为:tmp+=大于w[x]的数个数*w[x]+w[x]+小于w[x]的所有数的和。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const int N=1e5+;
int w[N];
int val[N],id[N];
int sz[N],lson[N],rson[N],tot;
vector<int>e[N];
LL num[N],sum[N];
LL ans[N],tmp; int calSize(int now,int pre)
{
sz[now]=;
for(int j=;j<e[now].size();j++)
{
int to=e[now][j];
if(to==pre) continue;
if(!lson[now]) lson[now]=to;
else rson[now]=to;
sz[now]+=calSize(to,now);
}
if(lson[now]&&rson[now]&&sz[lson[now]]>sz[rson[now]])
swap(lson[now],rson[now]);
return sz[now];
}
int lowbit(int x)
{
return x&(-x);
}
void update(LL *a,int x,int y)
{
while(x<=tot)
{
a[x]+=(LL)y;
x+=lowbit(x);
}
}
LL query(LL *a,int x)
{
LL r=;
while(x)
{
r+=a[x];
x-=lowbit(x);
}
return r;
} void add(int x)
{
tmp+=(query(num,tot)-query(num,id[x]))*(LL)w[x]+(LL)w[x];
tmp+=query(sum,id[x]);
update(num,id[x],);
update(sum,id[x],w[x]);
}
void del(int x)
{
update(num,id[x],-);
update(sum,id[x],-w[x]);
tmp-=(query(num,tot)-query(num,id[x]))*(LL)w[x]+(LL)w[x];
tmp-=query(sum,id[x]);
}
void cle(int x)
{
del(x);
if(lson[x]) cle(lson[x]);
if(rson[x]) cle(rson[x]);
}
void validate(int x)
{
add(x);
if(lson[x]) validate(lson[x]);
if(rson[x]) validate(rson[x]);
}
void dfs(int now)
{
if(!lson[now])
{
ans[now]=(LL)w[now];
add(now);
return ;
}
dfs(lson[now]);
if(rson[now])
{
cle(lson[now]);
dfs(rson[now]);
validate(lson[now]);
}
add(now);
ans[now]=tmp;
}
int main()
{
int T; cin>>T;
while(T--)
{
int n; scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&w[i]);
val[i]=w[i];
e[i].clear();
num[i]=sum[i]=;
lson[i]=rson[i]=;
}
sort(val+,val+n+);
tot=unique(val+,val+n+)-val-;
for(int i=;i<=n;i++)
{
id[i]=lower_bound(val+,val+tot+,w[i])-val;
}
for(int i=;i<n;i++)
{
int u,v; scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
calSize(,-);
tmp=;
dfs();
for(int i=;i<=n;i++)
printf("%lld ",ans[i]);
puts("");
}
return ;
}

hdu 6133---Army Formations(启发式合并+树状数组)的更多相关文章

  1. HDU 5997 rausen loves cakes(启发式合并 + 树状数组统计答案)

    题目链接  rausen loves cakes 题意  给出一个序列和若干次修改和查询.修改为把序列中所有颜色为$x$的修改为$y$, 查询为询问当前$[x, y]$对应的区间中有多少连续颜色段. ...

  2. #6041. 「雅礼集训 2017 Day7」事情的相似度 [set启发式合并+树状数组扫描线]

    SAM 两个前缀的最长后缀等价于两个点的 \(len_{lca}\) , 题目转化为求 \(l \leq x , y \leq r\) , \(max\{len_{lca(x,y)}\}\) // p ...

  3. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  4. hdu 6203 ping ping ping(LCA+树状数组)

    hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...

  5. hdu 1394 Minimum Inversion Number(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...

  6. HDU 5792 World is Exploding (树状数组)

    World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

  7. HDU 5773 The All-purpose Zero(树状数组)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5773 [题目大意] 给出一个非负整数序列,其中的0可以替换成任意整数,问替换后的最长严格上升序列长 ...

  8. POJ 3928 &amp; hdu 2492 &amp; Uva1428 PingPong 【树状数组】

    Ping pong                                                   Time Limit: 2000/1000 MS (Java/Others)   ...

  9. BZOJ.3653.谈笑风生(长链剖分/线段树合并/树状数组)

    BZOJ 洛谷 \(Description\) 给定一棵树,每次询问给定\(p,k\),求满足\(p,a\)都是\(b\)的祖先,且\(p,a\)距离不超过\(k\)的三元组\(p,a,b\)个数. ...

随机推荐

  1. (转)JAVA堆栈操作

    java.util.Queue用法(是util中的一个接口) 队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行 ...

  2. AC自动机总结及板子(不带指针)

    蒟蒻最近想学个AC自动机简直被网上的板子搞疯了,随便点开一个都是带指针的,然而平时用到指针的时候并不多,看到这些代码也完全是看不懂的状态.只好在大概理解后自己脑补(yy)了一下AC自动机的代码,居然还 ...

  3. centOS(redhat/oracle linux更换语言

    编辑/etc/sysconfig/i18n将LANG=”zh_CN.UTF-8″ 改为 LANG=”en_US.UTF-8″ 或其他语言中文乱码将LANG=”zh_CN.UTF-8″改为LANG=”z ...

  4. Vue项目搭建完整剖析全过程

    Vue项目搭建完整剖析全过程 项目源码地址:https://github.com/ballyalex 有帮助的话就加个星星呗~!  项目技术栈:vue+webpack+bower+sass+axios ...

  5. RabbitMQ入门-高效的Work模式

    扛不住的Hello World模式 上篇<RabbitMQ入门-从HelloWorld开始>介绍了RabbitMQ中最基本的Hello World模型.正如其名,Hello World模型 ...

  6. hdu--1072--Nightmare(bfs回溯)

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  7. mysql忘记密码,修改密码重新安装的一些问题

    前言 想要装cobra,却意外发现mysql连接失败,命令行连一下发现无论怎么样都连不上了. 我能想到的密码都用上了,糟糕!看来只能修改密码,或者重装了. 最后是重装搞定的,当然也发现了正确的修改密码 ...

  8. securecrt鼠标右键的配置

    在使用的secureCRT的情况下,选择好要复制的内容后点击右键时,会直接在命令行粘贴内容.如果不想右键直接粘贴而是跳出菜单选择,就要进行设置了. 方法如下: options->Gloabal ...

  9. python_装饰器_语法糖

    什么是高阶函数? -- 把函数名当做参数传给另外一个函数,在另外一个函数中通过参数调用执行 #!/usr/bin/python3 __author__ = 'beimenchuixue' __blog ...

  10. 【NO.10】Jmeter - 一个完整的录制脚本的过程

    上1篇介绍了"使用Jmeter对一个接口地址或者一个页面地址执行N次请求",也就是你自己干了一件从"零"开始的事情. 那么这1篇介绍"如何使用Jmeter录制'访问一个接口地址或者一个页面地址'的脚本 ...