2019年杭州师范大学第十二届程序设计竞赛 K:Little Sub and Triangles(三角形面积公式+二分)

博客围绕“Little Sub and Triangles”题目展开,给出已知三角形三个顶点坐标求面积公式S=0.5*fabs((x2 - x1)*(y3 - y1)-(x3 - x1)*(y2 - y1)),通过遍历求所有三角形面积,排序二分统计个数来解题,还给出题目描述、输入输出要求及示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Little Sub and Triangles

【题解】

数学太差推错公式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

Author

CHEN, Jingbang

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值