poj 1436 && zoj 1391 Horizontally Visible Segments (Segment Tree)
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)的更多相关文章
- POJ 1436 Horizontally Visible Segments(线段树)
POJ 1436 Horizontally Visible Segments 题目链接 线段树处理染色问题,把线段排序.从左往右扫描处理出每一个线段能看到的右边的线段,然后利用bitset维护枚举两个 ...
- POJ 1436 Horizontally Visible Segments (线段树·区间染色)
题意 在坐标系中有n条平行于y轴的线段 当一条线段与还有一条线段之间能够连一条平行与x轴的线不与其他线段相交 就视为它们是可见的 问有多少组三条线段两两相互可见 先把全部线段存下来 并按x ...
- (中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。
Description There is a number of disjoint vertical line segments in the plane. We say that two segme ...
- 【37%】【poj1436】Horizontally Visible Segments
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5200 Accepted: 1903 Description There ...
- POJ 1436 (线段树 区间染色) Horizontally Visible Segments
这道题做了快两天了.首先就是按照这些竖直线段的横坐标进行从左到右排序. 将线段的端点投影到y轴上,线段树所维护的信息就是y轴区间内被哪条线段所覆盖. 对于一条线段来说,先查询和它能相连的所有线段,并加 ...
- POJ 1436 Horizontally Visible Segments
题意: 有一些平行于y轴的线段 ,两条线段称为互相可见当且仅当存在一条水平线段连接这两条 与其他线段没交点. 最后问有多少组 3条线段,他们两两是可见的. 思路: 线段树,找出两两可见的那些组合, ...
- 【解题报告】pojP1436 Horizontally Visible Segments
http://poj.org/problem?id=1436 题目大意:有n条平行于x轴的线段,每条线段有y坐标,如果两条线段有一段x坐标数值相等,且中间没有其它线段阻隔,则称这两条线段"照 ...
- poj1436 Horizontally Visible Segments
这是一个区间更新的题目,先将区间放大两倍,至于为什么要放大可以这样解释,按照从左到右有4个区间,y值是[1,5],[1,2],[3,4],[1,4]如果不放大的话,查询[1,4]区间和前面区间的”可见 ...
- POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)
水博客,水一水. Horizontally Visible Segments Time Limit: 5000MS Memory Limit: 65536K Total Submissions: ...
随机推荐
- plsql中学习job
--1.plsql中学习job --学习job --建表 create table test_job(para_date date); commit; insert into test_job val ...
- Thinkphp js、css压缩类minify
说明:Minify 是用PHP5开发的应用,通过遵循一些Yahoo的优化规则来提高网站的性能.它会合并多个CSS或者JavaScript文件,移除一些不必要的空格和注释,进行gzip压缩,并且会设置浏 ...
- Lavavel 程序报错 MassAssignmentException in Model.php line 452: _token
Lavarel 用类似于下面命令插入数据时候出错 Comment::create($request->all()) 错误页面截图如下: 错误原因:下面这行代码应该写到对应的Model里面,而不是 ...
- JavaScript--location.href的跳转
页面重载 true 强制从服务器加载 false 优先从缓存加载 window.location.reload(true); window.location.href.self.location. ...
- LintCode_46 主元素
题目 给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一. 样例 给出数组[1,1,1,1,2,2,2],返回 1 思路 首先 发现所给的数组是顺序排列好的. 用动态规划 ...
- Linux下下载安装jdk1.7和IDEA
一.安装JDK1.7 准备: 到Oracle官网下载下载jdk1.7,参考博客 https://blog.csdn.net/H12KJGJ/article/details/79240984 官网地址: ...
- vue 微信内H5调起支付
在微信内H5调起微信支付,主要依赖于一个微信的内置对象WeixinJSBridge,这个对象在其他浏览器中无效. 主要代码: import axios from 'axios'; export def ...
- tyvjP1288 飘飘乎居士取能量块
P1288 飘飘乎居士取能量块 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 9月21日,pink生日:9月22日,lina生日:9月23日,轮到到飘飘乎居 ...
- es6 babel转码器安装配置及常见命令
示例:在d盘,新建文件夹es6,文件夹里新建一个文件es6.js. (1)先全局安装babel-cli,输入命令npm install babel-cli -g (2)输入d:( 进入d盘),再输入c ...
- Liferay 7 OSGi第三方jar包依赖问题
发现遇到这个问题的人挺多的,现在跟新一下好好写. 在开发Liferay的时候,我们常常会遇到Unresolved requirement: Import-Package: {package name} ...