问题 K: 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 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(-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<bits/stdc++.h>
using namespace std;
const int N = 2e7;
typedef long long ll;
ll n,q,x,y,l,r,cur;
double area[N];
vector <pair<ll,ll>> vec;
int main(){
	scanf("%lld%lld",&n,&q);
	for(int i = 1;i <= n;i++){
		scanf("%lld%lld",&x,&y);
		vec.push_back({x,y});
	}
	cur = 0;
	for(int i = 0;i < vec.size();i++)
		for(int j = 0;j < vec.size() && i != j;j++)
			for(int k = 0;k < vec.size() && i != j && i != k && j != k;k++)
				area[cur++] = 0.5 * fabs((vec[i].second - vec[j].second) * (vec[k]
				.first - vec[i].first) - (vec[k].second - vec[i].second) * (vec[i]
				.first - vec[j].first));				
	sort(area,area + cur);
	while(q--){
		scanf("%lld%lld",&l,&r);
		printf("%d\n",upper_bound(area,area + cur,r) - lower_bound(area,area + cur
		,l));
	}	
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值