C - Line-line Intersection Gym - 102220C(线段相交)
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
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
1
0
1
题解:两条直线不平行必相交,若平行:若重合答案加1,否则不算。
#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(线段相交)的更多相关文章
- POJ 1410 Intersection --几何,线段相交
题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...
- Line-line Intersection Gym - 102220C
题目链接:https://vjudge.net/problem/Gym-102220C 题意:求n 条直线两两相交有几对(也可以重合). 思路:用map和pair存所有直线的斜率和与X轴的交点,假设与 ...
- 简单几何(线段相交) POJ 1410 Intersection
题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...
- poj 1269 线段相交/平行
模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = #include <cmath> #include <cstdio> #include <iostrea ...
- HDU 4606 Occupy Cities ★(线段相交+二分+Floyd+最小路径覆盖)
题意 有n个城市,m个边界线,p名士兵.现在士兵要按一定顺序攻占城市,但从一个城市到另一个城市的过程中不能穿过边界线.士兵有一个容量为K的背包装粮食,士兵到达一个城市可以选择攻占城市或者只是路过,如果 ...
- POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)
Geometric Shapes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1243 Accepted: 524 D ...
- POJ 1410 Intersection(判断线段交和点在矩形内)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9996 Accepted: 2632 Desc ...
- poj1127 Jack Straws(线段相交+并查集)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Jack Straws Time Limit: 1000MS Memory L ...
- poj 1269 线段与线段相交
Intersecting Lines Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13605 Accepted: 60 ...
随机推荐
- 面向对象变成(OOP)-创建类和使用类
1.1.1对象的抽象:抽象是一种归纳或总结,对象是现实世界物体特征的实例. (1)一切皆是对象. (2)类是对象的抽象. 1.1.2 对象的使用: 当对象被抽象为类以后,就可以创建具体的实例来操作了. ...
- python全局变量、回调函数
1.python全局变量相关概念及使用 来自菜鸟教程上的例子: http://www.runoob.com/python3/python3-function.html 一.python入参需要注意地方 ...
- MySql 的操作指令(window)
1.登录: mysql -uroot -p 2.查看所有数据库: show databases 3.切换数据库 : use databasename(数据库名称) 4.查看数据库的所有表格 ...
- MySQL-TPS,QPS到底是什么
计算TPS,QPS的方式 qps,tps是衡量数据库性能的关键指标,网上普遍有两种计算方式 TPS,QPS相关概念 QPS:Queries Per Second 查询量/秒,是一台服务 ...
- postman批量接口测试注意事项
1.使用cvs文件 导入文件后最后行出现\r符号 用文本打开 删除最后一行空白行 2.打印cvs文件中的接口调用的参数 Pre-request Script: var beginDate=data.b ...
- PAT A1005-1008
A 1005 Spell It Right (20 point(s)) 25分的题目,比较简单,注意N的范围,用字符串处理即可. #include <iostream> #include ...
- 向mysql数据库中插入数据时显示“Duplicate entry '1′ for key ‘PRIMARY' ”错误
错误情况如题,出现这个错误的原因十分简单: 很明显,这是主键的问题. 在一张数据表中是不能同时出现多个相同主键的数据的 这就是错误的原因,解决的方法: 1.可以将这张表设置成无主键(mysql支持,其 ...
- XML技术详解
XML 1.XML概述 XML可扩展标记语言是一种基于文本的语言用作应用程序之间的通信模式,是一个非常有用的描述结构化信息的技术.XML工具使得转化和处理数据变得十分容易,但同样也要领域相关的标准和代 ...
- [C/C++]C/C++计算代码的运行时间
有很多时候,实现一个功能后可能不仅仅要效果,还要效率,如果可以在极短的时间内完成一个功能那当然是最好不过的啦,但是可能经常会事与愿违. 这里就写一下,都可以怎样用C/C++或者Qt的方法来测试代码的运 ...
- os发展史
01. 操作系统的发展历史 1.1 Unix 1965 年之前的时候,电脑并不像现在一样普遍,它可不是一般人能碰的起的,除非是军事或者学院的研究机构,而且当时大型主机至多能提供30台终端(30个键盘. ...