这是一个区间更新的题目,先将区间放大两倍,至于为什么要放大可以这样解释,按照从左到右有4个区间,y值是[1,5],[1,2],[3,4],[1,4]如果不放大的话,查询[1,4]区间和前面区间的”可见“情况时,由于[1,2],[2,3]将2,3端点覆盖,最终得不到[1,5]和区间[1,4]”可见“的情况,而实际上两者的[2,3]区间是可以存在一条水平线将其直接相连的。接下来将要更新的区间按照x从小到大排序,然后依次查询,查询之后再更新就行。

这样可以得到一个描述各个区间的相互关系的图G[N],G[i]存储的是在区间i右边的”可见“的区间,最后暴力统计一下,就可以解决问题了。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define M (1<<15)
#define maxn 16000
#define N 8001 using namespace std; typedef struct
{
int x,s,e;
} Seg; Seg seg[N];
int color[M],Hash[N];/*color[rt]为-1表示区间没有完全覆盖,0和其他整数表示被相应的sticks完全覆盖,*/
int n; vector<int>G[N]; int cmp(Seg a,Seg b)
{
return a.x<b.x;
} void PushDown(int rt)
{
if(color[rt]!=-)
{
color[rt<<]=color[rt<<|]=color[rt];
color[rt]=-;
}
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
color[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(color[rt]!=-)
{
if(Hash[color[rt]]!=color[rt])
{
Hash[color[rt]]=color[rt];
G[color[rt]].push_back(c);
}
return;
}
int m=(l+r)>>;
if(L<=m)
query(L,R,c,lson);
if(R>m)
query(L,R,c,rson);
} int main()
{
int tc;
scanf("%d",&tc);
while(tc--)
{
memset(color,,sizeof(color));
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d%d%d",&seg[i].s,&seg[i].e,&seg[i].x);
seg[i].s<<=;
seg[i].e<<=;
G[i].clear();
}
sort(seg+,seg+n+,cmp);
for(int i=; i<=n; i++)
{
memset(Hash,,sizeof(Hash));
int l=seg[i].s;
int r=seg[i].e;
query(l,r,i,,maxn,);
update(l,r,i,,maxn,);
}
int cnt=;
for(int i=; i<=n; i++)
{
for(int j=; j<G[i].size(); j++)
{
int k=G[i][j];
for(int x=; x<G[i].size(); x++)
for(int w=; w<G[k].size(); w++)
if(G[k][w]==G[i][x])
cnt++;
}
}
printf("%d\n",cnt);
}
return ;
}

poj1436 Horizontally Visible Segments的更多相关文章

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

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

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

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

  3. poj 1436 && zoj 1391 Horizontally Visible Segments (Segment Tree)

    ZOJ :: Problems :: Show Problem 1436 -- Horizontally Visible Segments 用线段树记录表面能被看见的线段的编号,然后覆盖的时候同时把能 ...

  4. 【37%】【poj1436】Horizontally Visible Segments

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5200   Accepted: 1903 Description There ...

  5. (中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。

    Description There is a number of disjoint vertical line segments in the plane. We say that two segme ...

  6. POJ 1436 Horizontally Visible Segments

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

  7. POJ 1436 (线段树 区间染色) Horizontally Visible Segments

    这道题做了快两天了.首先就是按照这些竖直线段的横坐标进行从左到右排序. 将线段的端点投影到y轴上,线段树所维护的信息就是y轴区间内被哪条线段所覆盖. 对于一条线段来说,先查询和它能相连的所有线段,并加 ...

  8. 【解题报告】pojP1436 Horizontally Visible Segments

    http://poj.org/problem?id=1436 题目大意:有n条平行于x轴的线段,每条线段有y坐标,如果两条线段有一段x坐标数值相等,且中间没有其它线段阻隔,则称这两条线段"照 ...

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

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

随机推荐

  1. 封装一个ISortable接口

    using System;/// <summary>/// 排序规范/// </summary>/// <typeparam name="T"> ...

  2. js一些算法实现

    1.约瑟夫环实现 //附有调试 function joseph(n,p){ var arr=[]; for(var i=0;i<n;i++){ arr.push(i); } debugger; ...

  3. Android设计模式系列

    http://www.cnblogs.com/qianxudetianxia/category/312863.html Android设计模式系列(12)--SDK源码之生成器模式(建造者模式) 摘要 ...

  4. Java分布式处理技术(RMI,JDNI)

    http://hedaoyuan.blog.51cto.com/4639772/813702 1.1 RMI的基本概念 1.1.1 什么是RMI RMI(Remote Method Invocatio ...

  5. ios6-7以后用户开热点后的屏幕适配

    // 排版时,注意logical coordinate space和device coordinate space的区别,注意frame和bounds的区别! - (void)loadView { / ...

  6. IOS 学习笔记 2015-04-15 手势密码(原)

    // // WPSignPasswordView.h // 网投网 // // Created by wangtouwang on 15/4/9. // Copyright (c) 2015年 wan ...

  7. (转)IOS中获取各种文件的目录路径的方法

    iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...

  8. hibernate符合主键

    当有符合主键时,一方与多方的复合主键顺序必须一致: <set> <key> <column name="A" /> <column nam ...

  9. Java基础巩固----泛型

    注:参考书籍:Java语言程序设计.本篇文章为读书笔记,供大家参考学习使用 1.使用泛型的主要优点是能够在编译时而不是在运行时检查出错误,提高了代码的安全性和可读性,同时也提高了代码的复用性. 1.1 ...

  10. 初用jquery

    ---恢复内容开始--- 这两天在顶顶大人的指导下,利用jquery框架做了一个动态切换的小页面.最终效果图: 这么萌萌哒的图片让我觉得一直在测试也没那么累.实现功能如下: 1.打开页面时,自动切换, ...