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 ...
随机推荐
- aapt环境变量配置
D:\android-sdk_r24.4.1-windows\android-sdk-windows\build-tools\28.0.2 将aapt路径添加到path中, 打开cmd 输入aapt
- 剑指offer8 旋转数组的最小数字
一种错误写法: class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { int len ...
- 路由器wan口ip地址显示0.0.0.0怎么办
http://m.xuexila.com/luyouqi/671049.html 这个网络时代里面我们最常用来连接网络的设备就是路由器了,现在的社会不管是工作还是生活几乎都离不开网络了,同时我们也要学 ...
- AspNetCore容器化(Docker)部署(四) —— Jenkins自动化部署
一.前言 (Jenkins.Docker.Git/Svn组建一套简单的自动化发布流程) 文章中用到的相关服务器角色 角色 环境 功能 开发机 Win10.Docker(Linux OS) 编码.调试 ...
- MySQL-03 SQL语句设计
学习要点 SQL语句分类 DML语句 DML 查询语句 SQL语句分类 数据操纵语言(DML):用来操纵数据库中数据的命令.包括:SELECT.INSERT.UPDATE.DELETE. 数据定义语言 ...
- OpenCV2:总结篇 工具方法函数
一.简介 OpenCV提供了一些工具方法函数来辅助完成图像运算 二.时间相关 1.getTickCount()和getTickFrequency() double tTime; tTime = (do ...
- 多线程test
import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import ...
- es6(三set和map数据结构)
es6中提供了一个新的数据结构Set,他有点类似数组,但和数组不同的是,在里面你如果写入重复的值的话,他不会显示重复值. const s =new Set(); [2,3,4,5,6,6,6,7,8, ...
- node.js从入门到放弃(二)
上章讲了学习node,应该去学习什么,对这些框架去进行学习现在咋们聊聊如何用原生来进行操作 主要来讲一下events-事件触发器 events是什么东西呢?他的英文翻译,活动,事件的意思,在计算机语言 ...
- N皇后递归
问题: n皇后问题:输入整数n, 要求n个国际象棋的皇后,摆在 n*n的棋盘上,互相不能攻击,输出全部方案. #include <iostream> using namespace std ...