POJ 1436 Horizontally Visible Segments(线段树)
POJ 1436 Horizontally Visible Segments
线段树处理染色问题,把线段排序。从左往右扫描处理出每一个线段能看到的右边的线段,然后利用bitset维护枚举两个线段。找出还有一个两个都有的线段
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <vector>
using namespace std; const int N = 8005;
bitset<N> g[N];
int t, n; struct Seg {
int x, y1, y2;
void read() {
scanf("%d%d%d", &y1, &y2, &x);
}
} s[N]; bool cmp(Seg a, Seg b) {
return a.x < b.x;
} #define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2) struct Node {
int l, r, val, setv;
} node[N * 4 * 2]; void pushup(int x) {
if (node[lson(x)].val == node[rson(x)].val) node[x].val = node[lson(x)].val;
else node[x].val = -1;
} void pushdown(int x) {
if (node[x].setv != -1) {
node[lson(x)].val = node[rson(x)].val = node[x].setv;
node[lson(x)].setv = node[rson(x)].setv = node[x].setv;
node[x].setv = -1;
}
} void build(int l, int r, int x = 0) {
node[x].l = l; node[x].r = r; node[x].val = -1; node[x].setv = -1;
if (l == r)
return;
int mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
} vector<int> g2[N]; void add(int l, int r, int v, int x = 0) {
if (node[x].val != -1 && node[x].l >= l && node[x].r <= r) {
if (g[node[x].val][v] == false)
g2[node[x].val].push_back(v);
g[node[x].val][v] = true;
node[x].setv = v;
node[x].val = v;
return;
}
if (node[x].l == node[x].r) {
node[x].val = v;
return;
}
pushdown(x);
int mid = (node[x].l + node[x].r) / 2;
if (l <= mid) add(l, r, v, lson(x));
if (r > mid) add(l, r, v, rson(x));
pushup(x);
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
g[i].reset();
g2[i].clear();
s[i].read();
}
sort(s, s + n, cmp);
build(0, N * 2 - 1);
for (int i = 0; i < n; i++)
add(s[i].y1 * 2, s[i].y2 * 2, i);
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < g2[i].size(); j++) {
if (g[i][g2[i][j]])
ans += (g[i]&g[g2[i][j]]).count();
}
}
printf("%d\n", ans);
}
return 0;
}
POJ 1436 Horizontally Visible Segments(线段树)的更多相关文章
- 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 ...
- POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)
水博客,水一水. Horizontally Visible Segments Time Limit: 5000MS Memory Limit: 65536K Total Submissions: ...
- POJ 1436 Horizontally Visible Segments
题意: 有一些平行于y轴的线段 ,两条线段称为互相可见当且仅当存在一条水平线段连接这两条 与其他线段没交点. 最后问有多少组 3条线段,他们两两是可见的. 思路: 线段树,找出两两可见的那些组合, ...
- poj 1436 && zoj 1391 Horizontally Visible Segments (Segment Tree)
ZOJ :: Problems :: Show Problem 1436 -- Horizontally Visible Segments 用线段树记录表面能被看见的线段的编号,然后覆盖的时候同时把能 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 1436 (线段树 区间染色) Horizontally Visible Segments
这道题做了快两天了.首先就是按照这些竖直线段的横坐标进行从左到右排序. 将线段的端点投影到y轴上,线段树所维护的信息就是y轴区间内被哪条线段所覆盖. 对于一条线段来说,先查询和它能相连的所有线段,并加 ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
随机推荐
- magic_quotes_runtime 和 magic_quotes_sybase 的作用
如果启用了 magic_quotes_runtime,大多数返回任何形式外部数据的函数,包括数据库和文本段将会用反斜线转义引号. 如果启用了magic_quotes_sybase,单引号会被单引号转义 ...
- js 数组元素排序?
Part.1 sort 方法 js 有自带排序方法 sort(), 默认 升序 排列 如: data() { return { arr: [1,3,2,5,6,8,7,4,9] } }, 控制台如 ...
- PHP14 动态图像处理
学习要点 如何使用PHP中的GD库 设计验证码类 PHP图片处理 设计图像图处理类 如何使用PHP中的GD库 在网站上GD库通常用来生成缩略图,或者用来对图片加水印,或者用来生成汉字验证码,或者对网站 ...
- 502 bad gateway nginx
此方法可能仅对于我的问题有效 我在VMware虚拟机启动docker container nginx的,一开始启动nginx的contatiner,在浏览器是可以正常访问的,但次日重新访问时就报502 ...
- JAVA用freemarker生成复杂Excel。(freemarker)
在生成Excel的时候,大多时候都是使用poi,jxl等进行的,但是对于复杂的Excel来说,这个工作量是非常的大的,而且,对于我这么懒的人来说,这是相当痛苦的一件事情,所以,我不得不找找有没有简单一 ...
- springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...
- 南邮CTF--md5_碰撞
南邮CTF--难题笔记 题目:md5 collision (md5 碰撞) 解析: 经过阅读源码后,发现其代码是要求 a !=b 且 md5(a) == md5(b) 才会显示flag,利用PHP语言 ...
- 学习PyQuery库
学习PyQuery库 好了,又是学习的时光啦,今天学习pyquery 来进行网页解析 常规导入模块(PyQuery库中的pyquery类) from pyquery import PyQuery as ...
- while循环处理列表和字典
一.在列表之间移动元素 假设有一个列表,里面存放的是网站新注册但没有验证的用户,验证这些用户后,如何将它们移动到另一个已验证用户列表中呢? 其中一种方法是使用while循环,在验证用户的同时,将其从未 ...
- jmeter 断言-各种分类讲解
jmeter中有个元件叫做断言(Assertion),它的作用和loadrunner中的检查点类似: 用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中的数据交互与预期一致. 使用断言 ...