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 简单字符串字典加密

    1 def crypt(source,key): from itertools import cycle result='' temp=cycle(key) for ch in source: res ...

  2. VUE.js入门学习(2)-基础精讲

    1.VUE 实例 - 一个项目是有很多的vue实例拼装的.每一个组建就是vue的实例. var vm = new Vue() 2.VUE 实例生命周期钩子 生命周期函数:VUE实例在某一个时间点会自动 ...

  3. jvm调优原则

    合理规划jvm性能调优 JVM性能调优涉及到方方面面的取舍,往往是牵一发而动全身,需要全盘考虑各方面的影响.但也有一些基础的理论和原则,理解这些理论并遵循这些原则会让你的性能调优任务将会更加轻松.为了 ...

  4. Scheduled定时任务器在Springboot中的使用

    Scheduled定时任务器是Spring3.0以后自带的一个定时任务器. 使用方式: 1.添加依赖 <!-- 添加 Scheduled 坐标 --> <dependency> ...

  5. vue 中使用 vue-fullpage

    安装并使用 安装 npm install --save vue-fullpage.js 引入 // 引用fullpage 插件 import Vue from 'vue' import 'fullpa ...

  6. hook鼠标键盘记录和回放

    unit Unit1; // download by http://www.codefans.net interface uses Windows, Messages, SysUtils, Class ...

  7. Java之创建线程的方式四:使用线程池

    import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.c ...

  8. 第二季第六天 part2 css动画

    transition:background 2s,width 3s(第二个参数为变化时间) 1s(第三个参数为延迟时间): class:hover {}  伪类,鼠标移上去一个变化 <img  ...

  9. Python 字典列表嵌套输入问题

  10. 【论文笔记系列】AutoML:A Survey of State-of-the-art (下)

    [论文笔记系列]AutoML:A Survey of State-of-the-art (上) 上一篇文章介绍了Data preparation,Feature Engineering,Model S ...