题意   在坐标系中有n条平行于y轴的线段  当一条线段与还有一条线段之间能够连一条平行与x轴的线不与其他线段相交  就视为它们是可见的  问有多少组三条线段两两相互可见

先把全部线段存下来  并按x坐标排序  线段树记录相应区间从右往左当前可见的线段编号(1...n)  超过一条就为0  然后从左往右对每条线段  先查询左边哪些线段和它是可见的  把可见关系存到数组中  然后把这条线段相应区间的最右端可见编号更新为这条线段的编号  最后暴力统计有多少组即可了

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #define lc p<<1, s, mid
  5. #define rc p<<1|1, mid+1, e
  6. #define mid ((s+e)>>1)
  7. using namespace std;
  8. const int N = 8005;
  9. int top[N * 8];
  10. bool g[N][N];
  11.  
  12. struct seg
  13. {
  14. int y1, y2, x;
  15. } line[N];
  16.  
  17. bool cmp(seg a, seg b)
  18. {
  19. return a.x < b.x;
  20. }
  21.  
  22. void build()
  23. {
  24. memset(g, 0, sizeof(g));
  25. memset(top, 0, sizeof(top));
  26. }
  27.  
  28. void pushup(int p)
  29. {
  30. top[p] = (top[p << 1] == top[p << 1 | 1]) ? top[p << 1] : 0;
  31. }
  32.  
  33. void pushdown(int p)
  34. {
  35. if(top[p])
  36. {
  37. top[p << 1] = top[p << 1 | 1] = top[p];
  38. top[p] = 0;
  39. }
  40. }
  41.  
  42. void update(int p, int s, int e, int l, int r, int v)
  43. {
  44. if(l <= s && e <= r)
  45. {
  46. top[p] = v;
  47. return;
  48. }
  49. pushdown(p);
  50. if(l <= mid) update(lc, l, r, v);
  51. if(r > mid) update(rc, l, r, v);
  52. pushup(p);
  53. }
  54.  
  55. void query(int p, int s, int e, int l, int r, int x)
  56. {
  57. if(top[p]) //p相应的区间已经仅仅可见一条线段
  58. {
  59. g[top[p]][x] = 1;
  60. return;
  61. }
  62. if(s == e) return;
  63. if(l <= mid) query(lc, l, r, x);
  64. if(r > mid) query(rc, l, r, x);
  65. }
  66.  
  67. int main()
  68. {
  69. int T, n, l, r;
  70. scanf("%d", &T);
  71. while(T--)
  72. {
  73. scanf("%d", &n);
  74. for(int i = 1; i <= n; ++i)
  75. scanf("%d%d%d", &line[i].y1, &line[i].y2, &line[i].x);
  76. sort(line + 1, line + n + 1, cmp);
  77.  
  78. build();
  79. for(int i = 1; i <= n; ++i)
  80. {
  81. //点化为区间会丢失间隔为1的区间 所以要乘以2
  82. l = (line[i].y1) * 2;
  83. r = (line[i].y2) * 2;
  84. query(1, 0, N * 2, l, r, i);
  85. update(1, 0, N * 2, l, r, i);
  86. }
  87.  
  88. int ans = 0;
  89. for(int i = 1; i <= n; ++i)
  90. {
  91. for(int j = i + 1; j <= n; ++j)
  92. {
  93. if(g[i][j])
  94. for(int k = j + 1; k <= n; ++k)
  95. if(g[j][k] && g[i][k]) ++ans;
  96. }
  97. }
  98.  
  99. printf("%d\n", ans);
  100. }
  101. return 0;
  102. }
  103. //Last modified : 2015-07-15 15:33

Horizontally Visible Segments

Description

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

Source

