原题链接

Problem Description
Given some segments which are paralleled to the coordinate axis. You need to count the number of their intersection.

The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments with length 0.

 
Input
The first line contains an integer T, indicates the number of test case.

The first line of each test case contains a number n(1<=n<=100000), the number of segments. Next n lines, each with for integers, x1, y1, x2, y2, means the two endpoints of a segment. The absolute value of the coordinate is no larger than 1e9.

 
Output
For each test case, output one line, the number of intersection.
 
Sample Input
2
4
1 0 1 3
2 0 2 3
0 1 3 1
0 2 3 2
4
0 0 2 0
3 0 3 2
3 3 1 3
0 3 0 2
 
Sample Output
4
0
 
题意:输入n条线段(x1,y1)-(x2,y2)  这n条线段平行于坐标轴,不存在重叠部分且不存在两条线段共断点,求交叉点数;
 
思路:输入数据n<=100000,而坐标x,y<1e9,所以首先可以将线段进行离散化,使得所有的坐标点在1~1e5之间,把平行于y轴的线段按x坐标排序,把平行于x轴的线段化作起点与结束点,即用一个结构体struct Node{int x;int y;}node[200005];  保存平行于x轴的线段的信息,例如一条平行于x轴的线段为(x1,y1)-(x2,y2)且y1==y2  则
node[i].x=x1,node[i].y=y1;  node[i+1].x=x2+1,node[i+1].y=0-y1;   这样可以把一段区间上的操作化作点的操作,减小复杂度。
        区间上的操作化作点的操作,什么意思呢?  其实就是在这段区间开始的时候加上这个数,那么在区间中间不必再考虑这条线(这段区间),因为开始已经加上了,最后在区间结束的下一位减去就行。
        最后,在1到2*1e5的循环,用一个一维的树状数组,当循环到i 时,先把关于node[].x==i 的 操作处理完,然后计算在x=i 直线处相交的点数,即为求出在平行于y轴且x==i的区间上的点数,用树状数组可以方便快速实现;
 
代码如下:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <map>
  6. using namespace std;
  7. typedef long long LL;
  8. LL ax[],ay[];
  9. LL c[];
  10. LL Lowbit(LL t)
  11. {
  12. return t&(t^(t-));
  13. }
  14. LL Sum(LL x)
  15. {
  16. LL sum = ;
  17. while(x > )
  18. {
  19. sum += c[x];
  20. x -= Lowbit(x);
  21. }
  22. return sum;
  23. }
  24. void add(LL li,LL d)
  25. {
  26. while(li<)
  27. {
  28. c[li]+=d;
  29. li=li+Lowbit(li);
  30. }
  31. }
  32.  
  33. struct Nodex
  34. {
  35. LL x1,x2;
  36. LL y;
  37. }nodex[];
  38.  
  39. struct Nodey
  40. {
  41. LL y1,y2;
  42. LL x;
  43. }nodey[];
  44.  
  45. struct Node
  46. {
  47. LL x;
  48. LL y;
  49. }node[];
  50.  
  51. bool cmp1(const Nodey s1,const Nodey s2)
  52. {
  53. return s1.x<s2.x;
  54. }
  55.  
  56. bool cmp2(const Node s1,const Node s2)
  57. {
  58. return s1.x<s2.x;
  59. }
  60. map<LL,LL>q1;
  61. map<LL,LL>q2;
  62.  
  63. int main()
  64. {
  65. LL T,n;
  66. scanf("%I64d",&T);
  67. while(T--)
  68. {
  69. q1.clear();
  70. q2.clear();
  71. scanf("%I64d",&n);
  72. LL tot1=,tot2=;
  73. for(LL i=;i<n;i++)
  74. {
  75. LL x1,x2,y1,y2;
  76. scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2);
  77. if(x1==x2)
  78. {
  79. nodey[tot2].y1=min(y1,y2);
  80. nodey[tot2].y2=max(y1,y2);
  81. nodey[tot2++].x=x1;
  82. }
  83. else
  84. {
  85. nodex[tot1].x1=min(x1,x2);
  86. nodex[tot1].x2=max(x1,x2);
  87. nodex[tot1++].y=y1;
  88. }
  89. }
  90.  
  91. LL num1,num2;
  92. for(LL i=;i<tot1;i++)
  93. {
  94. ax[i*]=nodex[i].x1;
  95. ax[i*+]=nodex[i].x2;
  96. }
  97. num1=*tot1;
  98. for(LL i=;i<tot2;i++)
  99. {
  100. ax[num1++]=nodey[i].x;
  101. }
  102. sort(ax,ax+num1);
  103. LL tot=,pre=-;
  104. for(LL i=;i<num1;i++)
  105. {
  106. if(ax[i]!=pre)
  107. {
  108. pre=ax[i];
  109. q1[pre]=++tot;
  110. }
  111. }
  112.  
  113. for(LL i=;i<tot2;i++)
  114. {
  115. ay[i*]=nodey[i].y1;
  116. ay[i*+]=nodey[i].y2;
  117. }
  118. num2=*tot2;
  119. for(LL i=;i<tot1;i++)
  120. ay[num2++]=nodex[i].y;
  121. sort(ay,ay+num2);
  122. tot=,pre=-;
  123. for(LL i=;i<num2;i++)
  124. {
  125. if(ay[i]!=pre)
  126. {
  127. pre=ay[i];
  128. q2[pre]=++tot;
  129. }
  130. }
  131.  
  132. for(LL i=;i<tot1;i++)
  133. {
  134. nodex[i].x1=q1[nodex[i].x1];
  135. nodex[i].x2=q1[nodex[i].x2];
  136. nodex[i].y=q2[nodex[i].y];
  137. }
  138. for(LL i=;i<tot2;i++)
  139. {
  140. nodey[i].y1=q2[nodey[i].y1];
  141. nodey[i].y2=q2[nodey[i].y2];
  142. nodey[i].x=q1[nodey[i].x];
  143. }
  144.  
  145. //for(LL i=0;i<tot1;i++)
  146. //cout<<"x: "<<nodex[i].x1<<" "<<nodex[i].x2<<" "<<nodex[i].y<<endl;
  147. //for(LL i=0;i<tot1;i++)
  148. //cout<<"y: "<<nodey[i].y1<<" "<<nodey[i].y2<<" "<<nodey[i].x<<endl;
  149. sort(nodey,nodey+tot2,cmp1);
  150. tot=;
  151. for(LL i=;i<tot1;i++)
  152. {
  153. node[tot].x=nodex[i].x1;
  154. node[tot++].y=nodex[i].y;
  155. node[tot].x=nodex[i].x2+;
  156. node[tot++].y=-nodex[i].y;
  157. }
  158. sort(node,node+tot,cmp2);
  159. LL j=,k=,sum=;
  160. memset(c,,sizeof(c));
  161. for(LL i=;i<=;i++)
  162. {
  163. while(node[j].x==i&&j<tot)
  164. {
  165. if(node[j].y>)
  166. add(node[j].y,);
  167. else
  168. add(-node[j].y,-);
  169. j++;
  170. }
  171. while(nodey[k].x==i&&k<tot2)
  172. {
  173. sum+=Sum(nodey[k].y2)-Sum(nodey[k].y1-);
  174. k++;
  175. }
  176. if(j>=tot) break;
  177. if(k>=tot2) break;
  178. }
  179. printf("%I64d\n",sum);
  180. }
  181. return ;
  182. }
 

