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 ...
随机推荐
- opencv vs2013提示缺少Qedit.h问题
#pragma include_alias( "dxtrans.h", "qedit.h" ) #define __IDxtCompositor_INTERFA ...
- Java集合--list接口
list是一个接口,实现类:Arraylist,Vector,Linkedlist list接口(有序): 常用方法 排除Collection中具有的之外的 添加功能 void add(int ind ...
- jstl中遍历Map
在jstl中遍历Map和遍历List与数组一样,都是使用forEach标签. 例子: <%@ page import="java.util.Map" %> <%@ ...
- 自动化运维工具ansible中常用模块总结
1.yum模块: name:要操作的软件包名字,可以是一个url或者本地rpm包路径,如name=nginx update_cache:更新软件包缓存,如update_cache=yes则更新软件包缓 ...
- HGP|VCG|UK10K|中科院职业人群队列研究计划|药物基因组学
全球性计划:表观组计划:肝计划 测1000人的变异level HGP计划三个阶段,范围逐步扩大和深化. Pilot:deep sequence---low coverage Phase 1 Phase ...
- OpenMP笔记(一)
原文:https://www.bearoom.xyz/2019/02/17/openmp1/ 并行技术有很多种,OpenMP算是比较简单可用的一种,OpenMP全称是 Open Multi-Proce ...
- [腾讯 TMQ] 接口测试用例设计
接口测试 [腾讯 TMQ] 接口测试用例设计 腾讯移动品质中心 · 2018年01月17日 · 最后由 于静 回复于 20 天前 · 21794 次阅读 本帖已被设为精华帖! 目录 作者:刘燕 团队: ...
- JavaScript 之 DOM 与 BOM
DOM是文档对象模型,用来获取或设置文档中标签的属性,例如获取或者设置input表单的value值. 由于DOM的操作对象是文档(Document),所以dom和浏览器没有直接关系. BOM是浏览器对 ...
- gcd--最大公因数
求两个数的最大公倍数 考完五校的第一天,在家补视频ing,简单来说的话就是给了两个数A,B 假设他们两个的最大公倍数为d,那么A=X*d,B=Y*d gcd就是把一直gcd(B%A,A)不断更新,其中 ...
- ubuntu14.10安装gitlab
1 换源: # curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/ ...