2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair dfs序+分块
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3192 Accepted Submission(s): 371
(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?
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
#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序+分块的更多相关文章
- 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 ...
- 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 ( ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 2016 ACM/ICPC Asia Regional Dalian Online
1009 Sparse Graph(hdu5876) 由于每条边的权值都为1,所以最短路bfs就够了,只是要求转置图的最短路,所以得用两个set来维护,一个用来存储上次扩散还没访问的点,一个用来存储这 ...
- 2016 ACM/ICPC Asia Regional Dalian Online(更新到五道题)
1006 Football Games 这道题输入也很阴险!!! 这道题过题姿势最优雅的,不是if else if else if.那样很容易wa的. 如果没有平手选项, 赢得加一分的话, 可以用La ...
随机推荐
- 1、SQL Server自动化运维 - 备份(一)业务数据库
为了能够恢复数据,数据库运维基础就是备份,备份自动化也是运维自动化首要进行的. 笔者的备份自动化,通过配置表快速配置为前提,同时记录备份过程,尽可能的减少人工操作.首先将SQL Server备份按用途 ...
- SQL Server需要监控哪些计数器
常规计数器 收集操作系统服务器的服务器性能信息,包括Processor.磁盘.网络.内存 Processor 处理器 1.1 % Processor Time指处理器用来执行非闲置线程时间的百分比.通 ...
- 关于点击Invalidate Caches/Restart禁止插件后,重新加载--Android Studio
1:47:27 Plugin Error Problems found loading plugins: Plugin "Google Analytics Uploader" wa ...
- Session 潜在bug防范
注意: Session的使用一定要及时的清理,因为它是“全局”的(包括其生命周期),所以在使用Session保存状态时,不用时要及时的NULL掉,小心潜在的Bug.
- Json在PHP与JS之间传输
1. JS-->PHP a). JS create Json <script> $(document).ready(function(){ /*--JS create Json--* ...
- .Net GridView 序号列
给GridView增加一列:序号列 <asp:TemplateField HeaderText="序号"> <ItemTemplate> <%# (( ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...
- py
import httplib,urllib import re import random def Login(userid,password): params=urllib.urlencode({' ...
- c语言socket通信
http://www.cnblogs.com/xudong-bupt/archive/2013/12/29/3483059.html
- vi编辑器使用
显示行号 set nu 取消行号 set nonu 定位到某一行 gg 定位到首行 G 定位到最后一行 在VI编辑器中切换调用外部shell命令 :!ifconfig 在编辑过程中,看ip地址 插 ...