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. [从零开始搭网站六]为域名申请免费SSL证书(https),并为Tomcat配置https域名所用的多SSL证书

    点击下面连接查看从零开始搭网站全系列 从零开始搭网站 由于国内的网络环境比较恶劣,运营商流量劫持的情况比较严重,一般表现为别人打开你的网站的时候会弹一些莫名其妙的广告...更过分的会跳转至别的网站. ...

  2. 《深入理解JVM虚拟机》读书笔记

    前言:<深入理解JVM虚拟机>是JAVA的经典著作之一,因为内容更偏向底层,所以之前一直没有好好的阅读过.最近因为刚好有空,又有了新目标.所以打算和<构架师的12项修炼>一起看 ...

  3. 3.git、TortoiseGit的安装、仓库的配置教程

    参考:https://blog.csdn.net/hc_ttxs/article/details/79375788 引言: Git: 就是最原始的分布式版本控制系统,是开源的. GitHub:与Git ...

  4. Integer的最大值

    来自:https://blog.csdn.net/qq_33611068/article/details/77369050 有这样一道题: 编程测试,遍历 0 到 int所能表示最大的正数,将消耗的时 ...

  5. [转][smart3d]Smart3D之手动配置 S3C 索引加载全部的OSGB瓦片数据

    转自:https://blog.csdn.net/u013719339/article/details/77840728/ 一.须知: S3C是Smart3D内部格式,实质上是一个分块模型的索引,可以 ...

  6. rm

    rm [选项]... 目录... 删除指定的<文件>(即解除链接). -d      --directory    删除可能仍有数据的目录 (只限超级用户)-f      --force  ...

  7. Debug记录(1)

    今天下午在给nRF52832写程序时,莫名遇到了这个错误 错误id是一个很奇怪的数. 原代码如下: static void timers_init(void) { uint32_t timer_err ...

  8. LFU Cache

    2018-11-06 20:06:04 LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频率也更高”. ...

  9. Go语言学习之5 进阶-排序、链表、二叉树、接口

    本节主要内容: 1. 结构体和方法2. 接口 1. 结构体和方法 (1). 用来自定义复杂数据结构     (2). struct里面可以包含多个字段(属性)     (3). struct类型可以定 ...

  10. 如何理解机器学习/统计学中的各种范数norm | L1 | L2 | 使用哪种regularization方法?

    参考: L1 Norm Regularization and Sparsity Explained for Dummies 专为小白解释的文章,文笔十分之幽默 why does a small L1 ...