【题解】
数学太差推错公式orz
已知三角形三个顶点坐标求三角形面积公式:S=0.5*fabs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))
遍历求出所有三角形的面积,排序二分统计个数即可。
【代码】
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const double eps = 1e-8;
double a[255][3];
double cul_s(int x,int y,int z)
{
double x1=a[x][0],y1=a[x][1],x2=a[y][0],y2=a[y][1],x3=a[z][0],y3=a[z][1];
double S=0.5*fabs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1));
return S;
}
int main()
{
int n,q; scanf("%d%d",&n,&q);
for(int i=0;i<n;i++) scanf("%lf%lf",&a[i][0],&a[i][1]);
vector <double> vec;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
for(int k=j+1;k<n;k++)
vec.push_back(cul_s(i,j,k));
sort(vec.begin(),vec.end());
while(q--){
double l,r; scanf("%lf%lf",&l,&r);
int a=lower_bound(vec.begin(),vec.end(),l-eps)-vec.begin();
int b=lower_bound(vec.begin(),vec.end(),r+eps)-vec.begin();
printf("%d\n",b-a);
}
return 0;
}
【题目】
Little Sub and Triangles AC
Time Limit: 2 s Memory Limit: 256 MB
Submission:874 AC:94 Score:100.00
Description
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.
Input
The first 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(−10^7 ≤ x,y ≤ 10^7) as their coordinates.
All queries will be described in the following q lines by giving two integers l, r(0 ≤ l ≤ r ≤ 10^18).
Output
Output the answer in one line for each query.
Samples
input:
4 2
0 1
100 100
0 0
1 0
0 50
0 2
output:
3
1