2016 ACM/ICPC Asia Regional Dalian Online
http://acm.hdu.edu.cn/showproblem.php?pid=5877
Weak Pair
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1268 Accepted Submission(s): 419
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
Source
2016 ACM/ICPC Asia Regional Dalian Online
Recommend
wange2014
题意:给一棵有向树,每个节点一个值ai, 另外给一个k, 求所有满足ai*aj<=k的点对数, 其中ai是aj祖先.
思路:题目难点在于数据范围:1<=N<=1e5, 0<=ai<=1e9, 0<=k<=1e18; 一开始,就想到来个dfs,O(n),外加树状数组统计处理O(log1e9),总复杂度是可以的,但是遗憾的是数据范围1e9是开不了树状数组的Orz.
然后想到了用map来代替数组数组,结果O(n(log1e9)^2),一开始没算仔细,LTE了,想着,别人的代码都是1000+ms过的,就想在一些细的地方优化看能不能过,然后LTE了几次. 最后终于决定通过数值转换(相当于离散化吧)降低复杂度->O(nlogn)级,我早些时候想到了,但是鉴于编码稍复杂~~~. 然后开始改,改好了之后,一直wa, 一直想不通!!! 后来发现在处理k/ai是可以溢出int的, 改了就过了, 亏我算来算去都觉得不会超过int,就一直找其它方面的问题Orz…
说说离散化处理吧,首先发现了数据并不多,但是范围大, 所以使用树状数组统计祖先中小于等于k/ai的节点数时log(1e9)^2吃不消, 所以就自然想到离散化了, 然而若只对ai进行离散化就会把k/ai的关系弄丢,所以同时确定k/ai的值并且进行离散化也就避免不了了…具体看代码>>>(703ms,看了下其他人的, 这算是很快了)
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
typedef long long LL;
const int maxn=1e5+100;
const int INF=2e9;
int datalimit;
int data[maxn],to[maxn];
int shu[maxn<<1];
vector<int> G[maxn];
LL n,k,sum;
int vis[maxn];
void init()
{
for (int i=1;i<=n;i++){
G[i].clear();
vis[i]=0;
}
}
LL get_sum(LL pos)
{
LL sum=0;
while (pos>0)
{
sum+=shu[pos];
pos-=(-pos)&pos;
}
return sum;
}
void updata(int pos, int val)
{
while (pos<=datalimit)
{
shu[pos]+=val;
pos+=(-pos)&pos;
}
}
void dfs(int v,int dep)
{//核心所在
if (to[v]!=-1)//这里要注意下
sum+=get_sum(to[v]);
else sum+=dep;//引入dep的意义所在
updata(data[v],1);//更新
for (int i=0;i<(int)G[v].size();i++){
dfs(G[v][i],dep+1);//O(v)
}
updata(data[v],-1);//消除更新
}
int main()
{
//freopen("D:in.txt","r",stdin);
int t;
scanf("%d",&t);
while (t--)
{
scanf("%I64d%I64d",&n,&k);
init();
vector<pair<LL,int> > vec;
for (int i=1;i<=n;i++){
scanf("%d",&data[i]);
vec.push_back(make_pair(data[i],i));
if (data[i])//这里要注意下
vec.push_back(make_pair(k/data[i],n+i));
else{
to[i]=-1;//相当于做个标记,这时data[i]=0;
}
}
for (int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
vis[b]=1;
G[a].push_back(b);
}
//开始离散化
int cnt=1,last=-INF;
sort(vec.begin(),vec.end());
for (int i=0;i<(int)vec.size();i++){
if (vec[i].second>n){//数据分类
if (vec[i].first!=last){//重新分号
last=vec[i].first;
to[vec[i].second-n]=++cnt;
}
else{
to[vec[i].second-n]=cnt;
}
}
else {
if (vec[i].first!=last){
last=vec[i].first;
data[vec[i].second]=++cnt;
}
else{
data[vec[i].second]=cnt;
}
}
shu[i]=0;//树状数组初始化
}
datalimit=cnt;//数组数组更新上限
vec.clear();
int root=1;
for (int i=1;i<=n;i++){
if (!vis[i]){
root=i; break;
}
}
sum=0;
dfs(root,0);
printf("%I64d\n",sum);
}
}