题意:给你一颗树,每个结点的儿子数不超过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的更多相关文章

  1. 【BZOJ-2809】dispatching派遣 Splay + 启发式合并

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2334  Solved: 1192[Submi ...

  2. 【BZOJ-2733】永无乡 Splay+启发式合并

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2048  Solved: 1078[Submit][Statu ...

  3. BZOJ2733 永无乡【splay启发式合并】

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]

    2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...

  5. bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)

    这题之前写过线段树合并,今天复习Splay的时候想起这题,打算写一次Splay+启发式合并. 好爽!!! 写了长长的代码(其实也不长),只凭着下午的一点记忆(没背板子...),调了好久好久,过了样例, ...

  6. 【BZOJ2733】永无乡[HNOI2012](splay启发式合并or线段树合并)

    题目大意:给你一些点,修改是在在两个点之间连一条无向边,查询时求某个点能走到的点中重要度第k大的点.题目中给定的是每个节点的排名,所以实际上是求第k小:题目求的是编号,不是重要度的排名.我一开始差点被 ...

  7. 算法复习——splay+启发式合并(bzoj2733-永无乡)

    题目: Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通 ...

  8. 【BZOJ 2733】【HNOI 2012】永无乡 Splay启发式合并

    启发式合并而已啦,, 调试时发现的错误点:insert后没有splay,把要拆开的树的点插入另一个树时没有把ch[2]和fa设为null,找第k大时没有先减k,,, 都是常犯的错误,比赛时再这么粗心就 ...

  9. 【BZOJ2809】【splay启发式合并】dispatching

    Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级. ...

随机推荐

  1. 爬虫--BeautifulSoup

    什么是BeautifulSoup? BeautifulSoup支持的一些解析库 基本使用 from bs4 import BeautifulSoup html =""" ...

  2. ctsc&apio2018八日游

    day0: 早就知道自己是打酱油的..早就做好了打铁的准备.. Q:那你来干嘛 A:当然是来玩啊!!玩啊!啊!! emmmmm 抱着半期考不及格的卷子瑟瑟发抖地上了飞机. day1:报道!当然还有在宾 ...

  3. AGC025简要题解

    AGC025简要题解 B RGB Coloring 一道简单题,枚举即可. C Interval Game 考虑可以进行的操作只有两种,即左拉和右拉,连续进行两次相同的操作是没有用的. 左拉时肯定会选 ...

  4. 【DeepLearning学习笔记】Coursera课程《Neural Networks and Deep Learning》——Week2 Neural Networks Basics课堂笔记

    Coursera课程<Neural Networks and Deep Learning> deeplearning.ai Week2 Neural Networks Basics 2.1 ...

  5. 自己动手实现arm函数栈帧回溯【转】

    转自:http://blog.csdn.net/dragon101788/article/details/18668505 内核版本:2.6.14 glibc版本:2.3.6 CPU平台:arm gl ...

  6. C++之容器

    容器,迭代器与容器适配器 所谓容器,即是将最常运用的一些数据结构(data structures)用类模板实现出来,用于容纳特定类型的对象.根据数据在容器中排列的特性,容器可概分为序列式(sequen ...

  7. 算法题之找出数组里第K大的数

    问题:找出一个数组里面前K个最大数. 解法一(直接解法): 对数组用快速排序,然后直接挑出第k大的数.这种方法的时间复杂度是O(Nlog(N)).N为原数组长度. 这个解法含有很多冗余,因为把整个数组 ...

  8. C语言比较巧妙的字符串分割程序

    在解析字符串时,能够解析的给出每个字符串的长度.内容.以及每个字符串的第一个字符的地址. short i; ; //切割之后的字符串的个数 ,ItemLen[],Idx[], ThCommandLen ...

  9. 我的新博客地址http://xxxbw.github.io/

    最近在学github,在github搭了个博客,以后也会使用另外一个博客.有兴趣的小伙伴可以看看~ 地址:http://xxxbw.github.io/

  10. 下载安装go插件包报错fatal: unable to access 'https://github.com/golang/tools.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054

    使用git命令来给vscode安装go插件的时候报错,如下: $ git clone https://github.com/golang/tools.git tools Cloning into 't ...