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 ...
随机推荐
- libopencv_highgui.a(window_gtk.cpp.o): undefined reference to symbol 'g_type_check_instance_is_a'
libopencv_highgui.a(window_gtk.cpp.o): undefined reference to symbol 'g_type_check_instance_is_a' 尝试 ...
- PDO知识
PDO: 一.含义: 数据访问抽象层 二.作用 :通过PDO能够访问其它的数据库 三. 用法: 1.造对象 ①$dsn="mysql:dbname=zz;host=localhost&quo ...
- Spark学习笔记(一)
1.调度 分为FIFO和FAIR两种模式 创建调度池:sc.setLocalProperty("spark.scheduler.pool", "pool6") ...
- POJ1326问题描述
Description Mileage program of ACM (Airline of Charming Merlion) is really nice for the travelers fl ...
- python中几个常用的算术函数
1.lambda函数(匿名函数) lambda函数使用方式:lambda[参数1,参数2....]:表达式,列表 实例如下: lambda x : x * 2,[1,2,3,4] lambda 2.r ...
- 30天,O2O速成攻略【8.15济南站】
活动概况 时间:2015年08月15日13:30-16:30 地点:山东大学凤岐茶社(山东大学中心校区北门18号楼东连廊一层) 主办:APICloud.蚁巡 网址:www.apicloud.com 费 ...
- Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)
<bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...
- Android界面实现----PagerTabStrip绚丽的滑动标签
在ViewPager这种可以滑动的控件上,总是有很多的文章可以做.Android自带的控件,实现一个指示器,这个控件,就是support-v4包里面的PagerTabStrip控件. 首先,我们先看一 ...
- [转载]WGS84坐标与Web墨卡托坐标互转
//经纬度转Wev墨卡托 dvec3 CMathEngine::lonLat2WebMercator(dvec3 lonLat) { dvec3 mercator; ; +lonLat.y)*PI/) ...
- [RGEOS]支持栅格数据读取和显示
SharpMap真的很强大,这里通过改造GdalRasterLayer类实现了在RGeos项目中支持栅格图像的读取和显示,同时支持影像的无级缩放. GdalRasterLayer通过读取FWTools ...