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.  
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.  
1
2
2
2
5
这题可以用并查集做,主要是要判断两条线段是不是相交,我的方法是记两条线段的端点是p1(x1,y1),p2(x2,y2)以及p3(x3,y3),p4(x4,y4),分别看4个端点有没有在另一条线段上,如果有,那么相交,如果没有,那么再用叉积判断每条线段的两个端点是不是在另一条线段的两边,如果p1,p2在线段p3p4的两边而且p3,p4在线段p1p2的两边,那么p1p2和p3p4相交,否则不相交。那么怎么判断p1,p2是否在线段p3p4的两边呢,这里用到了叉积,第一次取p1,p3,p4,如果向量p1p3和向量p1p4的叉积是正的,那么p1p2在p3p4的顺时针方向,反之在逆时针方向。再取p2,p3,p4,方法相同,如果两个叉积符号不同,那么p1,p2在线段p3p4的两边,否则不在。
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. int pre[1006];
  5. int find(int x){
  6. int r=x;
  7. while(r!=pre[r])r=pre[r];
  8. return r;
  9. }
  10. int shizhen(double x2,double y2,double x4,double y4,double x5,double y5){
  11. double x=x4-x2,y=y4-y2,xx=x5-x2,yy=y5-y2;
  12. if(x*yy-xx*y>0)return 1;
  13. else return -1;
  14. }
  15. int xianshang(double x2,double y2,double x3,double y3,double x4,double y4){
  16. if((x2-x3)*(y4-y2)-(y2-y3)*(x4-x2)!=0)return 0;
  17. if((x2<x3 && x2<x4) || (x2>x3 && x2>x4))return 0;
  18. return 1;
  19. }
  20. int panduan(double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5)
  21. {
  22. if(xianshang(x2,y2,x4,y4,x5,y5) || xianshang(x3,y3,x4,y4,x5,y5) || xianshang(x4,y4,x2,y2,x3,y3) || xianshang(x5,y5,x2,y2,x3,y3))return 1;
  23. if((shizhen(x2,y2,x4,y4,x5,y5)*shizhen(x3,y3,x4,y4,x5,y5)<0) && (shizhen(x4,y4,x2,y2,x3,y3)*shizhen(x5,y5,x2,y2,x3,y3)<0))return 1;
  24. return 0;
  25. }
  26. int main()
  27. {
  28. int T,n,m,i,j,a,t1,xianduan[1006],num[1005],num1,h;
  29. double x2[1006],x3[1006],y2[1006],y3[1006];
  30. char s[10];
  31. scanf("%d",&T);
  32. for(h=1;h<=T;h++)
  33. {
  34. scanf("%d",&n);
  35. num1=0;
  36. for(i=1;i<=n;i++){
  37. pre[i]=i;num[i]=0;
  38. }
  39. for(i=1;i<=n;i++){
  40. scanf("%s",s);
  41. if(s[0]=='P'){
  42. num1++;
  43. scanf("%lf%lf%lf%lf",&x2[num1],&y2[num1],&x3[num1],&y3[num1]);
  44. num[num1]=1;
  45. for(j=1;j<=num1-1;j++){
  46. if(panduan(x2[j],y2[j],x3[j],y3[j],x2[num1],y2[num1],x3[num1],y3[num1])){
  47. t1=find(j);
  48. if(num1!=t1){
  49. pre[t1]=num1;num[num1]+=num[t1];
  50. }
  51. else continue;
  52. }
  53. //printf("%d ",panduan(x2[j],y2[j],x3[j],y3[j],x2[num1],y2[num1],x3[num1],y3[num1]));
  54. }
  55. //printf("\n");
  56. }
  57. else if(s[0]=='Q'){
  58. scanf("%d",&a);
  59. t1=find(a);
  60. printf("%d\n",num[t1]);
  61. }
  62. }
  63. if(h!=T)printf("\n");
  64. /*for(i=1;i<=5;i++){
  65. t1=find(i);
  66. printf("%d ",num[t1]);
  67. }
  68. printf("\n");*/
  69. }
  70. return 0;
  71. }

