Segment set

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 4

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

A segment and all segments which are connected with it compose a segment set. The size of a segment set is the number of segments in it. The problem is to find the size of some segment set.

Input

In the first line there is an integer t - the number of test
case. For each test case in first line there is an integer n (n<=1000) - the
number of commands.

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.

Output

For each Q-command, output the answer. There is a blank line
between test cases.

Sample Input

1
10
P 1.00 1.00 4.00 2.00
P 1.00 -2.00 8.00 4.00
Q 1
P 2.00 3.00 3.00 1.00
Q 1
Q 3
P 1.00 4.00 8.00 2.00
Q 2
P 3.00 3.00 6.00 -2.00
Q 5

Sample Output

1
2
2
2
5
http://pic002.cnblogs.com/images/2011/287127/2011080416290750.jpg
做之前学习一下

 先进行快速排斥实验;再进行跨立实验

 
 

如果  p1 × p2 为正数,则相对原点(0,0)来说, p1 p 2 的顺时针方向; 如果p 1  × p2为负数,则p 1 在p 2 的逆时针方向。如果p 1× p =0,则p 1和p 2 模相等且共线,方向相同或相反。

#include <iostream>
#include<algorithm>
using namespace std;
struct node
{
double x,y; }dian1[],dian2[];
int par[];
int num[];
int findi(int x)
{
if(par[x]==x)
return x;
return par[x]=findi(par[x]);
}
void unioni(int x,int y)
{
int xx=findi(x);
int yy=findi(y);
if(xx!=yy)
{
par[xx]=yy;
num[yy]=num[xx]+num[yy]; }
}
double diancheng(node a,node b,node c)
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
int panduan(node a,node b,node c, node d)
{
int minpx=min(a.x,b.x);
int minpy=min(a.y,b.y);
int minqx=min(c.x,d.x);
int minqy=min(c.y,d.y);
int minux=max(minpx,minqx);
int minuy=max(minpy,minqy); int maxpx=max(a.x,b.x);
int maxpy=max(a.y,b.y);
int maxqx=max(c.x,d.x);
int maxqy=max(c.y,d.y);
int maxux=min(maxpx,maxqx);
int maxuy=min(maxpy,maxqy); if(minux>maxux||minuy>maxuy)
return ;
if(diancheng(a,b,c)*diancheng(a,b,d)>)
return ;
if(diancheng(c,d,b)*diancheng(c,d,a)>)
return ;
return ; }
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=;i<=n;i++)
{
par[i]=i;
num[i]=;
}
char a;
int k=;
while(n--)
{
cin>>a;
if(a=='P')
{
cin>>dian1[k].x>>dian1[k].y>>dian2[k].x>>dian2[k].y;
for(int i=;i<k;i++)
{
if(panduan(dian1[i],dian2[i],dian1[k],dian2[k]))
{
unioni(i,k);
}
}
k++;
}
else
{
int p;
cin>>p;
int pp=findi(p);
cout<<num[pp]<<endl;
}
}
if(T)
cout<<endl;
}
return ;
}

一开始没明白为什么跨立实验就可以解决 为什么要用快速排斥

应为点乘等于0;有3种可能。

而通过了快速排斥试验,所以上图左边的情况是不可能出现的,只会出现右边的两种情况。

左右2种都是 满足相交

Segment set(线段并查集)的更多相关文章

  1. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  2. 2018.08.02 hdu1558 Segment set(并查集+计算几何)

    传送门 这个直接用并查集维护. 每加入一条线段就将它与其他能相交的集合合并,维护一个size" role="presentation" style="posit ...

  3. HDU 1558 Segment set (并查集+线段非规范相交)

    题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. #include <cstdio> #inclu ...

  4. HDU 1558 Segment set(并查集)

    题意: 给你一些线段的起点和终点的坐标,最后问和某个线段相连的或者间接相连的线段有多少个(包括本身)? P X1 Y1X2 Y2  起点(X1,X2)终点(X2,Y2):按照出现次数依次编号为1,2, ...

  5. hdu 1558 Segment set 计算几何+并查集★

    #include <cstdio> #include <iostream> #include <string.h> using namespace std; ; # ...

  6. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  7. 判断线段相交(hdu1558 Segment set 线段相交+并查集)

    先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...

  8. hdu 1558 (线段相交+并查集) Segment set

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...

  9. HDU 1558 Segment set( 判断线段相交 + 并查集 )

    链接:传送门 题意:输入一个数 n 代表有 n 组操作,P 是在平面内加入一条线段,Q x 是查询第 x 条线段所在相交集合的线段个数 例如:下图 5 与 1.2 相交,1 与 3 相交,2 与 4 ...

随机推荐

  1. HDU 5143 NPY and arithmetic progression(思维)

    http://acm.hdu.edu.cn/showproblem.php?pid=5143 题意: 给定数字1,2,3,4.的个数每个数字能且仅能使用一次,组成多个或一个等差数列(长度大于等于3), ...

  2. JS进阶系列之this (call、apply、bind)

    在javascript中,this的指向是在执行上下文的创建阶段确定的,其实只要知道不同执行方式下,this的指向分别是是什么,就能很好的掌握this这个让人摸不透的东西. 一.全局执行 全局执行又分 ...

  3. PHPsession工作机制以及销毁session

  4. Qt5学习记录:QString与int值互相转换

    1)QString转int 直接调用toInt()函数 例: QString str("100"); int tmp = str.toInt(); 或者: bool ok; QSt ...

  5. Spring依赖

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. C# 调用Windows图片查看器

    /// <summary> /// 查看原图 /// </summary> /// <param name="image"></param ...

  7. IIS客户端没有权限

    运行CMDicacls c:\ /setintegritylevel M

  8. Linux CentOS 7 安装mongoDB(4.0.6)

    在官网下载安装包: https://www.mongodb.com/download-center/community 或者通过wget下载 wget https://fastdl.mongodb.o ...

  9. mysql / sqlserver / oracle 常见数据库分页

    空闲时间里用着mysql学习开发测试平台和测试用具, 在公司里将可用的测试平台部署,将数据库换成sqlserver 巴望着能去用oracle的公司 mysql中的分页 limit是mysql的语法se ...

  10. Java中String类型细节

    Java中String类型细节 一 . String两种初始化方式 1 . String str1= “abc”;//String类特有的创建字符对象的方式,更高效 在字符串缓冲区中检测”abc”是否 ...