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. Linux命令之sudo

    在 Linux  系统中,由于 root 的权限过大,一般情况下都不使用它.只有在一些特殊情况下才采用登录root 执行管理任务,一般情况下临时使用 root 权限多采用 su 和 sudo 命令. ...

  2. JS加载获取父窗体传递的参数

    JS加载获取父窗体传递的参数 $(document).ready(function () { var query = location.search.substring(1); var values ...

  3. 【Selenium2】【Python多线程】

    # all_tests_pro.py import unittest,time,os,multiprocessingimport HTMLTestRunner #查找多有含有thread的文件,文件夹 ...

  4. 关于System.in如何执行的问题

    import java.io.IOException; public class Test1 { public static void main(String[] args) throws IOExc ...

  5. Mongodb 创建管理员帐号与普通帐号

    数据库操作权限 readAnyDatabase 任何数据库的只读权限 userAdminAnyDatabase 任何数据库的读写权限 userAdminAnyDatabase 任何数据库用户的管理权限 ...

  6. Python使用win32com实现的模拟浏览器功能

    # -*- coding:UTF- -*- #!/user/bin/env python ''' Created on -- @author: chenzehe ''' import win32com ...

  7. Ubuntu 下 Python自由切换

    sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo update-alternati ...

  8. Android IPC 结篇

    一.概述 Android 的 IPC 方式有 Bundle .共享文件.AIDL .Messenger .ContentProvider .Socket ,我们在实现进程间通信时要选择哪一种方式来实现 ...

  9. 力扣(LeetCode) 9.回文数

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...

  10. Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务

    前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息“hello world”. 但自己的配置文件中必须配置config serve ...