Chino held a blind date event, there are n male guests and m female
guests attending.The personality characteristic value of each person can be represented
by an integer, the personality characteristic value of the i-th male
guest is ai , and the personality characteristic value of the j-th
female guest is bj.
Chino believes that when the calculation result of the personality
characteristic values of two people ai×bjin a certain range [l,r], the personality of two person is suitable each of them.
Chino would like to know how many matching combinations possible satisfied personality suitability between male and female guests.
InputThe first line contain two positive integers n,m(1≤n,m≤105)
The next line contain n integers ai(−109≤ai≤109)
indicates the personality characteristic value of the male guests.
Then the next line contain m integersbi(−109≤bi≤109)
indicates the personality characteristic value of the female guests.
the final line only contain two integers l,r(−109≤l≤r≤109)
Output
Output an integer on a line to indicate the number of matching combinations. Examples Input 5 5 1 2 3 4 5 1 2 3 4 5 1 20 Output 24 Input 3 4 -1 -3 -5 -7 -6 -8 -9 9 9 Output 1
Note
For the first example, except that the male guest 5 and the female guest 5 do not meet the conditions, the other conditions are all
legal, so the answer is 25-1=24For the second example, there is only a legal matching combination for male guest 1 and female guest 4.
思路
TAG: 二分查找,分情况讨论,尺取
要找 L≤ai×bj≤RL \leq a_i \times b_j \leq RL≤ai×bj≤R ,分成以下三种情况即可二分查找。
- bj>0b_j>0bj>0,变形为 L/bj≤ai≤R/bjL/b_j \leq a_i \leq R/b_jL/bj≤ai≤R/bj,注意边界。
- bj=0b_j=0bj=0,变形为 L≤0≤RL \leq 0 \leq RL≤0≤R,只要[L,R]的区间包括0,那么所有的a都满足。
- bj<0b_j<0bj<0,变形为 R/bj≤ai≤L/bjR/b_j \leq a_i \leq L/b_jR/bj≤ai≤L/bj,同样注意边界。
本题也可以使用尺取法将复杂度降低到O(n+m),并且不需要额外分情况讨论。
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005],b[100005];
int L,R;
int n,m;
int zfindl(ll n)
{
int l=1;
int r=m;
int mid;
while(l<=r)
{
mid=(l+r)>>1;
if(b[mid]*n>=L)r=mid-1;
else l=mid+1;
}
if(b[l]*n>=L&&b[l]*n<=R)return l;
else return -1;
}
int zfindr(ll n)
{
int l=1;
int r=m;
int mid;
while(l<=r)
{
mid=(l+r)>>1;
if(b[mid]*n<=R)l=mid+1;
else r=mid-1;
}
if(b[r]*n>=L&&b[r]*n<=R)return r;
else return -1;
}
int ffindl(ll n)
{
int l=1;
int r=m;
int mid;
while(l<=r)
{
mid=(l+r)>>1;
if(b[mid]*n>R)l=mid+1;
else r=mid-1;
}
if(b[l]*n>=L&&b[l]*n<=R)return l;
else return -1;
}
int ffindr(ll n)
{
int l=1;
int r=m;
int mid;
while(l<=r)
{
mid=(l+r)>>1;
if(b[mid]*n<L)r=mid-1;
else l=mid+1;
}
if(b[r]*n>=L&&b[r]*n<=R)return r;
else return -1;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=m;i++)cin>>b[i];
sort(a+1,a+1+n);
sort(b+1,b+1+m);
cin>>L>>R;
ll ans=0;
for(int i=1;i<=n;i++)
{
if(a[i]>=0)
{
int pos1=zfindl(a[i]);
int pos2=zfindr(a[i]);
if(pos1==-1||pos2==-1)continue;
else ans+=pos2-pos1+1;
}
else if(a[i]<0)
{
int pos1=ffindl(a[i]);
int pos2=ffindr(a[i]);
if(pos1==-1||pos2==-1)continue;
else ans+=pos2-pos1+1;
}
}
cout<<ans<<endl;
}