【Splay】【启发式合并】hdu6133 Army Formations
题意:给你一颗树,每个结点的儿子数不超过2。每个结点有一个权值,一个结点的代价被定义为将其子树中所有结点的权值放入数组排序后,每个权值乘以其下标的和。让你计算所有结点的代价。
二叉树的条件没有用到。
每个结点开一个Splay,从叶子往上启发式合并上去,可以先bfs一遍确定合并顺序。每一次将Splay大小较小的结点的权值全提取出来塞到较大的里面。
由于权值可能重复出现,所以每个结点记个cnt。
答案统计的时候,就将那个刚塞进去的旋到根,然后答案加上左子树的权值和,再加上(右子树的权值的个数+该结点的cnt)*该结点的权值。
然后将较大儿子的Splay的根丢给父亲。
不必要进行内存回收,通过计算,所开的空间只要达到nlogn即可,实际上100w足够了。
#include<cstdio>
#include<set>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1000000;
ll sum[maxn];
int fa[maxn],val[maxn],c[maxn][2],root,tot,siz[maxn],cnt[maxn];
void Maintain(int x)
{
siz[x]=siz[c[x][0]]+siz[c[x][1]]+cnt[x];
sum[x]=sum[c[x][0]]+sum[c[x][1]]+(ll)val[x]*(ll)cnt[x];
}
void NewNode(int &x,int Fa,int key)
{
x=++tot;
fa[x]=Fa;
c[x][0]=c[x][1]=0;
val[x]=key;
siz[x]=cnt[x]=1;
}
void Rotate(int x,bool flag)
{
int y=fa[x];
c[y][!flag]=c[x][flag];
fa[c[x][flag]]=y;
if(fa[y]){
c[fa[y]][c[fa[y]][1]==y]=x;
}
fa[x]=fa[y];
c[x][flag]=y;
fa[y]=x;
Maintain(y);
Maintain(x);
}
void Splay(int &root,int x,int goal)
{
if(!x){
return;
}
int y;
while((y=fa[x])!=goal){
if(fa[y]==goal){
Rotate(x,c[y][0]==x);
}
else{
if((c[y][0]==x)==(c[fa[y]][0]==y)){
Rotate(y,c[fa[y]][0]==y);
}
else{
Rotate(x,c[y][0]==x);
y=fa[x];
}
Rotate(x,c[y][0]==x);
}
}
Maintain(x);
if(!goal){
root=x;
}
}
int Find(int key,int x)
{
while(c[x][val[x]<key]){
if(val[x]==key){
return x;
}
x=c[x][val[x]<key];
}
return x;
}
void Insert(int &root,int key)
{
int x=Find(key,root);
if(val[x]==key){
++cnt[x];
Splay(root,x,0);
return;
}
NewNode(c[x][val[x]<key],x,key);
Splay(root,c[x][val[x]<key],0);
} int roots[100005];
ll anss[100005];
bool vis[100005];
int e,first[100005],nex[200005],v[200005];
int dep[100005];
void AddEdge(int U,int V){
v[++e]=V;
nex[e]=first[U];
first[U]=e;
}
int T,n,a[100005],b[100005];
bool cmp(const int &a,const int &b){
return siz[roots[a]]>siz[roots[b]];
}
int sons[100005],BI;
void dfs(int U){
for(int i=1;i<=cnt[U];++i){
Insert(roots[sons[1]],val[U]);
int X=Find(val[U],roots[sons[1]]);
Splay(roots[sons[1]],X,0);
if(c[X][0]){
anss[BI]+=sum[c[X][0]];
}
anss[BI]+=(ll)(cnt[X]+(c[X][1] ? siz[c[X][1]] : 0))*(ll)val[U];
}
if(c[U][0]){
dfs(c[U][0]);
}
if(c[U][1]){
dfs(c[U][1]);
}
}
int main(){
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
scanf("%d",&T);
int x,y;
for(;T;--T){
memset(first,0,sizeof(first));
e=0;
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
}
for(int i=1;i<n;++i){
scanf("%d%d",&x,&y);
AddEdge(x,y);
AddEdge(y,x);
}
memset(vis,0,sizeof(vis));
memset(dep,0,sizeof(dep));
memset(anss,0,sizeof(anss));
queue<int>q;
q.push(1);
int t=0;
while(!q.empty()){
int U=q.front(); q.pop(); vis[U]=1;
b[++t]=U;
for(int i=first[U];i;i=nex[i]){
if(!vis[v[i]]){
dep[v[i]]=dep[U]+1;
q.push(v[i]);
}
}
}
for(int i=t;i>=1;--i){
BI=b[i];
int dir_son=0;
for(int j=first[b[i]];j;j=nex[j]){
if(dep[v[j]]>dep[b[i]]){
sons[++dir_son]=v[j];
}
}
if(!dir_son){
NewNode(roots[b[i]],0,a[b[i]]);
anss[b[i]]=a[b[i]];
continue;
}
sort(sons+1,sons+dir_son+1,cmp);
anss[b[i]]=anss[sons[1]];
for(int j=2;j<=dir_son;++j){
dfs(roots[sons[j]]);
}
Insert(roots[sons[1]],a[b[i]]);
int X=Find(a[b[i]],roots[sons[1]]);
Splay(roots[sons[1]],X,0);
if(c[X][0]){
anss[BI]+=sum[c[X][0]];
}
anss[BI]+=(ll)(cnt[X]+(c[X][1] ? siz[c[X][1]] : 0))*(ll)a[b[i]];
roots[b[i]]=roots[sons[1]];
}
for(int i=1;i<=n;++i){
printf("%lld ",anss[i]);
}
puts("");
memset(sum,0,sizeof(ll)*(tot+1));
memset(fa,0,sizeof(int)*(tot+1));
memset(val,0,sizeof(int)*(tot+1));
for(int i=0;i<=tot;++i){
c[i][0]=c[i][1]=0;
}
memset(siz,0,sizeof(int)*(tot+1));
memset(cnt,0,sizeof(int)*(tot+1));
memset(roots,0,sizeof(int)*(tot+1));
// printf("%d\n",tot);
tot=0;
}
return 0;
}
【Splay】【启发式合并】hdu6133 Army Formations的更多相关文章
- 【BZOJ-2809】dispatching派遣 Splay + 启发式合并
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2334 Solved: 1192[Submi ...
- 【BZOJ-2733】永无乡 Splay+启发式合并
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2048 Solved: 1078[Submit][Statu ...
- BZOJ2733 永无乡【splay启发式合并】
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]
2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...
- bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)
这题之前写过线段树合并,今天复习Splay的时候想起这题,打算写一次Splay+启发式合并. 好爽!!! 写了长长的代码(其实也不长),只凭着下午的一点记忆(没背板子...),调了好久好久,过了样例, ...
- 【BZOJ2733】永无乡[HNOI2012](splay启发式合并or线段树合并)
题目大意:给你一些点,修改是在在两个点之间连一条无向边,查询时求某个点能走到的点中重要度第k大的点.题目中给定的是每个节点的排名,所以实际上是求第k小:题目求的是编号,不是重要度的排名.我一开始差点被 ...
- 算法复习——splay+启发式合并(bzoj2733-永无乡)
题目: Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通 ...
- 【BZOJ 2733】【HNOI 2012】永无乡 Splay启发式合并
启发式合并而已啦,, 调试时发现的错误点:insert后没有splay,把要拆开的树的点插入另一个树时没有把ch[2]和fa设为null,找第k大时没有先减k,,, 都是常犯的错误,比赛时再这么粗心就 ...
- 【BZOJ2809】【splay启发式合并】dispatching
Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级. ...
随机推荐
- MongoDB 数据库(1)
数据库 MongoDB (芒果数据库) 数据存储阶段 文件管理阶段 (.txt .doc .xls) 优点 : 数据可以长期保存 可以存储大量的数据 使用简单 缺点 : 数据一致性差 数据查找修改不方 ...
- Eureka服务续约(Renew)源码分析
主要对Eureka的Renew(服务续约),从服务提供者发起续约请求开始分析,通过阅读源码和画时序图的方式,展示Eureka服务续约的整个生命周期.服务续约主要是把服务续约的信息更新到自身的Eurek ...
- ie8下trim失效
1.ie8下使用trim失效 trim可以除去字符串两侧的空白字符,但ie8并不支持 2.解决方案 String.prototype.trim = function () { return this ...
- Django rest framework 限制访问频率(源码分析)
基于 http://www.cnblogs.com/ctztake/p/8419059.html 当用发出请求时 首先执行dispatch函数,当执行当第二部时: #2.处理版本信息 处理认证信息 处 ...
- sunos kernel src
https://github.com/eocallaghan/AuroraUX-SunOS https://github.com/zoyanhui/coroutine-libtask https:// ...
- 在Perl中采用open进行管道操作
在Perl中采用open进行管道操作 http://blog.sina.com.cn/s/blog_4840fe2a0100b8na.html perl exec管道和子进程 http://blog. ...
- Pretrained models for Pytorch (Work in progress)
The goal of this repo is: to help to reproduce research papers results (transfer learning setups for ...
- MySQL取得某一范围随机数(MySQL随机数)
若要在i ≤ R ≤ j 这个范围得到一个随机整数R ,需要用到表达式 FLOOR(i + RAND() * (j – i + 1)). 例如, 若要在7 到 12 的范围(包括7和12)内得到一个随 ...
- ofbiz多表外键关联查询
实现一:Screem.xml 中的 section 里,加 <action>, 加 get-related 实现二:在代码中使用 DynamicViewEntity对象,加入addMemb ...
- vivo手机的坑-禁止微信浏览器网页点击图片,图片会自动放大
这个坑让我郁闷极了,之前我的手机在微信浏览器打开网页点击图片时,啥事也没有 现在,咋回事,变了呢! 现在我打开微信浏览器网页面html,点击一个img标签的图片,图片会自动满屏放大,吓死宝宝了.其他人 ...