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

Problem Description
You 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?

 
Input
There 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

 
Output
For each test case, print a single integer on a single line denoting the number of weak pairs in the tree.
 
Sample Input
1
2 3
1 2
1 2
 
Sample Output
1
 
题意:给你一颗根树,求有多少点对(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 ;
}

2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair dfs序+分块的更多相关文章

  1. hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

    Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  2. 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  3. 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  4. HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  5. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  6. HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  7. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  8. 2016 ACM/ICPC Asia Regional Dalian Online

    1009 Sparse Graph(hdu5876) 由于每条边的权值都为1,所以最短路bfs就够了,只是要求转置图的最短路,所以得用两个set来维护,一个用来存储上次扩散还没访问的点,一个用来存储这 ...

  9. 2016 ACM/ICPC Asia Regional Dalian Online(更新到五道题)

    1006 Football Games 这道题输入也很阴险!!! 这道题过题姿势最优雅的,不是if else if else if.那样很容易wa的. 如果没有平手选项, 赢得加一分的话, 可以用La ...

随机推荐

  1. 1、SQL Server自动化运维 - 备份(一)业务数据库

    为了能够恢复数据,数据库运维基础就是备份,备份自动化也是运维自动化首要进行的. 笔者的备份自动化,通过配置表快速配置为前提,同时记录备份过程,尽可能的减少人工操作.首先将SQL Server备份按用途 ...

  2. SQL Server需要监控哪些计数器

    常规计数器 收集操作系统服务器的服务器性能信息,包括Processor.磁盘.网络.内存 Processor 处理器 1.1 % Processor Time指处理器用来执行非闲置线程时间的百分比.通 ...

  3. 关于点击Invalidate Caches/Restart禁止插件后,重新加载--Android Studio

    1:47:27 Plugin Error Problems found loading plugins: Plugin "Google Analytics Uploader" wa ...

  4. Session 潜在bug防范

    注意: Session的使用一定要及时的清理,因为它是“全局”的(包括其生命周期),所以在使用Session保存状态时,不用时要及时的NULL掉,小心潜在的Bug.

  5. Json在PHP与JS之间传输

    1. JS-->PHP a). JS create Json <script> $(document).ready(function(){ /*--JS create Json--* ...

  6. .Net GridView 序号列

    给GridView增加一列:序号列 <asp:TemplateField HeaderText="序号"> <ItemTemplate> <%# (( ...

  7. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

  8. py

    import httplib,urllib import re import random def Login(userid,password): params=urllib.urlencode({' ...

  9. c语言socket通信

    http://www.cnblogs.com/xudong-bupt/archive/2013/12/29/3483059.html

  10. vi编辑器使用

    显示行号 set nu 取消行号 set nonu 定位到某一行 gg 定位到首行 G 定位到最后一行 在VI编辑器中切换调用外部shell命令 :!ifconfig   在编辑过程中,看ip地址 插 ...