Weak Pair

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
 

题意:

  给出一棵n个结点的树和一个数k, 每个节点上有权值​​, 问有多少个有序对(u,v)  (u,v)满足uv的祖先, a[u] * a[v] <=K;

题解:

  最近学treap,发个题解

  从根开始dfs,

   用treap维护当前节点uu到根的节点权值序列,

   然后就在treap上查询小于等于K/a[v]​​的数的个数.

  之后把a[u]加到treap中, 退栈的时候把a[u]从treap中删除. 复杂度是nlogn的.

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18+;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e6+, mod = 1e9+, inf = 2e9; LL K,ans,a[N];
int n,size,root,head[N],t; struct data{
int l,r,size,rnd,w;
LL v;
}tr[N];
void update(int k)//更新结点信息
{
tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+tr[k].w;
}
void rturn(int &k)
{
int t=tr[k].l;tr[k].l=tr[t].r;tr[t].r=k;
tr[t].size=tr[k].size;update(k);k=t;
}
void lturn(int &k)
{
int t=tr[k].r;tr[k].r=tr[t].l;tr[t].l=k;
tr[t].size=tr[k].size;update(k);k=t;
}
void insert(int &k,LL x)
{
if(k==)
{
size++;k=size;
tr[k].size=tr[k].w=;tr[k].v=x;tr[k].rnd=rand();
return;
}
tr[k].size++;
if(tr[k].v==x)tr[k].w++;
else if(x>tr[k].v)
{
insert(tr[k].r,x);
if(tr[tr[k].r].rnd<tr[k].rnd)lturn(k);
}
else
{
insert(tr[k].l,x);
if(tr[tr[k].l].rnd<tr[k].rnd)rturn(k);
}
}
void del(int &k,LL x)
{
if(k==)return;
if(tr[k].v==x)
{
if(tr[k].w>)
{
tr[k].w--;tr[k].size--;return;
}
if(tr[k].l*tr[k].r==)k=tr[k].l+tr[k].r;
else if(tr[tr[k].l].rnd<tr[tr[k].r].rnd)
rturn(k),del(k,x);
else lturn(k),del(k,x);
}
else if(x>tr[k].v)
tr[k].size--,del(tr[k].r,x);
else tr[k].size--,del(tr[k].l,x);
}
int query_rank(int k,LL x)
{
if(k==)return ;
if(tr[k].v==x)return tr[tr[k].l].size+tr[k].w;
else if(x>tr[k].v)
return tr[tr[k].l].size+tr[k].w+query_rank(tr[k].r,x);
else return query_rank(tr[k].l,x);
} struct ss{int to,next;}e[N * ];
void add(int u,int v) {e[t].next=head[u];e[t].to=v;head[u]=t++;} void dfs(int u,int fa) {
LL limit = INF;
if(a[u] != ) limit = K/(a[u]);
ans += (query_rank(root,limit));
insert(root,a[u]);
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == fa) continue;
dfs(to,u);
}
del(root,a[u]);
}
int d[N];
int main() {
int T;
scanf("%d",&T);
while(T--) {
size = ;
root = ;
memset(head,,sizeof(head));t = ;
for(int i = ; i < N; ++i) {
tr[i].l = ;
tr[i].r = ;
d[i] = ;
}
scanf("%d%lld",&n,&K);
for(int i = ; i <= n; ++i) scanf("%lld",&a[i]);
for(int i = ; i < n; ++i) {
int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
d[v]++;
}
int rt;
for(int i = ; i <= n; ++i) if(!d[i]) rt = i;
ans = ;
dfs(rt,);
printf("%lld\n",ans); }
return ;
}

2016 ACM/ICPC Asia Regional Dalian Online HDU 5877 Weak Pair treap + dfs序的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ( ...

  6. 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 ...

  7. 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 ...

  8. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  9. HDU 5875 Function 2016 ACM/ICPC Asia Regional Dalian Online

    N个数(N<=100000),M个询问,每次询问L,R,求F(L,R). F(L,R)=F(L,R-1)%A[R] , L<R 这道题数据比较鶸 可以直接用递减爆 正确做法应该是倍增 用倍 ...

随机推荐

  1. js弹出提示信息,然后跳转到另一页面

    <script language="javascript">  alert("您的用户名与密码已成功修改!");  document.locatio ...

  2. unity3d 特殊文件夹和脚本编译顺序

    unity3d 特殊文件夹和脚本编译顺序 转自http://blog.csdn.net/u010019717/article/details/40474631 大多数情况下,您可以选择任何你喜欢的文件 ...

  3. selenium 右键下载图片,结合sikuli

    上一次写右键下载是结合robot,这次是使用selenium+sikuli 上一次日志:http://www.cnblogs.com/tobecrazy/p/3969390.html 有关sikuli ...

  4. SUSE下FTP服务器搭建

    FTP(File Transfer Protocol),是TCP/IP网络上两台计算机传送文件的协议,是在TCP/IP网络和Internet上最早使用的协议之一,属于网络协议组的应 用层.FTP客户机 ...

  5. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. 【leetcode】Two Sum (easy)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  7. 【linux】学习2

    鸟哥那本书的第6章 文件权限: ^                ^     ^      ^        ^              ^                 ^ 1         ...

  8. IOS - delegate为什么不使用retain

    循环引用所有的引用计数系统,都存在循环应用的问题.例如下面的引用关系: 对象a创建并引用了对象b.对象b创建并引用了对象c.对象c创建并引用了对象b. 这时候b和c的引用计数分别是2和1.当a不再使用 ...

  9. IIS7.0提示---无法识别的属性“targetFramework”。请注意属性名称区分大小写。

    当我把我做的网站放在IIS7.0的服务器上的时候,浏览时提示这个错误信息 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消 ...

  10. shell之数值运算

    Shell中声明变量默认是字符串, 要参与数值运算,可使用下面方式,简单,表示以数值方式.