ACM1558两线段相交判断和并查集
Segment set

There are two different commands described in different format shown below:
P x1 y1 x2 y2 - paint a segment whose coordinates of the two endpoints are (x1,y1),(x2,y2).
Q k - query the size of the segment set which contains the k-th segment.
k is between 1 and the number of segments in the moment. There is no segment in the plane at first, so the first command is always a P-command.
P 2.00 3.00 3.00 1.00
本题主要难关是判断量条线段是否相交,在我这篇文章里有详细介绍判断两条线段相交的方法。http://www.cnblogs.com/sytu/articles/3876585.html;
Notice:二维向量的叉乘公式是 axb=(x1,y1)x(x2,y2)=x1*y2-y1*x2;
下面为AC代码:
#include<iostream>
using namespace std;
#define MIN(x,y) (x < y ? x : y)
#define MAX(x,y) (x > y ? x : y)
typedef struct
{
double x,y;
} Point;
struct LINE
{
double x1,x2,y1,y2;
int flag;
};
LINE *line;
int lineintersect(int a,int b)
{
Point p1,p2,p3,p4;
p1.x=line[a].x1;p1.y=line[a].y1;
p2.x=line[a].x2;p2.y=line[a].y2;
p3.x=line[b].x1;p3.y=line[b].y1;
p4.x=line[b].x2;p4.y=line[b].y2; Point tp1,tp2,tp3,tp4,tp5,tp6;
if ((p1.x==p3.x&&p1.y==p3.y)||(p1.x==p4.x&&p1.y==p4.y)||(p2.x==p3.x&&p2.y==p3.y)||(p2.x==p4.x&&p2.y==p4.y))
return ;
//快速排斥试验
if(MAX(p1.x,p2.x)<MIN(p3.x,p4.x)||MAX(p3.x,p4.x)<MIN(p1.x,p2.x)||MAX(p1.y,p2.y)<MIN(p3.y,p4.y)||MAX(p3.y,p4.y)<MIN(p1.y,p2.y))
return ;
//跨立试验
tp1.x=p1.x-p3.x;
tp1.y=p1.y-p3.y;
tp2.x=p4.x-p3.x;
tp2.y=p4.y-p3.y;
tp3.x=p2.x-p3.x;
tp3.y=p2.y-p3.y;//从这到上面是一组的向量
tp4.x=p2.x-p4.x;//下面是一组的向量
tp4.y=p2.y-p4.y;
tp5.x=p2.x-p3.x;
tp5.y=p2.y-p3.y;
tp6.x=p2.x-p1.x;
tp6.y=p2.y-p1.y;
if ((tp1.x*tp2.y-tp1.y*tp2.x)*(tp2.x*tp3.y-tp2.y*tp3.x)>=) //此处用到了叉乘公式
{
if((tp4.x*tp6.y-tp4.y*tp6.x)*(tp6.x*tp5.y-tp6.y*tp5.x)>=)
return ;
}
return ;
}
int find(int x)
{
if(>line[x].flag)return x;
return line[x].flag=find(line[x].flag);
}
void Union(int a,int b)
{
int fa=find(a);
int fb=find(b);
if(fa==fb)return;
int n1=line[fa].flag;
int n2=line[fb].flag;
if(n1>n2)
{
line[fa].flag=fb;
line[fb].flag+=n1;
}
else
{
line[fb].flag=fa;
line[fa].flag+=n2;
}
}
void throwinto(int n)
{
if(n>)
{
for(int i=;i<n;i++)
{
if(lineintersect(i,n))
Union(i,n);
}
}
}
int main()
{
int t;
cin>>t;
int n,q;
int cn=t;
while(t--)
{ int cnt=;
cin>>n;
line=new LINE[n+];
for(int i=;i<=n;i++)
{
line[i].flag=-;
}
for(int i=;i<=n;i++)
{
char sj;
cin>>sj;
if(sj=='P')
{
cin>>line[cnt].x1>>line[cnt].y1>>line[cnt].x2>>line[cnt].y2;
throwinto(cnt);
cnt++;
}
else if(sj=='Q')
{
cin>>q;
int re=find(q);
cout<<-line[re].flag<<endl;
}
}
if(t>)cout<<endl;//注意格式问题,容易出错
}
return ;
}
ACM1558两线段相交判断和并查集的更多相关文章
- poj 1127:Jack Straws(判断两线段相交 + 并查集)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2911 Accepted: 1322 Descr ...
- You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...
- hdu 1147:Pick-up sticks(基本题,判断两线段相交)
Pick-up sticks Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- POJ_1227 Jack Straws 【二维平面判两线段相交】
一 题面 POJ1127 二 分析 在平面几何中,判断两线段相交的方法一般是使用跨立实验.但是这题考虑了非严格相交,即如何两个线段刚好端点相交则也是相交的,所以还需要使用快速排斥实验. 这里参考并引用 ...
- 【BZOJ4025】二分图(线段树分治,并查集)
[BZOJ4025]二分图(线段树分治,并查集) 题面 BZOJ 题解 是一个二分图,等价于不存在奇环. 那么直接线段树分治,用并查集维护到达根节点的距离,只计算就好了. #include<io ...
- 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)
[CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...
- poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)
Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...
- hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)
Geometric Shapes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1243 Accepted: 524 D ...
随机推荐
- 树(Tree,UVA 548)
题目描述: 题目思路: 1.使用数组建树 //递归 2.理解后序遍历和中序遍历,建立左右子树 3.dfs深度搜索找出权重最小的路径 #include <iostream> #include ...
- 【转】MMO即时战斗:技能实现
转自 http://blog.csdn.net/cyblueboy83/article/details/41628743 一.前言 基本所有MMO游戏无论是回合制.策略类.即时战斗等等类型都需要有相应 ...
- [Clr via C#读书笔记]Cp3共享程序集和强命名程
Cp3共享程序集和强命名程序集 私有方式部署+全局方式部署:弱命名程序集+强命名程序集 强命名程序集使用发布者的公钥私钥进行签名,唯一标识发布者. 共享dll被全部复制到System32中 强命名程序 ...
- java对json文件的操作
第一步:通过FileReader读取json文件第二步:使用BufferReader,先通过I/O读取一定大小的数据缓存到数组中,然后再从数组取出数据.第三步:用一个字符串把每次传来的数据处理后写到新 ...
- 对Objective-C中runtime的理解
Objective-C是面向runtime(运行时)的语言,在应用程序运行的时候来决定函数内部实现什么以及做出其它决定的语言.程序员可以在程序运行时创建,检 查,修改类,对象和它们的方法,Object ...
- 探讨C++实现一个不可被继承的类
C#和Java都提供了一种机制让一个类不能被继承,如C#中的sealed关键字和Java的final关键字,然而C++程序员就没这么好命了.不过C++也可以模拟出这种效果,原理基于:子类的构造函数会自 ...
- 【ADO.NET】SqlBulkCopy批量添加DataTable
使用事务和SqlBulkCopy批量插入数据 SqlBulkCopy是.NET Framework 2.0新增的类,位于命名空间System.Data.SqlClient下,主要提供把其他数据源的数据 ...
- C++基础知识(一)
C++中头文件中class的两个花括号后面要加上分号,否则会出现很多的莫名奇妙的错误. 一. 每一个C++程序(或者由多个源文件组成的C++项目)都必须包含且只有一个main()函数.对于预处理指令, ...
- 【bzoj1737】[Usaco2005 jan]Naptime 午睡时间 dp
题目描述 Goneril is a very sleep-deprived cow. Her day is partitioned into N (3 <= N <= 3,830) equ ...
- BZOJ4300 绝世好题(动态规划)
设f[i][j]为前i个数中所选择的最后一个数在第j位上为1时的最长序列长度,转移显然. #include<iostream> #include<cstdio> #includ ...