POJ 1436 Horizontally Visible Segments(线段树区间更新)

太过爱你忘了你带给我的痛 2022-06-10 00:09 262阅读 0赞

There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they can be connected by a horizontal line segment that does not have any common points with other vertical segments. Three different vertical segments are said to form a triangle of segments if each two of them are horizontally visible. How many triangles can be found in a given set of vertical segments?

Task

Write a program which for each data set:

reads the description of a set of vertical segments,

computes the number of triangles in this set,

writes the result.

Input

The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 20. The data sets follow.

The first line of each data set contains exactly one integer n, 1 <= n <= 8 000, equal to the number of vertical line segments.

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:

yi’, yi’’, xi - y-coordinate of the beginning of a segment, y-coordinate of its end and its x-coordinate, respectively. The coordinates satisfy 0 <= yi’ < yi’’ <= 8 000, 0 <= xi <= 8 000. The segments are disjoint.

Output

The output should consist of exactly d lines, one line for each data set. Line i should contain exactly one integer equal to the number of triangles in the i-th data set.

Sample Input

  1. 1
  2. 5
  3. 0 4 4
  4. 0 3 1
  5. 3 4 2
  6. 0 2 2
  7. 0 2 3

Sample Output

  1. 1

题解:

先说题意:

给你一堆平行于y轴的线段的下端点y1,上端点y2,和横坐标x,让你求有多少对3个”两两可见”的线段,所谓两两可见就是两条线段可以被同一根水平线穿过而且之间不穿过其他线

思路:

由于题目意思一开始就不太明白之间就搜了题解,所以不是自己想的。。只说一下我的理解

由于处于同一个x的两个相邻线段也是互相可见,我们将线段的纵坐标都乘上2来处理线段覆盖问题,然后就是将线段按照x轴从小到大排序,然后先询问区间,询问结果用一个二维数组再保存,然后再插入更新,最后暴力三重循环来求个数。

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<string>
  7. #include<stdio.h>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<deque>
  12. #define M (t[k].l+t[k].r)/2
  13. #define lson k*2
  14. #define rson k*2+1
  15. using namespace std;
  16. struct node
  17. {
  18. int l,r;
  19. int v;//存之前被那条线覆盖
  20. }t[16000*4];
  21. bool p[8005][8005];//存可见情况
  22. void Build(int l,int r,int k)
  23. {
  24. t[k].v=-1;//初始-1
  25. t[k].l=l;
  26. t[k].r=r;
  27. if(l==r)
  28. return;
  29. int mid=M;
  30. Build(l,mid,lson);
  31. Build(mid+1,r,rson);
  32. }
  33. void pushdown(int k)//向下更新
  34. {
  35. if(t[k].v!=-1)
  36. {
  37. t[lson].v=t[rson].v=t[k].v;
  38. t[k].v=-1;
  39. }
  40. }
  41. void update(int l,int r,int k,int v)区间更新
  42. {
  43. if(t[k].l==l&&t[k].r==r)
  44. {
  45. t[k].v=v;
  46. return;
  47. }
  48. pushdown(k);
  49. int mid=M;
  50. if(r<=mid)
  51. update(l,r,lson,v);
  52. else if(l>mid)
  53. update(l,r,rson,v);
  54. else
  55. {
  56. update(l,mid,lson,v);
  57. update(mid+1,r,rson,v);
  58. }
  59. }
  60. void query(int l,int r,int k,int v)//变相询问
  61. {
  62. if(t[k].v!=-1)//如果有线段覆盖了该段
  63. {
  64. p[t[k].v][v]=true;//注意是单向的,因为一对只算一次
  65. return;
  66. }
  67. if(t[k].l==t[k].r)
  68. return;
  69. pushdown(k);
  70. int mid=M;
  71. if(r<=mid)
  72. query(l,r,lson,v);
  73. else if(l>mid)
  74. query(l,r,rson,v);
  75. else
  76. {
  77. query(l,mid,lson,v);
  78. query(mid+1,r,rson,v);
  79. }
  80. }
  81. struct edge
  82. {
  83. int x,y1,y2;
  84. }a[8005];
  85. int cmp(edge a,edge b)//按照x来排序
  86. {
  87. return a.x<b.x;
  88. }
  89. int main()
  90. {
  91. int i,j,k,n,m,test,ans;
  92. scanf("%d",&test);
  93. while(test--)
  94. {
  95. scanf("%d",&n);
  96. Build(0,16000,1);
  97. memset(p,0,sizeof(p));
  98. for(i=0;i<n;i++)
  99. {
  100. scanf("%d%d%d",&a[i].y1,&a[i].y2,&a[i].x);
  101. a[i].y1*=2;
  102. a[i].y2*=2;//乘上2处理
  103. }
  104. sort(a,a+n,cmp);//排序
  105. for(i=0;i<n;i++)
  106. {
  107. query(a[i].y1,a[i].y2,1,i);
  108. update(a[i].y1,a[i].y2,1,i);
  109. }
  110. ans=0;
  111. for(i=0;i<n;i++)
  112. {
  113. for(j=0;j<n;j++)
  114. {
  115. if(p[i][j])//如果可见
  116. {
  117. for(k=0;k<n;k++)
  118. {
  119. if(p[i][k]&&p[j][k])
  120. {
  121. ans++;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. printf("%d\n",ans);
  128. }
  129. return 0;
  130. }

发表评论

表情:
评论列表 (有 0 条评论,262人围观)

还没有评论,来说两句吧...

相关阅读