POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)
水博客,水一水。
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 6290 | Accepted: 2287 |
Description
Task
Write a program which for each data set:
reads the description of a set of vertical segments,
computes the number of triangles in this set,
writes the result.
Input
The first line of each data set contains exactly one integer n, 1 <= n <= 8 000, equal to the number of vertical line segments.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
yi', yi'', xi - y-coordinate of the beginning of a segment, y-coordinate of its end and its x-coordinate, respectively. The coordinates satisfy 0 <= yi' < yi'' <= 8 000, 0 <= xi <= 8 000. The segments are disjoint.
Output
Sample Input
1
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
Sample Output
1
题意:输入n表示有n条线段,n行每行输入y1,y2,x表示线段的下端点,上端点以及线段的位置(横坐标位置),对于第i条线段和第k条线段如果用一条平行线能够经过它们并且在它们之间不经过其他线段,则称这两条线段互为可见,求有多少3条线段两两可见(懒得写题意,直接贴的别人的)
代码:
//线段树区间更新,端点放大2倍
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int maxn=8e3+; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int col[maxn<<];
bool mp[maxn][maxn]; struct node{
int y1,y2,x,id;
}line[maxn]; bool cmp(node a,node b)
{
return a.x<b.x;
} void init()
{
memset(mp,,sizeof mp);
memset(line,,sizeof line);
memset(col,,sizeof col);
} void pushdown(int rt)
{
if(col[rt]){
col[rt<<]=col[rt<<|]=col[rt];
col[rt]=;
}
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R){
col[rt]=c;
return ;
} pushdown(rt);
int m=(l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
} void query(int L,int R,int c,int l,int r,int rt)
{
if(col[rt]){
mp[c][col[rt]]=;
return ;
} if(l==r){
return ;
} int m=(l+r)>>;
if(L<=m) query(L,R,c,lson);
if(R> m) query(L,R,c,rson);
} int main()
{
int t;
scanf("%d",&t);
while(t--){
init();
int n;
scanf("%d",&n);
int N=-;
for(int i=;i<=n;i++){
scanf("%d%d%d",&line[i].y1,&line[i].y2,&line[i].x);
line[i].id=i;line[i].y1*=;line[i].y2*=;N=max(N,max(line[i].y1,line[i].y2));
}
sort(line+,line++n,cmp);//按x轴从小到大排序,降维,只对y轴进行建树
for(int i=;i<=n;i++){
query(line[i].y1,line[i].y2,line[i].id,,N,);
update(line[i].y1,line[i].y2,line[i].id,,N,);
}
int ans=;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(mp[i][j]){
for(int k=;k<=n;k++){
if(mp[i][k]&&mp[k][j]) ans++;
}
}
}
}
printf("%d\n",ans);
}
}
POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)的更多相关文章
- (中等) 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 (线段树·区间染色)
题意 在坐标系中有n条平行于y轴的线段 当一条线段与还有一条线段之间能够连一条平行与x轴的线不与其他线段相交 就视为它们是可见的 问有多少组三条线段两两相互可见 先把全部线段存下来 并按x ...
- POJ 1436 Horizontally Visible Segments(线段树)
POJ 1436 Horizontally Visible Segments 题目链接 线段树处理染色问题,把线段排序.从左往右扫描处理出每一个线段能看到的右边的线段,然后利用bitset维护枚举两个 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- POJ 1436 Horizontally Visible Segments
题意: 有一些平行于y轴的线段 ,两条线段称为互相可见当且仅当存在一条水平线段连接这两条 与其他线段没交点. 最后问有多少组 3条线段,他们两两是可见的. 思路: 线段树,找出两两可见的那些组合, ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- poj 2777 Count Color(线段树 区间更新)
题目:http://poj.org/problem?id=2777 区间更新,比点更新多一点内容, 详见注释, 参考了一下别人的博客.... 参考博客:http://www.2cto.com/kf/ ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
随机推荐
- 定时任务-Windows任务
定时任务-Windows任务 什么是windows任务 windows系统自带一个任务管理组件.可以执行自己写的程序,发送电子邮件(需要邮件服务器),显示消息(就是桌面弹出一个窗口).用的最多的就 ...
- css,对包含有子元素的元素进行flex后,会影响原有的布局。如何后续处理
对包含有子元素的元素进行flex后,会影响原有的布局. 例如设置两个div,第一个div包含一个img(图片),第二个div包含多个p元素(对前面的img的说明).如下图 1:当对着两个两个div进行 ...
- canvas教程(一) 简介
什么是 canvas 按例是要介绍一下 canvas 的,在没有 canvas 之前,我们为了网页的效果,很多情况下是使用了图片来实现,不过用图片就需要加载,而且图片的体积也是一个问题,所以在 htm ...
- 二维码扫码登录原理及简单demo
扫码登录原理转载自: https://www.cnblogs.com/liyasong/p/saoma.html 需求介绍 首先,介绍下什么是扫码登录.现在,大部分同学手机上都装有qq和淘宝,天猫等这 ...
- idea 启动项目报错,more than one fragment with the name [spring web] was found
这是由于idea导入项目的时候有多个模块,并且有多个web.xml导致的,先删除对应的模块,后启动即可. 另外也有可能是spring的jar冲突,把冲突的jar删除即可.
- vue v-show无法动态更新的问题
本人之前学过angularJS,记得v-for绑定的数组,只要切换v-if = ''item.show'' 只要改变相关的值,就可以对应的值,视图就会重新渲染,但是在vue中却不灵了,找到答案了,需要 ...
- Vue+Django项目部署
本地项目配置 1 复制 luffy/settings/dev.py为prop.py 修改luffy/settings/prop.py中以下几项 (1) allow_hosts ALLOWED_HOST ...
- java Excel文件流输出(后台)没有下载提示
/**方案: *1. *问题源头应该是前端用的是ajax方式请求路径,要想出现下载Excel的弹窗,我个人理解应该是等同于重新打开一个窗口,而ajax默认不动页面直接将值返回,所以没有提升下载. *解 ...
- Bash基础——快捷键
参考:The Best Keyboard Shortcuts for Bash (aka the Linux and macOS Terminal) Navigating Bash History w ...
- c++练手项目:英语单词拼写测试程序
代码比较简单.基本的思路是从文本文件中按行读取数据,数据结构为“hello-你好”.前面是英语,后面是中文,中间用“-”连接.程序通过查找连词符的位置来分割中文和英文.再通过和用户输入的单词进行比较判 ...