POJ 1436 Horizontally Visible Segments(线段树区间染色问题)
来源:互联网 发布:上海兄弟连java学费 编辑:程序博客网 时间:2024/06/08 13:52
题意:
在一个平面内,有一些竖直的线段,若两条竖直线段之间可以连一条水平线,这条水平线不与其他竖直线段相交,称这两条竖直线段为“相互可见”的。若存在三条竖直线段,两两“相互可见”,则构成“线段三角形”。给出一些竖直的线段,问一共有多少“线段三角形”。
解析:
可以将x-y坐标系,逆时针旋转90°来看,那么这题就可以转化成线段树的区间覆盖问题。
这题要解决两个问题:
1. 端点处理问题:若同一x位置有两条线段,y坐标为1~2和3~4。其实中间的空当2~3之间是可以引水平线段的,而这里我们都用整数处理,那条水平线段就被忽略了,可能会导致有一些水平相互可见的线段在计算中被忽略了,这里我们扩大y坐标之间的空间,这时我们就可以多出一个整数来便于我们的整数处理,这样就可以简单地处理端点问题,并且它对于所有情况都有很好的效果。
2. 线段处理问题:首先对线段以X大小进行排序;再进行询问,询问时是以当前的线段与先前的线段是不是“相互可见”的;再进行更新。
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#define pb push_back#define ls (o<<1)#define rs (o<<1|1)#define lson ls, L, M#define rson rs, M+1, Rusing namespace std;const int N = 8005*2;struct Interval { int y1, y2, x;} inter[N];vector<int> edge[N];int setv[N<<2];bool vis[N];int n;bool cmp(Interval a, Interval b) { return a.x < b.x;}void init(int n) { memset(setv, 0, sizeof(setv)); for(int i = 0; i <= n; i++) edge[i].clear();}void pushDown(int o) { if(setv[o]) { setv[ls] = setv[rs] = setv[o]; setv[o] = 0; }}void pushUp(int o) { if(setv[ls] == setv[rs]) setv[o] = setv[ls];}void query(int o, int L, int R, int ql, int qr, int val) { if(setv[o]) { if(!vis[setv[o]]) { edge[val].pb(setv[o]); vis[setv[o]] = true; }else return ; } if(L == R) return ; pushDown(o); int M = (L + R)/2; if(ql <= M) query(lson, ql, qr, val); if(qr > M) query(rson, ql, qr, val);}void modify(int o, int L, int R, int ql, int qr, int val) { if(ql <= L && R <= qr) { setv[o] = val; return ; } pushDown(o); int M = (L + R)/2; if(ql <= M) modify(lson, ql, qr, val); if(qr > M) modify(rson, ql, qr, val); pushUp(o);}int cal() { int ans = 0; int a, b, c; for(a = 1; a <= n; a++) { sort(edge[a].begin(), edge[a].end()); for(int i = 0; i < edge[a].size(); i++) { b = edge[a][i]; for(int j = 0; j < edge[b].size(); j++) { c = edge[b][j]; if(binary_search(edge[a].begin(), edge[a].end(), c)) ans++; } } } return ans;}int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); int x, y1, y2; for(int i = 1; i <= n; i++) { scanf("%d%d%d", &y1, &y2, &x); y1 *= 2, y2 *= 2; if(y1 > y2) swap(y1, y2); inter[i] = (Interval){y1, y2, x}; } sort(inter+1, inter+1+n, cmp); init(n); for(int i = 1; i <= n; i++) { memset(vis, false, sizeof(vis)); query(1, 0, N, inter[i].y1, inter[i].y2, i); modify(1, 0, N, inter[i].y1, inter[i].y2, i); } printf("%d\n", cal()); } return 0;}
0 0
- POJ 1436 Horizontally Visible Segments(线段树区间染色问题)
- POJ 1436 Horizontally Visible Segments (线段树·区间染色)
- POJ 1436——Horizontally Visible Segments(线段树,区间染色+暴力+简单hash)
- poj 1436 Horizontally Visible Segments(线段树基础,区间染色,拆点)
- poj 1436 Horizontally Visible Segments(线段树、区间覆盖)
- POJ 1436 Horizontally Visible Segments(线段树区间修改)
- POJ 1436 Horizontally Visible Segments(线段树区间更新)
- POJ 1436 Horizontally Visible Segments 线段树 染色
- poj 1436 线段树区间操作 Horizontally Visible Segments
- POJ 1436 Horizontally Visible Segments [线段树-区间更新]【数据结构】
- POJ 题目1436 Horizontally Visible Segments(线段树染色覆盖求相互是否可见)
- POJ 1436 Horizontally Visible Segments 线段树
- POJ 1436 Horizontally Visible Segments 线段树
- poj 1436 Horizontally Visible Segments[线段树]
- poj 1436 线段树 Horizontally Visible Segments
- POJ 1436 Horizontally Visible Segments (线段树)
- [poj 1436]Horizontally Visible Segments[线段树]
- poj 1436 Horizontally Visible Segments(线段树)
- 打鱼还是晒网
- 通过函数回调排序结构体数组
- LeetCode---(197)Largest Number
- 3.4Shadowpage页表虚拟化
- J2ee入门之开发环境搭建
- POJ 1436 Horizontally Visible Segments(线段树区间染色问题)
- 代码评审这点事,元芳你怎么看
- c++学习日记 1
- Being a Hero (hdu 3251 最小割 好题)
- scikit-learn:4.7. Pairwise metrics, Affinities and Kernels
- IO流(复制文本文件的5种方式案例(用字符流)
- 理解inode
- MyBatis 的 resultMap
- 项目、习惯以及Todolist的区别