正难则反的思想还是不能灵活应用啊

题意:给你n个点,每个点有一个权值,接着是n-1有向条边形成一颗有根树,问你有多少对点的权值乘积小于等于给定的值k,其中这对点必须是孩子节点与祖先的关系

我们反向思考,可以知道任一点都只对其每个祖先有贡献。所以我们可以转化为求每个点与其每个祖先的乘积小于等于给定的值k的对数。

我们dfs遍历这颗树使用树状数组维护,dfs遍历孩子就添点回溯就删点,接着对每个点计算树状数组里不大于(k/此点)的个数。注意值太大我们需要离散化,而且我们可以把每个点m与k/m都离散化出来,然后就是每次树状数组更新这个点的个数(加减1),查询不大于(k/此点)的个数

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
struct node
{
int pos;
ll val;
} nod[Max];
int bit[Max],vis[Max],n;
ll k,ans,rop[Max];
int head[Max],nnext[Max],to[Max],e;
bool cmp(struct node p1,struct node p2)
{
if(p1.val==p2.val)
return p1.pos<p2.pos;
return p1.val<p2.val;
}
void Add(int u,int v)
{
to[e]=v;
nnext[e]=head[u];
head[u]=e++;
return;
}
int lowbit(int x)
{
return x&(-x);
}
void AddBit(int x,int y)
{
while(x<=(n<<))
{
bit[x]+=y;
x+=lowbit(x);
}
return;
}
int Sum(int x)
{
int sum=;
while(x)
{
sum+=bit[x];
x-=lowbit(x);
}
return sum;
}
void dfs(int son,int fat)
{
AddBit(rop[son],);//遍历孩子添边
for(int i=head[son]; i!=-; i=nnext[i])
{
if(to[i]!=fat)
{
ans+=Sum(rop[to[i]+n]);
dfs(to[i],son);
}
}
AddBit(rop[son],-);//回溯删边
return;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(vis,,sizeof(vis));
scanf("%d %I64d",&n,&k);
for(int i=; i<=n; ++i)
{
scanf("%I64d",&nod[i].val);
nod[i].pos=i;
}
for(int i=; i<=n; ++i)//和k/num[u]一起离散化
{
if(nod[i].val)//关键注意除数为0
nod[i+n].val=k/nod[i].val;
else
nod[i+n].val=0ll;
nod[i+n].pos=i+n;
}
sort(nod+,nod++(n<<),cmp);
for(int i=; i<=(n<<); ++i)//离散化
rop[nod[i].pos]=i;
e=;
memset(head,-,sizeof(head));
int u,v;
for(int i=; i<n; ++i)
{
scanf("%d %d",&u,&v);
Add(u,v);
vis[v]++;
}
ans=0ll;
memset(bit,,sizeof(bit));
for(int i=; i<=n; ++i)
{
if(!vis[i])//找根
{
dfs(i,i);
}
}
printf("%I64d\n",ans);
}
return ;
}

Weak Pair

最新文章

  1. settings.php rwx
  2. 简单的jquery拖曵原理js特效实例
  3. H5前端面试题及答案(1)
  4. kafka 简介
  5. CentOS学习笔记--程序管理
  6. 又一编辑神器-百度编辑器-Ueditor
  7. 最基本MySQL命令及vi命令
  8. jsp各部分编码的含义
  9. ActivityManager
  10. Brain Network (medium)
  11. maven - 引用本地jar,进行jar包移动
  12. Redis 设计与实现 (二)--数据库
  13. vue中$router.push打开新窗口
  14. react-router 4.0(四)跳转404
  15. n&&m and n||m 的区别
  16. 设置IIS允许下载.config文件
  17. Ubuntu下Qt(Retex)无法输入中文
  18. ORA-12546: TNS: 权限被拒绝(ORA - 12546 TNS: Permission Denied)
  19. jmeter安装启动报错:Not able to find Java executable or version. Please check your Java installation
  20. IntelliJ IDEA 缓存和索引介绍

热门文章

  1. SSL证书的生成
  2. JS HTML DOM---Document对象
  3. Spring 缓存注解@Cacheable 在缓存时候 ,出现了第一次进入调用 方法 ,第二次不调用的异常
  4. python系列十四:Python3 文件
  5. Server Objects Extension(SOE)开发(一)
  6. el-tree 设置目录树中的某个节点为高亮状态
  7. Python3.6全栈开发实例[005]
  8. mapper文件提示:No data sources are configured to run this sql
  9. nodejs开发解决方案
  10. socket()模块和套接字对象的内建方法