题目描述
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
这个题我主要收获了 一个三角形的用坐标求面积的公式向量a(a,b)和向量b(x,y)的坐标方向相乘再相减,其实就是bx-ay,还有就是for循环控制输出,看看下面的for循环输出的其实形成三角形的数量这个对我来说其实就是这些细节总是把握不好,这次学会了从小到大进行控制。最后二分进行判断在区间里面的个数,注意sort一下
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
#define ll long long
struct point
{
ll x,y;
}a[900];
ll b[9000000];
int main()
{
int n,num=0,q;
ll ans;
scanf("%d %d",&n,&q);
for(int i=1;i<=n;i++)
{
scanf("%lld %lld",&a[i].x,&a[i].y);
for(int j=1;j<i;j++){
for(int k=j+1;k<i;k++){
ans=abs((a[i].x-a[j].x)*(a[i].y-a[k].y)-(a[i].x-a[k].x)*(a[i].y-a[j].y));
b[num++]=abs(ans);
}
}
}
sort(b,b+num);
for(int i=0;i<q;i++)
{
ll l,r;
scanf("%lld %lld",&l,&r);
ll count1=(upper_bound(b,b+num,r*2)-b)-(lower_bound(b,b+num,l*2)-b);
printf("%lld\n",count1);
}
return 0;
}