ZOJ :: Problems :: Show Problem

1436 -- Horizontally Visible Segments

  用线段树记录表面能被看见的线段的编号,然后覆盖的时候同时把能看到的线段记录下来。这里要用到拆点,在两个整点之间插入一个点。

  最后O(n^2)统计三角形的个数,因为每条线段可以看见的另外的线段的条数不多,所以可以直接枚举两条边。

代码如下:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <set> using namespace std; const int N = ;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define root 0, 16000, 1
int cov[N << ]; void up(int rt) {
if (~cov[rt << ] && cov[rt << ] == cov[rt << | ]) cov[rt] = cov[rt << ];
else cov[rt] = -;
} void down(int rt) { if (~cov[rt]) cov[rt << ] = cov[rt << | ] = cov[rt];} void build(int L, int R, int l, int r, int rt) {
if (l >= r) {
cov[rt] = L <= l && r <= R ? : ;
return ;
}
int m = l + r >> ;
build(L, R, lson);
build(L, R, rson);
up(rt);
} set<int> edge[N >> ]; void update(int k, int L, int R, int l, int r, int rt) {
if (L <= l && r <= R && ~cov[rt]) {
if (cov[rt]) edge[k].insert(cov[rt]);
cov[rt] = k;
return ;
}
int m = l + r >> ;
down(rt);
if (L <= m) update(k, L, R, lson);
if (m < R) update(k, L, R, rson);
up(rt);
} struct Node {
int l, r, p;
} seg[N >> ]; bool cmp(Node a, Node b) { return a.p < b.p;} int main() {
// freopen("in", "r", stdin);
int T, n;
scanf("%d", &T);
while (T-- && ~scanf("%d", &n)) {
for (int i = ; i <= n; i++) {
scanf("%d%d%d", &seg[i].l, &seg[i].r, &seg[i].p);
seg[i].l <<= , seg[i].r <<= ;
}
sort(seg + , seg + n + , cmp);
edge[].clear();
build(seg[].l, seg[].r, root);
for (int i = ; i <= n; i++) {
edge[i].clear();
update(i, seg[i].l, seg[i].r, root);
}
set<int>::iterator si, sj;
int ans = ;
for (int i = ; i <= n; i++) {
// cout << i << " : ";
// for (si = edge[i].begin(); si != edge[i].end(); si++) cout << *si << ' ';
// cout << endl;
for (si = edge[i].begin(); si != edge[i].end(); si++) {
int t = *si;
for (sj = edge[i].begin(); sj != edge[i].end(); sj++) {
ans += edge[t].find(*sj) != edge[t].end();
}
}
}
printf("%d\n", ans);
}
return ;
}

——written by Lyon

poj 1436 && zoj 1391 Horizontally Visible Segments (Segment Tree)的更多相关文章

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

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

  2. POJ 1436 Horizontally Visible Segments (线段树&#183;区间染色)

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

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

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

  4. 【37%】【poj1436】Horizontally Visible Segments

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5200   Accepted: 1903 Description There ...

  5. POJ 1436 (线段树 区间染色) Horizontally Visible Segments

    这道题做了快两天了.首先就是按照这些竖直线段的横坐标进行从左到右排序. 将线段的端点投影到y轴上,线段树所维护的信息就是y轴区间内被哪条线段所覆盖. 对于一条线段来说,先查询和它能相连的所有线段,并加 ...

  6. POJ 1436 Horizontally Visible Segments

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

  7. 【解题报告】pojP1436 Horizontally Visible Segments

    http://poj.org/problem?id=1436 题目大意:有n条平行于x轴的线段,每条线段有y坐标,如果两条线段有一段x坐标数值相等,且中间没有其它线段阻隔,则称这两条线段"照 ...

  8. poj1436 Horizontally Visible Segments

    这是一个区间更新的题目,先将区间放大两倍,至于为什么要放大可以这样解释,按照从左到右有4个区间,y值是[1,5],[1,2],[3,4],[1,4]如果不放大的话,查询[1,4]区间和前面区间的”可见 ...

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

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

随机推荐

  1. Struts_登录练习(未配置拦截器)

    1.在domain中建个User.java和其配置文件 并在hibernate.cfg.xml中加入配置 2.配置struts文件 3.在jsp文件中修改action地址和name属性,并标注错误信息 ...

  2. 让Drewtech的J2534 ToolBox 软件支持任何J2534的设备

    更改windows注册表中的FunctionLibrary和ConfigApplication,将DLL和exe路径替换原来的,其他不要动. 或者 create second key in regis ...

  3. Linux下安装配置maven

    参考博客: http://www.blogjava.net/caojianhua/archive/2011/04/02/347559.html 注意事项: 1.解压目录 我的maven解压目录为: / ...

  4. C++学习笔记(2)---2.5 C++函数编译原理和成员函数的实现

    转载自:http://c.biancheng.NET/cpp/biancheng/view/2996.html点击打开链接 从上节的例子可以看出,对象的内存模型中只保留了成员变量,除此之外没有任何其他 ...

  5. 在VMware安装Windows server 2003操作系统帮助文档

    在VMware上安装Windows server 2003操作系统,及VMware上三种网络连接模式(以VMware 10为例) 一.在Windows上安装VMware 10虚拟机软件 1.首先在Wi ...

  6. Sum Root to Leaf Numbers深度优先计算路径和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  7. U盘安装win7 raid设置

    需要先安装驱动!  大白菜U盘安装界面有一个选择驱动的选项,选择驱动即可.Intel_ESRT2_Windows_signed_DRV_v16.03.2014.1127

  8. Android LRUCache简介

    LRU Cache数据结构的介绍可以参考前面的http://www.cnblogs.com/XP-Lee/p/3441555.html. 本文以Android LRUCache来做一个简单的介绍.我们 ...

  9. 一个基于Asterisk构建的VOIP应用软件:Elastix介绍

    Elastix 是一种应用软件,它整合了适用于那些基于 Asterisk 的 PBX 的最好工具,并将它们集成为单一的.易用的接口.同时,它增加了自己的工具集,以及允许创建第三方模块来使 Elasti ...

  10. 小爬爬1.requests基础操作

    1.requests安装的问题 (1)如果requests没有安装,我们需要先安装这个模块,在cmd安装不了,我们可以在下面的位置,打开的窗体安装requests模块 pip install requ ...