hdu5314 Happy King
树分治。
代码
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define N 300010
using namespace std;
int dp,pre[N],p[N],tt[N],cnt,q[N];
LL c[N],ans;
int n,i,a,b,w[N],vis[N],s[N],tmp,d,tot1,tot2,v[N];
struct g{
int a,b;
}A[N],B[N];
bool cmp(g x,g y)
{
return x.b<y.b;
}
void link(int x,int y)
{
dp++;pre[dp]=p[x];p[x]=dp;tt[dp]=y;
}
int lowbit(int x)
{
return x&(-x);
}
void cc(int x,int w)
{
while (x<=n)
{
c[x]+=w;
x=x+lowbit(x);
}
}
LL getsum(int x)
{
LL ans=0;
while (x>0)
{
ans=ans+c[x];
x=x-lowbit(x);
}
return ans;
}
void dfs(int x,int fa,int Min,int Max)
{
int i;
Min=min(Min,w[x]);
Max=max(Max,w[x]);
if (Max-Min<=d)
{
tot1++;A[tot1].a=Min;A[tot1].b=Max;
tot2++;B[tot2]=A[tot1];
}
else
return;
i=p[x];
while (i)
{
if ((!vis[tt[i]])&&(tt[i]!=fa))
dfs(tt[i],x,Min,Max);
i=pre[i];
}
}
void getroot(int x,int fa,int sum)
{
int i,flag=0;
i=p[x];s[x]=1;
while (i)
{
if ((!vis[tt[i]])&&(tt[i]!=fa))
{
getroot(tt[i],x,sum);
s[x]+=s[tt[i]];
if (s[tt[i]]>sum/2) flag=1;
}
i=pre[i];
}
if (sum-s[x]>sum/2) flag=1;
if (!flag) tmp=x;
}
int ef(int x)
{
int l,r,m;
l=1;r=n;
while (l<=r)
{
m=(l+r)>>1;
if (v[m]>x)
r=m-1;
else
l=m+1;
}
return r;
}
void work(int x,int sum)
{
int root,i,ta,tb;
getroot(x,0,sum);
root=tmp;
i=p[root];
vis[root]=1;
while (i)
{
if (!vis[tt[i]])
{
if (s[tt[i]]>s[root])
work(tt[i],sum-s[root]);
else
work(tt[i],s[tt[i]]);
}
i=pre[i];
} tot1=1;
A[tot1].a=w[root];
A[tot1].b=w[root];
i=p[root];
while (i)
{
if (!vis[tt[i]])
{
tot2=0;
dfs(tt[i],0,w[root],w[root]);
sort(B+1,B+1+tot2,cmp);
cnt=0;
for (int j=1;j<=tot2;j++)
{
ta=ef(B[j].b);
tb=ef(B[j].b-d-1);
ans=ans-(getsum(ta)-getsum(tb));
ta=ef(B[j].a);cnt++;q[cnt]=ta;
cc(ta,1);
} for (int j=1;j<=cnt;j++)
cc(q[j],-1);
}
i=pre[i];
}
sort(A+1,A+1+tot1,cmp);
cnt=0;
for (int j=1;j<=tot1;j++)
{
ta=ef(A[j].b);
tb=ef(A[j].b-d-1);
ans=ans+(getsum(ta)-getsum(tb));
ta=ef(A[j].a);cnt++;q[cnt]=ta;
cc(ta,1);
}
for (int j=1;j<=cnt;j++)
cc(q[j],-1);
vis[root]=0;
}
int main()
{
int test;
scanf("%d",&test);
while (test)
{
test--;
dp=0;
memset(p,0,sizeof(p));
scanf("%d%d",&n,&d);
for (i=1;i<=n;i++)
{
scanf("%d",&w[i]);
v[i]=w[i];
}
sort(v+1,v+1+n);
for (i=1;i<=n-1;i++)
{
scanf("%d%d",&a,&b);
link(a,b);
link(b,a);
}
ans=0;
work(1,n);
printf("%I64d\n",ans*2);
}
}
hdu5314 Happy King的更多相关文章
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- [bzoj1087][scoi2005]互不侵犯king
题目大意 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上 左下右上右下八个方向上附近的各一个格子,共8个格子. 思路 首先,搜索可以放弃,因为这是一 ...
- King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...
- 【状压DP】bzoj1087 互不侵犯king
一.题目 Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上.下.左.右,以及左上.左下.右上.右下八个方向上附近的各一个格子,共8个格子. I ...
- ZOJ 2334 Monkey King
并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子 Monkey King ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout
K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...
- BZOJ-1087 互不侵犯King 状压DP+DFS预处理
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...
- POJ1364 King
Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
随机推荐
- 简述AFN(AFNetWorking 2.X)的实现分析和简单使用【转】
[转载] Axc注:版本为AFNetWorking 2.6 3.0需要修改大部分对象 一:声明本 人以前一直是使用ASI网络请求类库的.最近才开始了解使用AFNetworing这个类库.网上也肯 ...
- HBase学习笔记-基础(一)
HBase版本:0.97 1.Get Gets实在Scan的基础上实现的. 2.联合查询(Join) HBase是否支持联合是一个网上常问问题.简单来说 : 不支持.至少不像传统RDBMS那样支持. ...
- C#判断文件是复制还是剪切
private void button1_Click(object sender, EventArgs e){IDataObject vDataObject = Clipboard.GetDataOb ...
- const 修饰函数
At the very first ,I got a problem . Vector Vector::operator+(const Vector &v)const{ return Vect ...
- 对象(List<T>)的序列化和反序列化
本文描述将对象(List<T>)序列化到 XML 文档中和从 XML 文档中反序列化为对象(List<T>). 命名空间: System.Xml.Serialization 程 ...
- Mac下安装和配置mongoDB
mac下的mongodb下载安装比较简单,主要有两种方式,一种是下载压缩包解压,另一种是通过npm或者homebrew命令安装,这里就不赘述了, 复杂的在于mongodb运行环境的配置(若未配置运行环 ...
- (转帖) java内存分配分析/栈内存、堆内存
http://blog.csdn.net/qh_java/article/details/9084091
- 去除undefined和末尾逗号及把字符串数字转成数字数组的方法
function removeundefined(str){ var v=new Array(),b=""; var tmp=fil(str); for(var i=0;i ...
- C# winForm程序开机启动和托盘显示 (转http://blog.csdn.net/xinyue3054/article/details/6599508)
这段时间一直进行cs项目,故整理下开机自动运行.托盘.显示.隐藏. (1).在窗口上点击关闭按钮或者最小化时将托盘显示: (2).双击托盘图标显示窗口: (3).右键点击托盘图标提供三个菜单选项,“退 ...
- web页面的适配问题
一个web页面既要在宽屏上显示,又要在窄屏上显示,既要在电脑上显示,又要在手机上显示,这个适配问题相当的麻烦. 其实解决电脑与手机的适配问题,一般有两个思路:一个是做判断,根据不同条件在css和js做 ...