Little Sub and Triangles(向量叉积)

题目描述
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值