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: ...
随机推荐
- Python网络爬虫与信息提取[request库的应用](单元一)
---恢复内容开始--- 注:学习中国大学mooc 嵩天课程 的学习笔记 request的七个主要方法 request.request() 构造一个请求用以支撑其他基本方法 request.get(u ...
- MySQL忘记root密码重置密码(5.7版本)
网上找了一堆方法都不行,经过折腾一番,发现MySQL不同版本重置密码也存在一定的差异!记录下Mysql5.7.18版本的重置密码方法. 1.找到/etc/mysql/my.cnf 修改此文件添加一行s ...
- JSP-Cookie和Session
1 会话技术简介 1.1 存储客户端状态 1.2 会话技术 2 Cookie技术 2.1 Cookie技术的购物 2.2 服务器端向客户端发Cookie //1.创建cookie对象 Cookie c ...
- POJ2182Lost Cows
Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11946 Accepted: 7690 Descri ...
- http响应头信息
HTTP 响应头信息 HTTP请求头提供了关于请求,响应或者其他的发送实体的信息. 在本章节中我们将具体来介绍HTTP响应头信息. 应答头 说明 Allow 服务器支持哪些请求方法(如GET.POST ...
- Qt: error lnk1158 无法运行rc.exe
解决办法:(依据自己的环境而定) 将C:\Program Files (x86)\Windows Kits\\bin\10.0.15063.0\x64 目录下的rc.exe 和rcdll.dll 复制 ...
- Mac下升级node到最新版本
建议先装n,再用n把node升级到最新稳定版: $ npm install -g n $ n stable
- 分布式--ActiveMQ 消息中间件(一) https://www.jianshu.com/p/8b9bfe865e38
1. ActiveMQ 1). ActiveMQ ActiveMQ是Apache所提供的一个开源的消息系统,完全采用Java来实现,因此,它能很好地支持J2EE提出的JMS(Java Message ...
- 替换文本:将文本文件中的所有src替换为dst
题意: 将文本文件中的所有src替换为dst 方法一:使用String import java.io.File; import java.io.FileNotFoundException; impor ...
- Android基础&进阶
基础总结篇之一:Activity生命周期 基础总结篇之二:Activity的四种launchMode 基础总结篇之三:Activity的task相关 基础总结篇之四:Service完全解析 基础总结篇 ...