树分治。

代码

#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的更多相关文章

  1. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  2. [bzoj1087][scoi2005]互不侵犯king

    题目大意 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上 左下右上右下八个方向上附近的各一个格子,共8个格子. 思路 首先,搜索可以放弃,因为这是一 ...

  3. King's Quest —— POJ1904(ZOJ2470)Tarjan缩点

    King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...

  4. 【状压DP】bzoj1087 互不侵犯king

    一.题目 Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上.下.左.右,以及左上.左下.右上.右下八个方向上附近的各一个格子,共8个格子. I ...

  5. ZOJ 2334 Monkey King

    并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子                       Monkey King ...

  6. 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 ...

  7. BZOJ-1087 互不侵犯King 状压DP+DFS预处理

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...

  8. POJ1364 King

    Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...

  9. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

随机推荐

  1. C# 64位系统中类型所占空间大小

    Boolean   8Byte DateTime 8Byte Decimal  16Byte String 引用地址空间8Bypte Int 4Bypte 类所占空间大小 (byte):各个filed ...

  2. JVM内存状况查看方法和分析工具-jmap

    jmap -heap 27657 Attaching to process ID 27657, please wait... Debugger attached successfully. Serve ...

  3. 常用分类列表wp_list_categories()

    使用: <ul> <?php $args= array( 'depth'=>1, 'orderby'=>id, 'style'=>none ); wp_list_c ...

  4. 用Js的eval解析JSON中的注意点

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eval函数来解析,并且使用jquery的ea ...

  5. 公共POI导出Excel方法--java

    最早开始的时候做过一些数据Excel导出的功能,但是到后期每一次导出都需要写一些差不多类似的代码,稍微研究了一下写了个公共的导出方法. 这里用的是POI,然后写成了一个公共类,传入设置好格式的数据,就 ...

  6. 网络拥塞控制(三) TCP拥塞控制算法

    为了防止网络的拥塞现象,TCP提出了一系列的拥塞控制机制.最初由V. Jacobson在1988年的论文中提出的TCP的拥塞控制由“慢启动(Slow start)”和“拥塞避免(Congestion  ...

  7. .net加载到vb 进程

    .net加载到vb 进程时,总是不能加载进去,什么原因呢? 要尝试三个步骤, 首先调试vb ,没有问题,代码能够调试 然后注册.net的dll,生成tlb文件,生成解决方案,调整附加到进程时的选项. ...

  8. 股票中带有ST和*ST的股票是什么意思啊?一图了解新三板与主板、中小板、创业板制度差异!

    st表示已经亏损超过1年以上的 *st表示连续亏损3年以上,并且有退市风险的,随时可能会“退市”到时候你的钱可能都拿不会来 主板股票代码上海60开头,深圳000开头,通常指大中型企业:中小板002开头 ...

  9. POJ 1028解答

    #include <iostream>#include <cstdio>#include <cmath>#include <stack>#include ...

  10. 第三方Jar上传到Nexus3

    mvn deploy:deploy-file -DgroupId=ESPPaaSService -DartifactId=ESPPaaSService -Dversion=1.0 -Dpackagin ...