【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以外,每名忍者都有且仅有一个上级. ...
随机推荐
- oracle scott用户不存在
scott用户拥有一些基础的数据表,可以供我们练习sql.先执行 alter user scott account unlock; 查看scott用户是否存在 当scott用户不存在,我们就需要在$O ...
- VS开发工具 因插件问题导致 已停止工作 解决办法
解决方案如下:No1. 开始-->所有程序-->Microsoft Visual Studio 2012-->Visual Studio Tools-->VS2012 开发人员 ...
- HTTPS加密通信原理及数字证书系统
https加密通信原理: 公钥私钥成对,公钥公之于众,私钥只有自己知道. 用公钥加密的信息只能由与之相对应的私钥解密. 甲给乙发送数据时,甲先用乙的公钥加密这段数据,再用自己的私钥对这段数据的特征数据 ...
- 64_l4
libnormaliz-devel-3.1.4-2.fc26.i686.rpm 23-May-2017 00:24 31214 libnormaliz-devel-3.1.4-2.fc26.x86_6 ...
- UNIX v6
UNIX v6 http://download.csdn.net/download/u013896535/9106775 https://github.com/chromium/mini_chromi ...
- python近期遇到的一些面试问题(二)
1. 解释什么是栈溢出,在什么情况下可能出现. 栈溢出是由于C语言系列没有内置检查机制来确保复制到缓冲区的数据不得大于缓冲区的大小,因此当这个数据足够大的时候,将会溢出缓冲区的范围.在Python中, ...
- 【模板】BZOJ 3781: 小B的询问 莫队算法
http://www.lydsy.com/JudgeOnline/problem.php?id=3781 N个数的序列,每次询问区间中每种数字出现次数的平方和,可以离线. 丢模板: #include ...
- C++11——Use auto keyword in C++11
版权声明:本文系原创,转载请注明来源. Compile your program with: g++ -std=c++ -o target_name filen_ame.cpp or: g++ -st ...
- C#面向对象(OOP)入门—第一天—多态和继承(方法重载)
面向对象是什么 面向对象是一种基于对象的编程方法,它取代了仅仅依靠方法和流程的编程方式.面向对象的编程语言中,对象(object)其实就是指特定类型.或某个类的实例.面向对象使得编程人员更容易组织和管 ...
- [PAT] 1146 Topological Order(25 分)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...