hdu1558 Segment set的更多相关文章

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

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

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

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

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

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

  4. 【计算几何初步-线段相交+并查集】【HDU1558】Segment set

    Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. kafka的log存储解析——topic的分区partition分段segment以及索引等

    转自:http://blog.csdn.net/jewes/article/details/42970799 引言 Kafka中的Message是以topic为基本单位组织的,不同的topic之间是相 ...

  6. ORA-10635: Invalid segment or tablespace type

    上周星期天在迁移数据时,碰到了ORA-10635: Invalid segment or tablespace type 错误,当时的操作环境如下: 操作系统版本: [oracle@xxxxx scr ...

  7. Oracle逻辑结构(TableSpace→Segment→Extent→Block)

    一.逻辑体系结构图 二.逻辑结构图组成介绍 从上表可以看出,一个数据库是由多个表空间(tablespace)组成,一个表空间又由多个段(segment)组成,一个段又由多个区(extent)组成,一个 ...

  8. IOS开发--自定义segment控件,方便自定义样式

    系统的segment控件太封闭,想换个颜色加个背景太难了,忍不住自己写一个,以备不时之需 这个控件给出了很多自定义属性的设置,用起来还是比较方便的,需要注意的 itemWidth如果不设置,则会按照控 ...

  9. 填坑*** WARNING L15: MULTIPLE CALL TO SEGMENT

    填坑*** WARNING L15: MULTIPLE CALL TO SEGMENT 警告:发生了重入! 解释:在主循环里调用了一个函数,而在中断服务中又一次调用了同样的函数.当主循环运行到该函数中 ...

随机推荐

  1. 【剑指 Offer】05.替换空格

    题目描述 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 示例 1: 输入:s = "We are happy." 输出:"We%20a ...

  2. LeetCode226 翻转二叉树

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题  ...

  3. (数据科学学习手札103)Python+Dash快速web应用开发——页面布局篇

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...

  4. Linux学习笔记 | 常见错误之账户密码正确但是登录不进去系统

    前言: 笔者今日由于Linux版本的原因,需要Linux内核版本不能太高的系统,而日常使用的ubuntu系统不能满足需求,于是新建了一个虚拟机,选用的系统是Ubuntu16的,配置了一下午的各种依赖环 ...

  5. 没搞清楚网络I/O模型?那怎么入门Netty

    微信搜索[阿丸笔记],关注Java/MySQL/中间件各系列原创实战笔记,干货满满. 本文是Netty系列笔记第二篇 Netty是网络应用框架,所以从最本质的角度来看,是对网络I/O模型的封装使用. ...

  6. 【Linux】自建回收站

    linux rm命令是即刻删除的,而且挺多人喜欢加上-f强制命令,更暴力的是删除文件夹直接 rm -rf ,这样子代表你执行完后,就完全被干掉了. 还是推荐在linux下设置回收站,写一个shell脚 ...

  7. Web安全之CSRF(跨站请求伪造)

    CSRF(跨站请求伪造)概述 Cross-site request forgery 简称为"CSRF",在CSRF的攻击场景中攻击者会伪造一个请求(这个请求一般是一个链接),然后欺 ...

  8. SAPLink 非常好用的工具

    对于SAP LINK,如果你想将一个程序完整的保存到本地,包括程序的自定义屏幕.菜单等等,那么请使用这个工具,它能够将一个程序完整的保存下来,并且移植到另一个SAP系统中,用来左程序的迁移和本地保存备 ...

  9. H3C防火墙开启区域间互访

    配置ip和路由以及将端口放至Untrust之后,外网还是不通,需要以下命令 interzone policy default by-priority 或者下面: security-zone intra ...

  10. apache状态显示报错AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdo...is message

    今天启动apache查看状态发现报错,说不能确认服务器完全确认域名,以下是报错内容: [root@localhost ~]# service httpd status Redirecting to / ...