题目描述
Little Sub loves triangles. Now he has a problem for you.
Given n points on the two-dimensional plane, you have to answer many queries. Each query require you to calculate the number of triangles which are formed by three points in the given points set and their area S should satisfy l≤S≤r.
Specially, to simplify the calculation, even if the formed triangle degenerate to a line or a point which S = 0, we still consider it as a legal triangle.
输入
The fi rst line contains two integer n, q(1≤n≤250, 1≤q≤100000), indicating the total number of points.
All points will be described in the following n lines by giving two integers x, y(-107≤x, y≤107) as their coordinates.
All queries will be described in the following q lines by giving two integers l, r(0≤l≤r≤1018).
输出
Output the answer in one line for each query.
样例输入
4 2
0 1
100 100
0 0
1 0
0 50
0 2
样例输出
3
1
思路
根据向量叉积公式求得所组成的平行四边形的面积,排序后二分查找即可
代码实现
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<cstring>
#include<set>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
const int N=1e5+5;
typedef long long ll;
ll sum[250*250*250+5];
ll x[300],y[300],n,q,cnt;
int main()
{
scanf("%lld%lld",&n,&q);
for(int i=0;i<n;i++)
{
scanf("%lld%lld",&x[i],&y[i]);
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
sum[cnt++]=abs((x[i]-x[j])*(y[k]-y[j])-(x[k]-x[j])*(y[i]-y[j]));
}
}
}
sort(sum,sum+cnt);
while(q--)
{
ll l,r,s1,s2;
scanf("%lld%lld",&l,&r);
l*=2;
r*=2;
s1=lower_bound(sum,sum+cnt,l)-sum;
s2=upper_bound(sum,sum+cnt,r)-sum;
printf("%lld\n",s2-s1);
}
return 0;
}