[ZOJ1391][POJ1436] Horizontally Visible Segments
来源:互联网 发布:超人钢铁之躯 知乎 编辑:程序博客网 时间:2024/05/18 00:26
题目大意:
若两条竖直线段之间可以连一条水平线,这条水平线不与其他竖直线段相交,称这两条竖直线段为“相互可见”的。若存在三条竖直线段,两两”相互可见“,则构成”线段三角形“。给出一些竖直的线段,问一共有多少”线段三角形“
解题思路:
这道题在刘汝佳的黑书里有解题思路的指导,在图论部分出现(利用线段树之利用了一个欧拉定理的推论来求三角形个数,不过这里没有用那种方法)
预处理:
1、竖直坐标应该乘以二,想象一下若同一x位置有两条线段,y坐标为1~2和3~4。其实中间的空当2~3之间是可以引水平线段的,而这里我们都用整数处理,那条水平线段就被忽略了,可能会导致有一些水平相互可见的线段在计算中被忽略了。
2、所有线段按x排序
线段树处理:
按x从小到大插入每一条边seg[i]
1、quey用于在插入边之前统计 l~r这一段上已经染有的颜色(其实是最后插入的边的id),这些颜色的边可以看到这一步准备插入的边seg[i]。这里用一个数组v来标记每一种颜色是否已经和seg[i]相互可视,来避免颜色的重复统计。
2、update用于插入边seg[i]
”线段三角形"个数统计:
在线段树处理中的query部分统计得每一条边seg[i]可以看到的id比自己大的边,放在vector see[i]里。接下来暴力枚举答案即可。
枚举i可以看到的边j和k,再枚举判断j是否可以看到k。(注意query中得到的可视结果都是由较小的边看到较大的边)
源代码:
#include <cstdio>#include <cstdlib>#include <cstring>#include <vector>#include <algorithm>using namespace std;#define MIX -1#define maxn 10000struct tnode{ int l, r, c;}tree[maxn*4];struct segment{ int x,y1,y2;}seg[maxn];bool cmp(segment a, segment b){ return a.x<b.x;}int v[maxn];vector<int> see[maxn];void buildtree(int k, int l, int r){ tree[k].l=l; tree[k].r=r; tree[k].c=MIX; if (l==r) return; int mid=(l+r)>>1; buildtree(k<<1, l, mid); buildtree((k<<1)+1, mid+1, r);}void update(int k, int l, int r, int id){ if (l<=tree[k].l && r>=tree[k].r) { tree[k].c=id; return; } int lson=k<<1, rson=lson+1, mid=(tree[k].l+tree[k].r)>>1; if (tree[k].c!=MIX) { tree[lson].c = tree[rson].c = tree[k].c; tree[k].c=MIX; } if (l<=mid) update(lson, l ,r, id); if (mid<r) update(rson, l, r, id); if (tree[lson].c==tree[rson].c) tree[k].c = tree[lson].c;}void query(int k, int l, int r, int id){ if (tree[k].c!=MIX) { if (v[tree[k].c]!=id) { see[tree[k].c].push_back(id); v[tree[k].c]=id; } return; } if (tree[k].l==tree[k].r) return; int lson=k<<1, rson=lson+1, mid=(tree[k].l+tree[k].r)>>1; if (tree[k].c!=MIX) { tree[lson].c = tree[rson].c = tree[k].c; tree[k].c=MIX; } if (r<=mid) query(lson, l, r, id); else if (mid<l) query(rson, l, r, id); else { query(lson, l, mid, id); query(rson, mid+1, r, id); } if (tree[lson].c==tree[rson].c) tree[k].c=tree[lson].c;}int main(){ int cs, n, ans; scanf("%d", &cs); while (cs--) { buildtree(1, 0, 16000); scanf("%d", &n); for (int i=0; i<n; i++) { scanf("%d%d%d", &seg[i].y1, &seg[i].y2, &seg[i].x); seg[i].y1*=2; seg[i].y2*=2; see[i].clear(); } sort(seg, seg+n, cmp); memset(v, 255, sizeof(v)); for (int i=0; i<n; i++) { query(1, seg[i].y1, seg[i].y2, i); update(1, seg[i].y1, seg[i].y2, i); } ans=0; for (int i=0; i<n; i++) { int si=see[i].size(); for (int j=0; j<si; j++) //i sees see[i][j] for (int k=j+1; k<si; k++) //i sees see[i][k] (promise j<k) { int sj=see[see[i][j]].size(); for (int l=0; l<sj; l++) //check if see[i][j] sees see[i][k] if (see[see[i][j]][l] == see[i][k]) { ans++; break; } } } printf("%d\n",ans); } return 0;}
- [ZOJ1391][POJ1436] Horizontally Visible Segments
- POJ1436 Horizontally Visible Segments
- poj1436 Horizontally Visible Segments
- POJ1436 Horizontally Visible Segments
- poj1436 Horizontally Visible Segments
- 【37%】【poj1436】Horizontally Visible Segments
- POJ1436:Horizontally Visible Segments(区间更新)
- poj1436 Horizontally Visible Segments 线段树
- poj1436 Horizontally Visible Segments 线段树成段更新
- poj1436 Horizontally Visible Segments(区间覆盖+暴力)
- POJ1436 Horizontally Visible Segments 线段树区间更新--区间覆盖
- 【POJ1436】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
- 约会
- 获取iPhone用户手机号
- LPC2478 通用输入/输出口(GPIO)
- android 锁屏
- 日语口语1.10 実はわが社は貴社の取り扱い製品に興味を持っておりますので
- [ZOJ1391][POJ1436] Horizontally Visible Segments
- vc中20个开发小技巧
- iPhone中关闭程序的可用方法
- 初学者分析MFC代码
- 同步机制
- createfile 中生成的文件如何清空?
- C/C++ 数学库函数
- 11/7/7 list
- Swing 窗体关闭对话框提示