当前位置: 代码迷 >> 综合 >> 问题 K: Little Sub and Triangles
  详细解决方案

问题 K: Little Sub and Triangles

热度:82   发布时间:2023-11-22 13:44:35.0

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