2016暑假多校联合---Counting Intersections的更多相关文章

  1. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  2. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

  3. 2016暑假多校联合---Substring(后缀数组)

    2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...

  4. 2016暑假多校联合---To My Girlfriend

    2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...

  5. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

  6. 2016暑假多校联合---Another Meaning

    2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...

  7. 2016暑假多校联合---Death Sequence(递推、前向星)

    原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...

  8. 2016暑假多校联合---Joint Stacks (STL)

    HDU  5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...

  9. 2016暑假多校联合---GCD

    Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...

随机推荐

  1. paip.为什么使用多线程的原因.

    paip.为什么使用多线程的原因. 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net/att ...

  2. iOS开发----优秀文章推荐

    UI界面 iOS和Android 界面设计尺寸规范  http://www.alibuybuy.com/posts/85486.html iPhone app界面设计尺寸规范  http://www. ...

  3. ServletConfig接口默认是哪里实现的?

    问题:Servlet接口默认是哪里实现的? 答:GenericServlet 1.结构 2.ServletConfig.GenericServlet.HttpServlet的关系如下: public ...

  4. 将Windows系统编译的.NET Core程序发布到Ubuntu系统

    在可移植方面.NET Core应用程序分为两种,Portable application(便捷,需要目标机器安装.NET Core Runtime)和Self-contained applicatio ...

  5. andriod sdk模拟器安装过程中报错

    andriod sdk模拟器安装过程中,出现下述错误: Failed to fetch URL http://dl-ssl.google.com/android/repository/reposito ...

  6. POJ3069 POJ2586 解题报告(异曲同工的贪心算法)

    [POJ 3069](2586见下) 原题在此:http://poj.org/problem?id=3069 题目大意: 一个直线上有N个点.点i的距离是Xi.从这些点中选取若干个加上标记.要求:对于 ...

  7. JSON学习之一

    1.JSON:JavaScript Object Notation(javaScript对象表示法),JSON是存储和交换文本信息的语法,类似XML:JSON比XML更小,更快,更易解析: { &qu ...

  8. Git使用命令

    git init 初始化仓库 git init --bare 初始化一个裸仓库 git branch 查看本地分支 git branch -a 查看全部分支 git remote 远程仓库管理 add ...

  9. javascript类型系统——布尔Boolean类型

    × 目录 [1]定义 [2]应用场景 [3]转为布尔[4]实例方法 前面的话 布尔值Boolean类型可能是三种包装对象Number.String和Boolean中最简单的一种.Number和Stri ...

  10. 后端码农谈前端(CSS篇)第四课:选择器补充(伪类与伪元素)

    一.伪类: 属性 描述 :active 向被激活的元素添加样式. :focus 向拥有键盘输入焦点的元素添加样式. :hover 当鼠标悬浮在元素上方时,向元素添加样式. :link 向未被访问的链接 ...