POJ 1436 Horizontally Visible Segments 简单hash+区间更新
来源:互联网 发布:合众思壮怎么样知乎 编辑:程序博客网 时间:2024/06/01 08:24
分析:线段树好题,类似poj2528, 题意是求解线段三角形的数目
题目大意:
在一个平面内,有一些竖直的线段,若两条竖直线段之间可以连一条水平线,这条水平线不与其他竖直线段相交,称这两条竖直线段为“相互可见”的。若存在三条竖直线段,两两“相互可见”,则构成“线段三角形”。给出一些竖直的线段,问一共有多少“线段三角形”。
首先处理端点问题:想象一下若同一x位置有两条线段,y坐标为1~2和3~4。其实中间的空当2~3之间是可以引水平线段的,而这里我们都用整数处理,那条水平线段就被忽略了,可能会导致有一些水平相互可见的线段在计算中被忽略了,这里我们扩大y坐标之间的空间,这时我们就可以多出一个整数来便于我们的整数处理,这样就可以简单地处理端点问题,并且它对于所有情况都有很好的效果.自己画个图就明白了。
线段处理:首先对线段以X大小进行排序;再进行询问,询问时是以当前的线段与先前的线段是不是“相互可见”的;再进行更新。
#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 20000 + 10;int color[maxn<<2];int Hash[maxn<<2];vector<int> v[maxn];//存储从左边看每条线段可见的线段idvoid pushdown(int root){ if (color[root] != -1){ color[root<<1] = color[root<<1|1] = color[root]; color[root] = -1; }}void update(int root, int l, int r, int id, int ql, int qr){ if (ql <= l && r <= qr){ color[root] = id; return; } pushdown(root); int m = (l+r) >> 1; if (ql <= m) update(root<<1, l, m, id, ql, qr); if (qr > m) update(root<<1|1, m+1, r, id, ql, qr);}void query(int root, int l, int r, int id, int ql, int qr){ if (color[root] != -1){ if (Hash[color[root]] != id){ // 去重 v[color[root]].push_back(id); // color[root]可见id Hash[color[root]] = id; } return; } if (l == r) return; pushdown(root); int m = (l+r) >> 1; if (ql <= m) query(root<<1, l, m, id, ql, qr); if (qr > m) query(root<<1|1, m+1, r, id, ql, qr);}struct point{ int x, y1, y2;}q[10000];bool cmp(point a, point b){ return a.x < b.x;}int main(){ int T; scanf("%d", &T); while (T--){ memset(color, -1, sizeof(color)); memset(Hash, -1, sizeof(Hash)); int n; scanf("%d", &n); for (int i = 0; i<n; i++) v[i].clear(); for (int i = 0; i<n; i++){ scanf("%d%d%d", &q[i].y1, &q[i].y2, &q[i].x); q[i].y1 = q[i].y1 * 2; q[i].y2 = q[i].y2 * 2; } sort(q, q+n, cmp); for (int i = 0; i<n; i++){ query(1, 1, 17000, i, q[i].y1, q[i].y2); update(1, 1, 17000, i, q[i].y1, q[i].y2); } int res = 0; for (int i = 0; i<n; i++) // 暴力求解 for (int j = 0; j<v[i].size(); j++){ int t = v[i][j]; for (int k = 0; k<v[i].size(); k++) for (int z = 0; z<v[t].size(); z++) if (v[i][k] == v[t][z]) // 暴力寻找第三边 res ++; } printf("%d\n", res); } return 0;}
0 0
- POJ 1436 Horizontally Visible Segments 简单hash+区间更新
- 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-1436Horizontally Visible Segments
- Java版的Shell程序
- Spring启动后获取所有拥有特定注解的Bean
- 并查集详解
- POJ-2513 Colored Sticks
- C - Saruman's Army
- POJ 1436 Horizontally Visible Segments 简单hash+区间更新
- 取模(mod)与取余(rem)的区别
- 深入理解Android之AOP
- Glide 使用心得-看视频收藏的图片
- 每日英语(持续更新
- Java编程思想学习笔记(七) 第8章 多态
- OJ系列之---单词倒排
- eclipse导入已经存在的workspace
- DZNEmptyDataSet UItabelView空数据 背景图的实现