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 ...
随机推荐
- 【转】一篇很全面的freemarker教程---有空慢慢实践
FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1,文本:直接输出的部分 2,注释:<#-- ... -->格式部分,不会输 ...
- nRF51822之pstorage使用摘要
https://devzone.nordicsemi.com/question/15271/how-can-i-write-10kb-of-data-to-internal-flash/?answer ...
- linux OSlab4 添加自定义系统调用
http://blog.csdn.net/ly01kongjian/article/details/8947285 http://www.cnblogs.com/hoys/archive/2011/0 ...
- Java迭代 : Iterator和Iterable接口
从英文意思去理解 Iterable :故名思议,实现了这个接口的集合对象支持迭代,是可迭代的.able结尾的表示 能...样,可以做.... Iterator: 在英语中or 结尾是都是表示 .. ...
- C/C++ 中长度为0的数组
参考文献:http://blog.csdn.net/zhaqiwen/article/details/7904515 近日在看项目中的框架代码时,发现了了一个奇特的语法:长度为0的数组例如 uint8 ...
- php--城市分类
效果图:
- java对象中继承和变量初始化顺序浅析
先上例子代码 public class F { int age = 5; public F() { print(); } public void print() { System.out.printl ...
- 算法训练 Hanoi问题
算法训练 Hanoi问题 时间限制:1.0s 内存限制:512.0MB 问题描述 如果将课本上的Hanoi塔问题稍做修改:仍然是给定N只盘子,3根柱子,但是允许每次最多移动相邻的 ...
- [LeetCode]题解(python):118-Excel Sheet Column Title
题目来源 https://leetcode.com/problems/excel-sheet-column-title/ Given a positive integer, return its co ...
- Using dbms_shared_pool.purge to remove a single task from the library cache
我们都知道可是使用 alter system flush shared_pool 来清除shared pool 信息,当时不能指定清除某个对象.因为在系统繁忙的时侯 使用 alter system f ...