Description

  There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they can be connected by a horizontal line segment that does not have any common points with other vertical segments. Three different vertical segments are said to form a triangle of segments if each two of them are horizontally visible. How many triangles can be found in a given set of vertical segments? 
 
  题意大致就是说给你一些线段,问相邻两个线段有没有公共的部分,且没有其他线段阻挡。
  
  首先对x排序,然后枚举,对于每个线段,先询问这个区间上的所有其他线段,然后再覆盖更新。
  对于COL数组,如果为-1表示这个区间上有多个线段,否则的话表示的是线段的编号(这里假设先放了一个编号为0的线段。)
  (PS:我能说这个题我调试了一下午吗,刚开始把COL设置为了bool的变量,结果。。。T T , 一直没找出错误来。。。痛苦。。。)
 
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib> #define lson L,M,po*2
#define rson M+1,R,po*2+1 using namespace std; const int N=*; int COL[**];
bool map1[][]; void pushDown(int po)
{
if(COL[po]>=)
{
COL[po*]=COL[po*+]=COL[po];
COL[po]=-;
}
} void update(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
COL[po]=ut; return;
} pushDown(po); int M=(L+R)/; if(ul<=M)
update(ul,ur,ut,lson);
if(ur>M)
update(ul,ur,ut,rson); if(COL[po*]==COL[po*+]) //这里算是一个剪枝操作,可以减少递归次数。
COL[po]=COL[po*];
else
COL[po]=-;
} void query(int ql,int qr,int qt,int L,int R,int po)
{
if(COL[po]!=-)
{
map1[qt][COL[po]]=map1[COL[po]][qt]=;
return;
} if(L==R)
return; int M=(L+R)/; if(ql<=M)
query(ql,qr,qt,lson);
if(qr>M)
query(ql,qr,qt,rson);
} struct state
{
int y1,y2,x1;
}; state sta[]; int cmp(const void*a,const void*b)
{
return (*(state*)a).x1-(*(state*)b).x1;
} int main()
{
int d;
cin>>d; int n; while(d--)
{
memset(map1,,sizeof(map1));
memset(COL,,sizeof(COL)); cin>>n; for(int i=;i<n;++i)
scanf("%d %d %d",&sta[i].y1,&sta[i].y2,&sta[i].x1); qsort(sta,n,sizeof(state),cmp); for(int i=;i<=n;++i)
{
query(sta[i-].y1*,sta[i-].y2*,i,,N,);
update(sta[i-].y1*,sta[i-].y2*,i,,N,);
} long long ans=; for(int i=;i<=n;++i) //直接暴力求解,居然不超时。。。
for(int j=i+;j<=n;++j)
if(map1[i][j])
for(int k=j+;k<=n;++k)
if(map1[i][k]&&map1[j][k])
++ans; cout<<ans<<endl;
} return ;
}

(中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。的更多相关文章

  1. POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)

    水博客,水一水. Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536K Total Submissions:  ...

  2. POJ 1436 Horizontally Visible Segments (线段树&#183;区间染色)

    题意   在坐标系中有n条平行于y轴的线段  当一条线段与还有一条线段之间能够连一条平行与x轴的线不与其他线段相交  就视为它们是可见的  问有多少组三条线段两两相互可见 先把全部线段存下来  并按x ...

  3. POJ 1436 Horizontally Visible Segments(线段树)

    POJ 1436 Horizontally Visible Segments 题目链接 线段树处理染色问题,把线段排序.从左往右扫描处理出每一个线段能看到的右边的线段,然后利用bitset维护枚举两个 ...

  4. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  5. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  6. POJ 1436 Horizontally Visible Segments

    题意: 有一些平行于y轴的线段 ,两条线段称为互相可见当且仅当存在一条水平线段连接这两条  与其他线段没交点. 最后问有多少组  3条线段,他们两两是可见的. 思路: 线段树,找出两两可见的那些组合, ...

  7. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  8. (中等) UESTC 360 Another LCIS ,线段树+区间更新。

    Description: For a sequence S1,S2,⋯,SN, and a pair of integers (i,j), if 1≤i≤j≤N and Si<Si+1<S ...

  9. poj 2777 Count Color(线段树 区间更新)

    题目:http://poj.org/problem?id=2777 区间更新,比点更新多一点内容, 详见注释,  参考了一下别人的博客.... 参考博客:http://www.2cto.com/kf/ ...

随机推荐

  1. HDU2952:Counting Sheep(DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  2. POJ 2773 Happy 2006(欧几里德算法)

    题意:给出一个数m,让我们找到第k个与m互质的数. 方法:这题有两种方法,一种是欧拉函数+容斥原理,但代码量较大,另一种办法是欧几里德算法,比较容易理解,但是效率很低. 我这里使用欧几里德算法,欧几里 ...

  3. laytpl.js 模板使用记录

    {{# for(var j = 0, len = d.length; j < len; j++){ }} <div class="pure-u-1-5 pure-u-sm-1 p ...

  4. Linux链接VPN进行转发

    1.安装client sudo apt-get install pptp-linux 2.连接vpn server sudo pptpsetup --create pptpd --server x.x ...

  5. 任意2个io直接驱动LCD1602,并且不需外加芯片(转)

    http://www.amobbs.com/thread-4301955-1-1.html *此处只摘录部分内容,详细内容请关注原贴. 这就是电路,细心的朋友会发现实物图中有几个贴片的阻容件,秘密就在 ...

  6. Ubuntu新建用户

    新建用户的命令是useradd,修改密码是passwd,如下: sudo useradd linc sudo passwd linc 但是问题出现了,home目录下并没有相对应的linc目录. 原来u ...

  7. 小红的难题<递推>

    题意:五个数:N,x,y,A,B;N是台阶总数,x,y是每步可以走x或者y步,但是一定要走到A,B台阶上. 思路:学长给的题解,递推,稍微优化一点. >重点在递推 #include<cst ...

  8. drawRect 进阶

    iOS的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView 的类,并重写drawRect方法,在这里进行绘图操作,程序会自动 ...

  9. 总结自己的Git常用命令

    总结自己的Git常用命令 使用git也有一段时间了,把自己常用的命令用自己的描述记录起来,方便自己备忘也方便其他人参考. 目录: 最基本的命令: git clone 拷贝并跟踪远程的master分支. ...

  10. 半透命opacity:(0-1),对于IE6版本不支持需要用filter:alpha(opacity=0-100)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...