首页 技术 正文
技术 2022年11月11日
0 收藏 734 点赞 2,980 浏览 2854 个字

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3192    Accepted Submission(s): 371

Problem DescriptionYou are given a rooted tree of N nodes, labeled from 1 to N. To the ith node a non-negative value ai is assigned.An ordered pair of nodes (u,v) is said to be weak if
  (1) u is an ancestor of v (Note: In this problem a node u is not considered an ancestor of itself);
  (2) au×av≤k.

Can you find the number of weak pairs in the tree?

 InputThere are multiple cases in the data set.
  The first line of input contains an integer T denoting number of test cases.
  For each case, the first line contains two space-separated integers, N and k, respectively.
  The second line contains N space-separated integers, denoting a1 to aN.
  Each of the subsequent lines contains two space-separated integers defining an edge connecting nodes u and v , where node u is the parent of node v.

Constrains: 
  
  1≤N≤105 
  
  0≤ai≤109 
  
  0≤k≤1018 OutputFor each test case, print a single integer on a single line denoting the number of weak pairs in the tree. Sample Input12 31 21 2 Sample Output1 题意:给你一颗根树,求有多少点对(u,v)  u!=v满足u是v的祖先且点权au*av<=k思路:问题转化一下,就是求对于每一个点u,以该点为根的子树下,有多少个点v的权值是小于等于(k/au + 1); 由于是子树的问题,那么可以想到的是先求出一个dfs序,将问题转化为区间查询的问题,那么问题就是,对于每个点u,在区间[st[u]+1,ed[u]]有多少个值是小于等于(k/au + 1); 求一个区间有多少个数小于莫个数G的数可以用分块实现,就是完整的块二分,两边的块暴力, 复杂度nsqrt(n)  坑点:一直以为1就是root。。。 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + ;
struct Edge {
int to, nex;
}e[maxn];
int n;
ll k;
ll a[maxn];
int root;
int head[maxn], tot;
void init() {
memset(head, -, sizeof head);
tot = ;
}
void add(int u, int v) {
e[tot].to = v;
e[tot].nex = head[u];
head[u] = tot++;
}
ll id[maxn];
int flag[maxn];
void input() {
scanf("%d%I64d", &n, &k);
memset(flag, , sizeof flag);
for(int i = ; i < n; ++i) scanf("%I64d", &a[i]);
int u, v;
for(int i = ; i < n; ++i) {
scanf("%d%d", &u, &v);
u--; v--;
flag[v] = ;
add(u, v);
}
for(int i = ; i < n; ++i) if(flag[i] == ) { root = i; break; }
}
int st[maxn], ed[maxn], tim;
void dfs(int u) {
st[u] = ++tim;
id[tim] = a[u];
for(int i = head[u]; ~i; i = e[i].nex) {
dfs(e[i].to);
}
ed[u] = tim;
}const int SIZE = ;
ll block[maxn / SIZE + ][SIZE + ];
void init2() {
int b = , j = ;
for(int i = ; i < n; ++i) {
block[b][j] = id[i];
if(++j == SIZE) { b++; j = ; }
}
for(int i = ; i < b; ++i) sort(block[i], block[i] + SIZE);
if(j) sort(block[b], block[b] + j);
}int query(int L, int R, ll v) {
int lb = L / SIZE, rb = R / SIZE;
int k = ;
if(lb == rb) {
for(int i = L; i <= R; ++i) if(id[i] < v) k++;
} else {
for(int i = L; i < (lb + ) * SIZE; ++i) if(id[i] < v) k++;
for(int i = rb * SIZE; i <= R; ++i) if(id[i] < v) k++;
for(int b = lb + ; b < rb; ++b) {
k += lower_bound(block[b], block[b] + SIZE, v) - block[b];
}
}
return k;
}
void solve() {
tim = -;
dfs(root);
init2();
ll ans = ;
for(int i = ; i < n; ++i) {
if(st[i] == ed[i]) continue;
if(a[i] == ) { ans += (ed[i] - st[i]); continue; }
ll v = k / a[i] + ;
ans += query(st[i]+, ed[i], v); }
printf("%I64d\n", ans);
}
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int cas;
while(~scanf("%d", &cas)) {
//int cas;
while(cas --) {
init();
input();
solve();
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289