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. 在Py文件中引入django环境

    复制manage.py中的相关代码即可并将文件置于Project文件夹(与manage.py同位置)下 示例: #! /usr/bin/env python # -*- coding:utf- -*- ...

  2. struts2类型转换器、 类型转换错误 以及INPUT view

    1.1.1    Struts2中的类型转换器 Struts2内置了常见数据类型多种转换器 boolean 和 Boolean char和 Character int 和 Integer long 和 ...

  3. Effective C++ -----条款14: 在资源管理类中小心copying行为

    复制RAII对象必须一并复制它所管理的资源,所以资源的copying行为决定RAII对象的copying行为. 普遍而常见的RAII class copying行为是:抑制copying(使用私有继承 ...

  4. java classpath批量设置shell脚本

    java classpath批量设置shell脚本   注意:linux bash jar文件之间的分隔符是':'    export JAR_HOME=path to directory which ...

  5. 将xml文件作为一个小的数据库,进行学生的增删改查

    1.xml文件: <?xml version="1.0" encoding="UTF-8"?><Students> <studen ...

  6. 迭代器iterator

    现在接着上篇的,写一来标识vector 的元素的对象迭代器iterator: 还是通过具体代码举例: 下面我讲了一个我暑期团队的故事哦~~: #include<iostream> #inc ...

  7. 【linux】学习4

    文件压缩: gzip :压缩   解压缩 zcat: 读取压缩文件 gzip text1  :压缩text1 得到 text1.gz 原文件不见了 gzip -c text1 > text1.g ...

  8. 【python】类中的self

    在python的类中,经常会写self,代表对象自己.如下例: #coding=utf-8 class Foo: def __init__(self, name): self.name = name ...

  9. NEFU 503 矩阵求解 (非01异或的高斯消元)

    题目链接 中文题,高斯消元模板题. #include <iostream> #include <cstdio> #include <cmath> #include ...

  10. Sql如何自动定时备份数据库

    直接上图