题目: Little Sub and Triangles
题意:
数据:
4 2
0 1
100 100
0 0
1 0
0 50
0 2
思想:因为最多只用250个点,通过O(N^3)把所有的三角形面积求出来,通过二分查找,找到答案。
叉积公式:S=abs(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2) /2;
代码:
#include<bits/stdc++.h>
using namespace std;
const int num = 255;
#define LL long long
struct Node
{
LL x, y;
} a[num];
vector<LL>v;
int main()
{
int n, q;
scanf("%d %d", &n, &q);
for(int i = 0; i < n; i++)
{
scanf("%lld %lld", &a[i].x, &a[i].y);
}
v.clear();
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++)
{
LL ans = abs(a[i].x*a[j].y + a[j].x*a[k].y+a[k].x*a[i].y-a[i].x*a[k].y-a[j].x*a[i].y-a[k].x*a[j].y);//叉积
v.push_back(ans);
}
}
}
sort(v.begin(), v.end());
LL l, r;
for(int i = 1; i <= q; i++)
{
scanf("%lld %lld", &l, &r);
LL pos = upper_bound(v.begin(), v.end(), r*2) - lower_bound(v.begin(), v.end(), l*2);//二分查找,因为上面的结果没有除以2,所以l,r乘以2
printf("%lld\n", pos);
}
return 0;
}