树形DP+树状数组 HDU 5877 Weak Pair
- //树形DP+树状数组 HDU 5877 Weak Pair
- // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值
- // 这道题要离散化
- #include <bits/stdc++.h>
- using namespace std;
- #define LL long long
- typedef pair<int,int> pii;
- const double inf = 123456789012345.0;
- const LL MOD =100000000LL;
- const int N = 2e5+;
- const int maxx = ;
- #define clc(a,b) memset(a,b,sizeof(a))
- const double eps = 1e-;
- void fre() {freopen("in.txt","r",stdin);}
- void freout() {freopen("out.txt","w",stdout);}
- inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-; ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;}
- map<LL,LL> ma;
- LL a[N];
- LL c[N],b[N];
- LL in[N];
- vector<LL> g[N];
- LL lowbit(LL x){ return x&(-x);}
- LL add(LL x,int t){
- while(x>){
- c[x]+=t;
- x-=lowbit(x);
- }
- }
- LL Sum(LL x){
- LL sum=;
- while(x<maxx){
- sum+=c[x];
- x+=lowbit(x);
- }
- return sum;
- }
- LL ans=;
- LL n,k;
- void dfs(LL rt){
- for(LL i=;i<(int)g[rt].size();i++){
- LL v=g[rt][i];
- ans+=Sum(ma[a[v]]);
- if(a[v]==) add(maxx,);
- else add(ma[k/a[v]],);
- dfs(v);
- if(a[v]==) add(maxx,-);
- else add(ma[k/a[v]],-);
- }
- }
- int main(){
- int T;
- scanf("%d",&T);
- while(T--){
- ma.clear();
- memset(c,,sizeof(c));
- scanf("%I64d%I64d",&n,&k);
- for(int i=;i<=n;i++){
- scanf("%I64d",&a[i]);
- b[i*-]=a[i];
- if(a[i]!=) b[i*-]=k/a[i];
- g[i].clear();
- in[i]=;
- }
- sort(b,b+*n);
- int K=unique(b,b+*n)-b;
- int cxt=;
- for(int i=;i<K;i++){
- ma[b[i]]=++cxt;
- }
- for(LL i=;i<n-;i++){
- LL u,v;
- scanf("%I64d%I64d",&u,&v);
- g[u].push_back(v);
- in[v]++;
- }
- LL rt;
- for(LL i=;i<=n;i++){
- if(in[i]==){
- rt=i;
- break;
- }
- }
- ans=;
- if(a[rt]==) add(maxx,);
- else add(ma[k/a[rt]],);
- dfs(rt);
- printf("%I64d\n",ans);
- }
- return ;
- }
树形DP+树状数组 HDU 5877 Weak Pair的更多相关文章
- bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 793 Solved: 503[Submit][S ...
- 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...
- 奶牛抗议 DP 树状数组
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...
- HDU 5877 Weak Pair(弱点对)
HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Jav ...
- HDU 5877 Weak Pair(树状数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...
- HDU 5877 Weak Pair(树状数组+dfs+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意: 给出一棵树,每个顶点都有权值,现在要你找出满足要求的点对(u,v)数,u是v的祖先并且a[u]*a ...
- HDU - 5877 Weak Pair (dfs+树状数组)
题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...
- hdu 5877 Weak Pair dfs序+树状数组+离散化
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Prob ...
- HDU 5877 Weak Pair DFS + 树状数组 + 其实不用离散化
http://acm.hdu.edu.cn/listproblem.php?vol=49 给定一颗树,然后对于每一个节点,找到它的任何一个祖先u,如果num[u] * num[v] <= k.则 ...
随机推荐
- parent children
class parent{ protected static int count=0; public parent() { count++; } } public class child extend ...
- 256. Paint House
题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...
- Vim的可视模式
可视模式可以看到选中的字符串, 并对其进行操作 v:进入字符选择模式 V:进入行选择模式 ctrl-v(Window是ctrl-q):进入block选择模式 o:移动光标到选择的另一端 O:移动光标到 ...
- Ubuntu 12.04搭建MTK 6577 安卓开发环境
Ubuntu 12.04搭建 MTK 6577安卓开发环境 1. 下载并安装Vmware虚拟机: 2. 下载并在虚拟机上安装Ubuntu 12.04 iso 安装包:下载地址: ...
- SQLiteParameter不能将TableName作为参数
http://stackoverflow.com/questions/1274432/sqlite-parameters-not-allowing-tablename-as-parameter Gen ...
- TeeChart的X轴为时间,多个Y轴的显示
最后上代码 public partial class Test : Form { private TChart tChart = new TChart(); ; public Test() { Ini ...
- WPF中的Drawing
以前在用WinForm的时候,可以通过GDI+接口在窗体上动态绘制自定义的图形.在WPF中有没有对应的API呢,最近项目中用到了这个,在这里总结一下. WPF中的Drawing主要提供了几类API: ...
- HDU 1672 Cuckoo Hashing
Cuckoo Hashing Description One of the most fundamental data structure problems is the dictionary pro ...
- bzoj1876: [SDOI2009]SuperGCD
更相减损数. 上手就debug了3个小时,直接给我看哭了. 3个函数都写错了是什么感受? 乘2函数要从前往后乘,这样后面的数乘2进位以后不会干扰前面的数. 除2函数要从后往前除,这样前面的数借来的位不 ...
- sql2005主从数据库同步配置
网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库.主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据的 ...