The 13th Chinese Northeast Contest C. Line-line Intersection(平面几何)

Posted li_wen_zhuo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The 13th Chinese Northeast Contest C. Line-line Intersection(平面几何)相关的知识,希望对你有一定的参考价值。

题目描述

There are n lines l1,l2,…,ln on the 2D-plane.
Staring at these lines, Calabash is wondering how many pairs of (i,j) that 1≤i<j≤n and li,lj share at least one common point. Note that two overlapping lines also share common points.
Please write a program to solve Calabash’s problem.

Input

The first line of the input contains an integer T(1≤T≤1000), denoting the number of test cases.
In each test case, there is one integer n(1≤n≤100000) in the first line, denoting the number of lines.
For the next n lines, each line contains four integers xai,yai,xbi,ybi(|xai|,|yai|,|xbi|,|ybi|≤109). It means li passes both (xai,yai) and (xbi,ybi). (xai,yai) will never be coincided with (xbi,ybi).
It is guaranteed that ∑n≤106.

Output

For each test case, print a single line containing an integer, denoting the answer.

Example

Input
3
2
0 0 1 1
0 1 1 0
2
0 0 0 1
1 0 1 1
2
0 0 1 1
0 0 1 1
Output
1
0
1

题目大意

有n条线,给你n条线上的一对点。问这些线之间有多少对交点(一对重合的线算1)。

题目分析

这道题有三种情况:

  1. 两条直线的斜率k不相同,则这两条直线一定会有一个交点。
  2. 两条直线的斜率k相同,但是截距b不相同,则两条线一定没有交点
  3. 两条直线的斜率k和截距b都相同,则这两条直线有交点。

每读入一条直线,我们只需要算出这一条直线的斜率k和截距b,numk //表示之前读入的直线中斜率也为k的直线个数 | t //表示之前读入的直线中斜率为k并且截距为b的直线个数

那么之前读入的直线中与该条直线相交的直线数为: n u m ( 直 线 总 数 ) − n u m k + t 。 num(直线总数)-numk+t。 num(线)numk+t

注:这道题的斜率k和截距b不能用浮点数来存(因为有浮点误差,浮点数不靠谱)
我们可以用pair来存储k: y / g c d ( x , y ) , x / g c d ( x , y ) {y/gcd(x,y) , x/gcd(x,y)} y/gcd(x,y),x/gcd(x,y) (x=x2-x1,y=y2-y1)
( x 1 ∗ y 2 − x 2 ∗ y 1 ) / g c d ( x , y ) (x1*y2-x2*y1)/gcd(x,y) (x1y2x2y1)/gcd(x,y) 来表示b。

代码如下
#include <iostream>
#include <cmath>
#include <cstdio>
#include <set>
#include <string>
#include <cstring>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#define LL long long
#define ULL unsigned long long
#define PII pair<LL,LL>
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=1e5+5,INF=1e9+7;
struct Node {
	PII k;				//表示斜率 k
	LL b;				//表示截距 b
	bool operator< (const Node a)const
	{
		if(k==a.k) return b<a.b; 
		return k<a.k;
	}
};
int main() 
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--) 
	{
		int n;
		cin>>n;
		map<PII,int> mp;		//这个map中只存储斜率,用于查找斜率相同的直线的个数
		map<Node,int> s;		//这个map中存斜率和截距,用于在查找斜率和截距都相同的线的个数
		LL ans=0;
		for(int i=1;i<=n;i++)
		{
			LL x1,x2,y1,y2;
			cin>>x1>>y1>>x2>>y2;
			LL x=x2-x1,y=y2-y1,c=x1*y2-x2*y1;
			LL g=__gcd(x,y);
			PII k={x/g,y/g}; c/=g;
			int numk=mp[k],t=s[{k,c}];		//算出 numk 和 t
			ans+=i-1-numk+t;				//记录答案
			mp[k]++;						//将这条线放入集合中
			s[{k,c}]++;
		}
		cout<<ans<<endl;
	}
	return 0;
}

以上是关于The 13th Chinese Northeast Contest C. Line-line Intersection(平面几何)的主要内容,如果未能解决你的问题,请参考以下文章

The 13th Chinese Northeast Contest C. Line-line Intersection(平面几何)

The 13th Chinese Northeast Collegiate Programming Contest E题

The 15th Chinese Northeast Collegiate H - Loneliness(思维,构造)

The 15th Chinese Northeast Collegiate H - Loneliness(思维,构造)

The 15th Chinese Northeast Collegiate C. Vertex Deletion(树形dp)

The 15th Chinese Northeast L. k-th Smallest Common Substring(广义SAM,对节点字典序排序)