题目链接:点这里!!!
题意:
给你一颗n个节点的有根树,问你有多少au*av<=k,且u是v的祖先。
数据范围:n<=100000,0<=ai<=1e9,0<=k<=1e18
题解:
我们对所有的ai,k/ai离散化,然后dfs这颗树,开始遍历x这个节点的时候询问当前有多少数小于等于k/a[x]然后加入到答案里,然后将a[x]加入到树状数组里,dfs完x时,将a[x]从树状数组里删除!!
我们发现我们每次询问的都是x节点的祖先,就ok了!
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
typedef long long LL;
const LL MOD = 1000000007;
const int N = 1e5+15;
const int maxn = 2e6+15;
const int letter = 130;
const int INF = 2e9+15;
const double pi=acos(-1.0);
const double eps=1e-13;
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int cnt,n,in[N],tot,head[N],f[2*N];
LL k,a[N],b[2*N];
LL sum=0;
struct edges{
int to,next;
}e[N];
int lowbit(int x){return x&(-x);}
void add(int x,int val){
while(x<2*N){
f[x]+=val;
x+=lowbit(x);
}
}
int query(int x){
int ans=0;
while(x>0){
ans+=f[x];
x-=lowbit(x);
}
return ans;
}
void addedges(int u,int v){
e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
void dfs(int x){
int l=lower_bound(b+1,b+cnt+1,k/a[x])-b;
int v=lower_bound(b+1,b+cnt+1,a[x])-b;
sum+=1ll*query(l);
add(v,1);
for(int i=head[x];i!=-1;i=e[i].next)
dfs(e[i].to);
v=lower_bound(b+1,b+cnt+1,a[x])-b;
add(v,-1);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%I64d",&n,&k);
for(int i=1;i<=n;i++) scanf("%I64d",a+i),b[i]=a[i];
clr(in,0),clr(head,-1);
clr(f,0);
sum=tot=0;
cnt=n;
for(int i=1;i<=n;i++) b[++cnt]=k/a[i];
sort(b+1,b+cnt+1);
cnt=unique(b+1,b+cnt+1)-(b+1);
int x,y;
for(int i=0;i<n-1;i++){
scanf("%d%d",&x,&y);
addedges(x,y);
in[y]++;
}
for(int i=1;i<=n;i++){
if(!in[i]) dfs(i);
}
printf("%I64d\n",sum);
}
return 0;
}