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

题解:两条直线不平行必相交,若平行:若重合答案加1,否则不算。

我们用两个map来刻画直线的特性,mp1刻画a*x+b*y的直线系有多少个,mp2刻画a*x+b*y=c这一条直线有多少个。
假设当前直线与之前的线段都相交,那么我们需要减去与这条直线平行而不重合的直线。即ans+=i-1+mp2[]-mp1[].
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<stdio.h>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>P;
typedef pair<pair<ll,ll>,ll>Pi;
const int maxn=;
map<pair<ll,ll>,ll>mp1;//两个参数a,b,代表形如a*x+b*y=c(c任意)的直线有多少个
map<pair<pair<ll,ll>,ll>,ll>mp2;//三个参数a,b,c,代表形如a*x+b*y=c的直线有多少个,即相同直线有多少个
ll cnt,ans,n;
int main()
{
ios::sync_with_stdio();
int T;
cin>>T;
while(T--){
ans=cnt=;
cin>>n;
mp1.clear(),mp2.clear();
for(int i=;i<=n;i++){
ll x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
ll a=x1-x2,b=y1-y2,c=x1*y2-x2*y1;
ll g=__gcd(a,b);
a/=g;b/=g;c/=g;
mp1[P(a,b)]++;
mp2[Pi(P(a,b),c)]++;
ans+=i-+mp2[Pi(P(a,b),c)]-mp1[P(a,b)];//假设与前i-1条边都相交,需要减去与他平行而不重合的线段
}
cout<<ans<<endl;
}
return ;
}

C - Line-line Intersection Gym - 102220C(线段相交)的更多相关文章

  1. POJ 1410 Intersection --几何,线段相交

    题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...

  2. Line-line Intersection Gym - 102220C

    题目链接:https://vjudge.net/problem/Gym-102220C 题意:求n 条直线两两相交有几对(也可以重合). 思路:用map和pair存所有直线的斜率和与X轴的交点,假设与 ...

  3. 简单几何(线段相交) POJ 1410 Intersection

    题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...

  4. poj 1269 线段相交/平行

    模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = #include <cmath> #include <cstdio> #include <iostrea ...

  5. HDU 4606 Occupy Cities ★(线段相交+二分+Floyd+最小路径覆盖)

    题意 有n个城市,m个边界线,p名士兵.现在士兵要按一定顺序攻占城市,但从一个城市到另一个城市的过程中不能穿过边界线.士兵有一个容量为K的背包装粮食,士兵到达一个城市可以选择攻占城市或者只是路过,如果 ...

  6. POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)

    Geometric Shapes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1243   Accepted: 524 D ...

  7. POJ 1410 Intersection(判断线段交和点在矩形内)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9996   Accepted: 2632 Desc ...

  8. poj1127 Jack Straws(线段相交+并查集)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Jack Straws Time Limit: 1000MS   Memory L ...

  9. poj 1269 线段与线段相交

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13605   Accepted: 60 ...

随机推荐

  1. Python logging模块 控制台、文件输出

    步骤 导入logging模块 设置level(此处是DEBUG) 添加文件handler和流handler import logging logger=logging.getLogger(__name ...

  2. 【Linux】linux磁盘管理

    在服务器管理中,我们会关心硬盘用了多少,还有多少剩余空间,哪些文件占用空间最大等等.以便我们在合适的时机为服务器添加硬盘分区以及管理磁盘文件等操作,让磁盘的利用率最大化,现在我们看下linux系统中和 ...

  3. 18 12 14 python提高 装饰器

    ---恢复内容开始--- 装饰器还真的有些没看懂  一个任意传参的装饰器   一个通用装饰器 def set_func(func): print("------开始进行装饰") # ...

  4. sort()函数使用详解

    使用时需要导入头文件<algorithm> #include<algorithm> 语法描述:sort(begin,end,cmp),cmp参数可以没有,如果没有默认非降序排序 ...

  5. h5-全屏插件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 79.常用的返回QuerySet对象的方法使用详解: filter, exclude,annotate

    返回新的QuerySet的常用方法: 1.filter: 将满足条件的数据提取出来,返回一个新的QuerySet 以下所使用的模型article,category,定义模型models.py文件中,示 ...

  7. hook鼠标

    library dllMouse; uses SysUtils, Classes, UnitHookDLL in 'UnitHookDLL.pas', UnitHookConst in 'UnitHo ...

  8. 线段树--线段树【模板1】P3372

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入格式 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. 第二行包含 ...

  9. 微信小程序下载图片到本地

    downloadImg: function(e){ //触发函数 console.log(e.currentTarget.dataset.url) wx.downloadFile({ url: e.c ...

  10. vue项目准备2

    单文件组件与路由 .vue结尾的文件都是单文件组件 路由就是根据网址的不同返回的页面不同 多页应用与单页应用 多页应用: 每次页面跳转,服务器都会返回一个html. 优点:首次展现页面快.搜索引擎排名 ...