POJ 1436 Horizontally Visible Segments (线段树&#183;区间染色)的更多相关文章

  1. (中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。

    Description There is a number of disjoint vertical line segments in the plane. We say that two segme ...

  2. POJ 1436 Horizontally Visible Segments(线段树)

    POJ 1436 Horizontally Visible Segments 题目链接 线段树处理染色问题,把线段排序.从左往右扫描处理出每一个线段能看到的右边的线段,然后利用bitset维护枚举两个 ...

  3. POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)

    水博客,水一水. Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536K Total Submissions:  ...

  4. POJ 1436 Horizontally Visible Segments

    题意: 有一些平行于y轴的线段 ,两条线段称为互相可见当且仅当存在一条水平线段连接这两条  与其他线段没交点. 最后问有多少组  3条线段,他们两两是可见的. 思路: 线段树,找出两两可见的那些组合, ...

  5. poj 3225 Help with Intervals(线段树,区间更新)

    Help with Intervals Time Limit: 6000MS   Memory Limit: 131072K Total Submissions: 12474   Accepted:  ...

  6. POJ 2750 Potted Flower(线段树的区间合并)

    点我看题目链接 题意 : 很多花盆组成的圆圈,每个花盆都有一个值,给你两个数a,b代表a位置原来的数换成b,然后让你从圈里找出连续的各花盆之和,要求最大的. 思路 :这个题比较那啥,差不多可以用DP的 ...

  7. POJ-2777 Count Color(线段树,区间染色问题)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40510 Accepted: 12215 Descrip ...

  8. [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)

    题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有n段被染色,问染 ...

  9. URAL-1987 Nested Segments 线段树简单区间覆盖

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1987 题意:给定n条线段,每两条线段要么满足没有公共部分,要么包含.给出m个询问,求当前 ...

随机推荐

  1. 调试查看CLR运行代码

    SOS (Son of Strike)调试扩展可以让我们在调试过程中查看CLR运行代码.SOS.dll随.NET一起安装,对于.NET 4.0来说,SOS.dll的所在位置是:C:\Windows\M ...

  2. 测试 Nginx 作为前端下各种模式的性能

    测试环境: 1:Nginx 独立处理静态面页请求 5000,开了60个线程 2:Nginx作为前端请求转给 Weblogic 12c 处理 (Spring 4.0平台下的动态面页效果如图) 3:Ngi ...

  3. 玩转storm

    下载地址 http://storm.apache.org/downloads.html

  4. 字符函数PATINDEX()与STUFF()

    函数 PATINDEX() STUFF() 取数字 英语 汉字 更多:http://msdn.microsoft.com/zh-cn/library/ms188395.aspx PATINDEX() ...

  5. 《Windows核心编程》第七章——进程优先级实验

    其实就是做个实验,看看SetPriorityClass是否真的会生效. 设计思路:主线程一直在进行某种操作(这里是写文件操作),以保证有一定的CPU占用率:生成的子线程来接收你的命令,决定将进程改变为 ...

  6. 《Java核心技术 卷II 高级特性(原书第9版)》

    <Java核心技术 卷II 高级特性(原书第9版)> 基本信息 原书名:Core Java Volume II—Advanced Features(Ninth Edition) 作者: ( ...

  7. 终端控制类getopt isatty select ttyname

    getopt(分析命令行参数) 相关函数 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const argv[ ],con ...

  8. OpenCV学习(22) opencv中使用kmeans算法

    kmeans算法的原理参考:http://www.cnblogs.com/mikewolf2002/p/3368118.html 下面学习一下opencv中kmeans函数的使用.      首先我们 ...

  9. BFS(广搜)DFS(深搜)算法解析

    图是一种灵活的数据结构,一般作为一种模型用来定义对象之间的关系或联系.对象由顶点(V)表示,而对象之间的关系或者关联则通过图的边(E)来表示. 图可以分为有向图和无向图,一般用G=(V,E)来表示图. ...

  10. freetds简介、安装、配置及使用介绍

    什么是FreeTDS 简单的说FreeTDS是一个程序库,可以实现在Linux系统下访问微软的SQL数据库! FreeTDS 是一个开源(如果你喜欢可以称为自由)的程序库,是TDS(表列数据流 )协议 ...