hdu 6133---Army Formations(启发式合并+树状数组)
>
> --- 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.
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.
#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(启发式合并+树状数组)的更多相关文章
- HDU 5997 rausen loves cakes(启发式合并 + 树状数组统计答案)
题目链接 rausen loves cakes 题意 给出一个序列和若干次修改和查询.修改为把序列中所有颜色为$x$的修改为$y$, 查询为询问当前$[x, y]$对应的区间中有多少连续颜色段. ...
- #6041. 「雅礼集训 2017 Day7」事情的相似度 [set启发式合并+树状数组扫描线]
SAM 两个前缀的最长后缀等价于两个点的 \(len_{lca}\) , 题目转化为求 \(l \leq x , y \leq r\) , \(max\{len_{lca(x,y)}\}\) // p ...
- hdu 5869 区间不同GCD个数(树状数组)
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- hdu 6203 ping ping ping(LCA+树状数组)
hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...
- hdu 1394 Minimum Inversion Number(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...
- HDU 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- HDU 5773 The All-purpose Zero(树状数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5773 [题目大意] 给出一个非负整数序列,其中的0可以替换成任意整数,问替换后的最长严格上升序列长 ...
- POJ 3928 & hdu 2492 & Uva1428 PingPong 【树状数组】
Ping pong Time Limit: 2000/1000 MS (Java/Others) ...
- BZOJ.3653.谈笑风生(长链剖分/线段树合并/树状数组)
BZOJ 洛谷 \(Description\) 给定一棵树,每次询问给定\(p,k\),求满足\(p,a\)都是\(b\)的祖先,且\(p,a\)距离不超过\(k\)的三元组\(p,a,b\)个数. ...
随机推荐
- 使用JDBC技术连接数据库(附源码)--JAVA的简单应用
一.创建数据库(以mysql数据库为例) mysql数据库的下载安装与配置 -可参考博主之前的随笔:Windows平台下搭建MySQL数据库 创建wxb数据库-create database wxb; ...
- 中国(北方)大学生程序设计训练赛(第二周) (A B D G)
比赛链接 A题是KMP,先把A拼接到B的后面,然后利用next数组的意义(包括其具体含义,以及失配时的应用),得到ans #include<bits/stdc++.h> using nam ...
- [信息安全] 3.HTTPS工作流程
[信息安全]系列博客:http://www.cnblogs.com/linianhui/category/985957.html 0. 简单回顾 在前面两篇博客中介绍了密码相关的一些基本工具,包括(对 ...
- [luogu]P1352 没有上司的舞会[树形DP]
本Lowbee第一次写树形DP啊,弱...一个变量写错半天没看出来...... 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点 ...
- Multimodal —— 看图说话(Image Caption)任务的论文笔记(三)引入视觉哨兵的自适应attention机制
在此前的两篇博客中所介绍的两个论文,分别介绍了encoder-decoder框架以及引入attention之后在Image Caption任务上的应用. 这篇博客所介绍的文章所考虑的是生成captio ...
- (转)volatile关键字
Java线程:volatile关键字 Java™ 语言包含两种内在的同步机制:同步块(或方法)和 volatile 变量.这两种机制的提出都是为了实现代码线程的安全性.其中 Volatile 变量 ...
- HTML随笔2
1. 快捷键: * div*8 + tab键 - 可快捷出现八个div标签. * 标签名."名称" + tab键 - 可快速出现类名为"名称"的该标签,如果不加 ...
- AngularJS $compile动态生成html
angular.module('app') .directive('compile', function ($compile) { return function (scope, element, a ...
- 封装自己的Ajax框架
Ajax技术就是利用javascript和xml实现异步交互的功能. 首先先来介绍一下Ajax相关知识点 一.Ajax对象的创建 1.创建Ajax对象的方式 a.第一种方式是针对IE浏览器 b.第二种 ...
- FaceRank,最有趣的 TensorFlow 入门实战项目
FaceRank,最有趣的 TensorFlow 入门实战项目 TensorFlow 从观望到入门! https://github.com/fendouai/FaceRank 最有趣? 机器学习是不是 ...