`
Simone_chou
  • 浏览: 195685 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

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

    博客分类:
  • POJ
 
阅读更多
Horizontally Visible Segments
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3805   Accepted: 1392

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
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3

Sample Output

1

 

      题意:

      给出 T,代表有 T 组样例。每个样例给出一个 n(1 ~ 8000),代表有 n 条垂直的线,后给出这 n 条垂直线的 y1,y2,x (0 ~ 8000)值,寻找任意的三条线,两两之间用横线穿过当且仅当穿过这两条垂直线,寻找有多少个组合满足这样条件的三条线。

 

      思路:

      线段树 + 区间覆盖。不需要离散化,但是存在一个问题就是,有可能两条线用一条水平线穿过只穿过了两条线的端点,而线段树维护子节点是维护一个线段元的覆盖情况的,所以要对 y 坐标方法处理,即 X 2。这样的话就可以转化成线段元来处理了。每添加一条线前先查询该区间能到达的线分别是哪些,再插入进去,用 Map 标记每条线的到达情况,后暴力求解 Map [ i ] [ j ] == 1 且 Map [ i ] [ k ] 且 Map [ j ] [ k ] 的一共有多少即可。记得建树和查询的时候也要对树 X 2 来建和查询,不够细心 WA 了几次。数据比较小,也不需要离散化。

 

       AC: 

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 8005;

typedef struct {
        int x, y1, y2;
} node;

node line[MAX];
bool Map[MAX][MAX];
int cover[MAX * 5];
int n;

bool cmp (node a, node b) {
        return a.x < b.x;
}


void push_down (int node) {
        if (cover[node]) {
                cover[node << 1] = cover[node];
                cover[node << 1 | 1] = cover[node];
                cover[node] = 0;
        }
}

void build (int node, int l, int r) {
        if (r == l) {
                cover[node] = 0;
        } else {
                int mid = (r + l) >> 1;
                build(node << 1, l, mid);
                build(node << 1 | 1, mid + 1, r);
                cover[node] = 0;
        }
}

void updata (int node, int l, int r, int cl, int cr, int c) {
        if (cl > r || cr < l) return;
        if (cl <= l && cr >= r) {
                cover[node] = c;
                return;
        }
        if (r == l) return;

        push_down(node);
        int mid = (r + l) >> 1;
        if (cl <= mid) updata(node << 1, l, mid, cl, cr, c);
        if (cr >= mid) updata(node << 1 | 1, mid + 1, r, cl, cr, c);
}

void query (int node, int l, int r, int ql, int qr, int q) {
        if (ql > r || qr < l) return;
        if (ql <= l && qr >= r) {
                if (cover[node])  {
                        Map[q][ cover[node] ] = 1;
                        return;
                }
        }
        if (r == l) return;

        push_down(node);
        int mid = (r + l) >> 1;
        if (ql <= mid) query(node << 1, l, mid, ql, qr, q);
        if (qr >= mid) query(node << 1 | 1, mid + 1, r, ql, qr, q);
}

void solve () {

        int sum = 0;
        for (int i = n; i >= 3; --i) {
                for (int j = i - 1; j >= 2; --j) {
                        if (Map[i][j]) {
                                for (int k = j - 1; k >= 1; --k) {
                                        if (Map[i][k] && Map[j][k]) ++sum;
                                }
                        }
                }
        }

        printf("%d\n", sum);
}

int main() {
        int t;
        scanf("%d", &t);

        while (t--) {

                scanf("%d", &n);

                memset(Map, 0, sizeof(Map));
                build(1, 0, MAX * 2);

                for (int i = 1; i <= n; ++i) {
                        scanf("%d%d%d", &line[i].y1, &line[i].y2, &line[i].x);
                }

                sort(line + 1, line + 1 + n, cmp);

                for (int i = 1; i <= n; ++i) {
                        query(1, 0, MAX * 2, line[i].y1 * 2, line[i].y2 * 2, i);
                        updata(1, 0, MAX * 2, line[i].y1 * 2, line[i].y2 * 2, i);
                }

                solve();
        }

        return 0;
}

 

分享到:
评论

相关推荐

    藏经阁-Horizontally Scalable Relation.pdf

    藏经阁-Horizontally Scalable Relation

    藏经阁-Horizontally Scalable Relational Databases with Spark.pdf

    " Horizontally Scalable Relational Databases with Spark" 在这个文件中,我们可以提炼出以下几个关键知识点: 一、Citus 介绍 Citus 是一个开源的关系数据库管理系统,支持水平扩展、多租户和实时分析。它可以...

    2,878 horizontally transferred genes in rice genome revealed by BLAST homology and phylogenetic tree

    水稻基因组中通过BLAST同源搜索和系统发育树预测的2878条水平转移基因,郭兴益,王煜,水平基因转移(HGT)现象在许多原核生物中均有报导,对于其适应新的生境有重要意义。有关原核生物DNA转移到高等真核生物核基因...

    dolly.js:简单的jQuery小部件,可将类似电子表格的单元格克隆功能添加到HTML表中

    Dolly.js-轻松克隆表 Dolly.js是一个简单而... console.log(this, "has been cloned " + ui.cloneX + " cells horizontally and " + ui.cloneY + " vertically."); } }); 您可以在examples目录中找到更详细的examples

    Enhanced optical and thermal performance of white light-emitting diodes with horizontally layered quantum dots phosphor nanocomposites

    In this work, we propose a novel packaging scheme with horizontally layered QDs-phosphor nanocomposites to obtain an enhanced optical and thermal performance for WLEDs. Three different WLEDs, ...

    idea 快捷键

    - **Split Editor Horizontally (Ctrl+] )**:水平分割编辑器。 - **Split Editor Vertically (Ctrl+[ )**:垂直分割编辑器。 - **Close Other Tabs (Ctrl+Shift+F4)**:关闭其他标签页。 - **Show Intentions (Alt+...

    UnityShader使用Shader将图片进行水平/竖直镜像翻转

    描述:使用Shader将图片进行水平/竖直镜像翻转 资源类型:unitypackage,导入即可使用 ...It is possible to autonomously mirror and flip graphics horizontally or vertically during runtime, as well as simultane

    最新Ehlib 5.0.13(含完整Delphi、C++builder源代码,完全支持delphi 2010正式版)

    Grid can scroll data smoothly vertically and horizontally. + Hot track. Grid can highlight a cell or a row under mouse cursor. + Grid can show vertical line in gradient mode between data rows and...

    网页前端canvas简单处理图片的白点(白边),旋转和反转图片

    - flip the image horizontally or vertically - rotate the image +/- 90d ## Guide - Please run `npm install` in the folder to install npm modules. - Visit the static ...

    horizontally-scaling-socket.io:socket.io的MVP与多服务器设置一起使用

    如何使用 // git clone npm i 启动Redis服务器使用您的Redis服务器的信息填写nodemon.json中缺少的环境变量 npm start 大量的console.logs ......... 此仓库模拟2个Web客户端和2个节点服务器 客户端1连接到服务器1...

    poi最新版jar包

    - HSSF(Horizontally Stored Format):支持旧版的BIFF格式,用于读写.xls文件。 - XSSF(XML Spreadsheet Format):支持.xlsx文件,基于OOXML标准。 -SXSSF(Streaming Usermodel API):内存效率更高,适用于...

    poi3.11-jar包

    1. **文件格式支持**:Apache POI主要处理的是Microsoft Office使用的二进制文件格式,如HSSF(Horizontally Stored Formatted Sheets)用于Excel,HWPF(Horizontally Stored Word Processor Files)用于Word,以及...

    poi-bin-3.15-beta1-20160409.zip

    - **自动化报告**:通过编程方式创建和更新Word或Excel报告,尤其在需要定期自动生成报告的场景下。 - **服务器端处理Office文档**:在服务器端,Apache POI可以处理上传的Office文档,例如生成报表或进行数据导入...

    POI 相关jar包.rar

    10. **版本更新**: 版本4.1.2的更新可能包含了一些性能提升、错误修复和新功能,比如对OOXML格式更全面的支持或对旧格式的兼容性改进。 总的来说,"POI 相关jar包.rar"提供的是一套完整的Apache POI库,可以帮助...

    JAVA操作Excel文件+核心代码.docx

    核心代码中使用了Apache POI库的HSSF(Horizontally Segmented Storage Format)来操作Excel文件。HSSF是Apache POI库中的一个模块,用于读取和写入Excel文件。核心代码中,首先创建了一个Excel工作簿,然后创建了一...

    Apache POI 3.16 JAR 包

    此外,POI还支持HWPF(Horizontally Stored Word Format)用于Word文档,以及HSLF(Horizontally Stored PowerPoint Format)用于PowerPoint。 **Apache POI 3.16 版本** Apache POI 3.16是该项目的一个稳定版本,...

    IDEA新手开发使用教程

    通过`Window`菜单中的`Activate Editor Splitter`或`Split Vertically`和`Split Horizontally`选项,可以调整编辑器的布局模式。 ### 2. IDEA常用快捷键 - `Ctrl+Shift+V`:粘贴板历史记录,选择性粘贴。 - `Ctrl+...

    Apche POI JAR包

    同时,它持续更新以保持对新版本Office格式的支持。 总的来说,Apache POI是一个功能强大的Java库,为开发者提供了一整套处理Microsoft Office文档的工具,使得在Java环境中与Office文件的交互变得简单而高效。无论...

    MyBase Desktop 6.0.2 破解正式版(官方更新至10/10/2011)

    11.Added: the 'Arrange child windows on running queries' option: Tile vertically/horizontally and Cascade 12.Added: an option to scan titles only within the 'Search by RegExp' plugin. 13.Improved: ...

Global site tag (gtag.js) - Google Analytics