hdu 5877 Weak Pair(树状数组)

Weak Pair

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



Problem Description
You are given a rooted tree of N nodes, labeled from 1 to N . To the i th 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×avk .

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:
  
   1N105
  
   0ai109
  
   0k1018
 

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
 

Source
2016 ACM/ICPC Asia Regional Dalian Online


题意: 给出一棵nnn个结点的树和一个数kkk, 每个节点上有权值aia_iai, 问有多少个有序对(u,v)(u,v)(u,v)满足uuuvvv的祖先, 且au×av≤ka_u \times a_v \le kau×avk.

题解:

对于单个节点而言,可以讨论以这个节点为v,有多少祖先和其相乘满足条件,即算单点对答案的贡献度。

这题很容易就能转化成:

  对单个节点而言,统计比当前节点大的祖先k/ai;

由此,可以用树状数组维护从根节点开始的k/ai值。

每遍历到一个节点,求出树状数组里比当前ai大的数的个数。

再将其的k/ai值植入树状数组。

还有一点遍历过的节点要及时删除。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+100;
typedef long long ll;
vector<int>v[maxn];
map<long long,int>p;
int vis[maxn*2];
ll num[maxn*2],ans,c[maxn*2],data[maxn*2];
int n,len;
ll k;
void prepare(long long *x) {
    for(int i=1;i<=2*n;i++) data[i]=x[i];
    sort(data+1,data+2*n+1);
    int m=unique(data+1,data+2*n+1)-data-1;
    for(int i=1;i<=2*n;i++) x[i]=lower_bound(data+1,data+m+1,x[i])-data;
}
int lowbit(int x)//计算lowbit
{
    return x&(-x);
}
void add(int i,int val)//将第i个元素更改为val
{
    while(i<=2*n)
    {
        c[i]+=val;
        i+=lowbit(i);
    }
}
int sum(int i)//求前i项和
{
    int s=0;
    while(i>0)
    {
        s+=c[i];
        i-=lowbit(i);
    }
    return s;
}
void dfs(int s,int mm)
{
    int m=v[s].size();
    int cc=sum(num[s]-1);
    ans+=mm-cc;
    mm++;
    add(num[s+n],1);
    for(int i=0;i<m;i++)
    {
        int u=v[s][i];
        dfs(u,mm);
    }
    add(num[s+n],-1);
    mm--;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ans=0;
        memset(vis,0,sizeof(vis));
        memset(c,0,sizeof(c));
        for(int i=0;i<=n;i++)
            v[i].clear();
        scanf("%d%I64d",&n,&k);
        int a,b;
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&num[i]);
            num[i+n]=k/num[i];
        }
        prepare(num);
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d",&a,&b);
            v[a].push_back(b);
            vis[b]++;
        }
        int s;
        for(int i=1;i<=n;i++)
        if(vis[i]==0)
        {
            s=i;
            break;
        }
        dfs(s,0);
        printf("%I64d\n",ans);
    }
    return 0;
}


从根开始dfs, .


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值