水博客,水一水。

Horizontally Visible Segments
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6290   Accepted: 2287

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

题意:输入n表示有n条线段,n行每行输入y1,y2,x表示线段的下端点,上端点以及线段的位置(横坐标位置),对于第i条线段和第k条线段如果用一条平行线能够经过它们并且在它们之间不经过其他线段,则称这两条线段互为可见,求有多少3条线段两两可见(懒得写题意,直接贴的别人的)

代码:

  1. //线段树区间更新,端点放大2倍
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<cstdlib>
  8. using namespace std;
  9. typedef long long ll;
  10. const int maxn=8e3+;
  11.  
  12. #define lson l,m,rt<<1
  13. #define rson m+1,r,rt<<1|1
  14.  
  15. int col[maxn<<];
  16. bool mp[maxn][maxn];
  17.  
  18. struct node{
  19. int y1,y2,x,id;
  20. }line[maxn];
  21.  
  22. bool cmp(node a,node b)
  23. {
  24. return a.x<b.x;
  25. }
  26.  
  27. void init()
  28. {
  29. memset(mp,,sizeof mp);
  30. memset(line,,sizeof line);
  31. memset(col,,sizeof col);
  32. }
  33.  
  34. void pushdown(int rt)
  35. {
  36. if(col[rt]){
  37. col[rt<<]=col[rt<<|]=col[rt];
  38. col[rt]=;
  39. }
  40. }
  41.  
  42. void update(int L,int R,int c,int l,int r,int rt)
  43. {
  44. if(L<=l&&r<=R){
  45. col[rt]=c;
  46. return ;
  47. }
  48.  
  49. pushdown(rt);
  50. int m=(l+r)>>;
  51. if(L<=m) update(L,R,c,lson);
  52. if(R> m) update(L,R,c,rson);
  53. }
  54.  
  55. void query(int L,int R,int c,int l,int r,int rt)
  56. {
  57. if(col[rt]){
  58. mp[c][col[rt]]=;
  59. return ;
  60. }
  61.  
  62. if(l==r){
  63. return ;
  64. }
  65.  
  66. int m=(l+r)>>;
  67. if(L<=m) query(L,R,c,lson);
  68. if(R> m) query(L,R,c,rson);
  69. }
  70.  
  71. int main()
  72. {
  73. int t;
  74. scanf("%d",&t);
  75. while(t--){
  76. init();
  77. int n;
  78. scanf("%d",&n);
  79. int N=-;
  80. for(int i=;i<=n;i++){
  81. scanf("%d%d%d",&line[i].y1,&line[i].y2,&line[i].x);
  82. line[i].id=i;line[i].y1*=;line[i].y2*=;N=max(N,max(line[i].y1,line[i].y2));
  83. }
  84. sort(line+,line++n,cmp);//按x轴从小到大排序,降维,只对y轴进行建树
  85. for(int i=;i<=n;i++){
  86. query(line[i].y1,line[i].y2,line[i].id,,N,);
  87. update(line[i].y1,line[i].y2,line[i].id,,N,);
  88. }
  89. int ans=;
  90. for(int i=;i<=n;i++){
  91. for(int j=;j<=n;j++){
  92. if(mp[i][j]){
  93. for(int k=;k<=n;k++){
  94. if(mp[i][k]&&mp[k][j]) ans++;
  95. }
  96. }
  97. }
  98. }
  99. printf("%d\n",ans);
  100. }
  101. }

POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)的更多相关文章

  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 (线段树&#183;区间染色)

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

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

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

  4. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  5. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  6. POJ 1436 Horizontally Visible Segments

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

  7. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  8. poj 2777 Count Color(线段树 区间更新)

    题目:http://poj.org/problem?id=2777 区间更新,比点更新多一点内容, 详见注释,  参考了一下别人的博客.... 参考博客:http://www.2cto.com/kf/ ...

  9. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. 2019 UCloudjava面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.UCloud等公司offer,岗位是Java后端开发,因为发展原因最终选择去了UCloud,入职一年时间了,也 ...

  2. python安装和pycharm安装与笔记

    目录 计算机的基础知识 python安装和使用 pycharm安装和使用 [TOC] 计算机的基础知识 计算机是由什么组成的 cpu-----大脑 主板----身体 电源----心脏 内存----临时 ...

  3. Flutter — IDE Shortcuts for Faster Development

    https://medium.com/flutter-community/flutter-ide-shortcuts-for-faster-development-2ef45c51085b If yo ...

  4. 【转载】 C#中使用float.Parse方法将字符串转换为Float类型

    在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为单精度Float类型就是一个常见的类型转换操作,float.Parse方法是C#中专门用来将字符串转换为float类型的,f ...

  5. 虚拟Dom详解 - (一)

    随着Vue和React的风声水起,伴随着诸多框架的成长,虚拟DOM渐渐成了我们经常议论和讨论的话题.什么是虚拟DOM,虚拟DOM是如何渲染的,那么Vue的虚拟Dom和React的虚拟DOM到底有什么区 ...

  6. v8--sort 方法 源码 (1) 插入排序法

    v8--sort方法源码中对于长度较短的数组使用的是插入排序法. 部分源码: function InsertionSort(a, from, to) { for (var i = from + 1; ...

  7. Linux对变量的截取替换

    //对变量截取 ${variable} 对变量值的修正(增删数据),仅仅修改输出后的结果对源值,并不做修改 #变数后对接字符 # ## //截取从前面开始 % %% //截取从后面开始 / // // ...

  8. Linux命令——parted

    参考:8 Linux ‘Parted’ Commands to Create, Resize and Rescue Disk Partitions 简介 parted是磁盘分区操作工具,支持多种磁盘分 ...

  9. HTML常用全部代码--第一部分--HTML/CSS( 小伙伴要牢记😁😁😁😁 )

    <一>html代码大全:结构性定义 (1) 文件类型<HTML></HTML> (放在档案的开头与结尾) (2) 文件主题<TITLE></TIT ...

  10. HikariCP 个人实例

    pom依赖 <!--HikariCP数据库连接池--> <dependency> <groupId>com.zaxxer</groupId> <a ...