问题 K: Little Sub and Triangles
时间限制: 2 Sec 内存限制: 256 MB
提交: 427 解决: 76
[提交] [状态] [命题人:admin]
题目描述
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
思路:主要就是掌握已知三点坐标求三角形面积,不清楚的点这里[(click here)
(https://blog.youkuaiyun.com/Kris_Kris/article/details/89818365)
代码由队友提供
#include<bits/stdc++.h>
#define ll long long
using namespace std;
typedef struct
{
ll x;
ll y;
}node;
double aa[30000000];
node pp[300];
double area(node p1,node p2,node p3)
{
return abs(p1.x*p2.y + p1.y*p3.x + p2.x*p3.y - p2.y*p3.x - p3.y*p1.x - p1.y*p2.x) / 2.0;
}
int main()
{
int n, q;
int tmp = 0;
scanf("%d %d", &n, &q);
for (int i = 0; i < n; i++)
{
scanf("%lld %lld",&pp[i].x,&pp[i].y);
}
if (n >= 3)
{
for (int i = 0; i < n - 2; i++)
{
for (int j = i + 1; j < n - 1; j++)
{
for (int k = j + 1; k < n; k++)
{
aa[tmp++] = area(pp[i], pp[j], pp[k]);
}
}
}
}
sort(aa, aa + tmp);
while (q--)
{
ll l, r;
scanf("%lld %lld", &l, &r);
if (n <= 2)
{
cout << 0 << endl;
continue;
}
ll t1 = lower_bound(aa + 0, aa + tmp, l) - aa;
ll t2 = upper_bound(aa + 0, aa + tmp, r) - aa;
printf("%lld\n", t2 - t1);
}
return 0;
}