题目链接:hdu_5862_Counting Intersections

题意:

给你与坐标轴平行的线段,问你交点数

题解:

实质就是扫描线,这里我用树状数组来记录,所有线段按X坐标排序,遇到横线段的左端点就对应y坐标+1,遇到右端点,就对应y坐标-1,遇到竖线段,就询问对应的区间段

 #include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
typedef long long ll; const int N=1e5+;
int sum[N*],n,t,nn,has[N*],cnt,x1,x2,y1,y2,ed,hed; struct seg{
int x,l,r,op;
bool operator<(const seg &b)const{return x<b.x||(x==b.x&&op<b.op);}
}s[N*]; inline void add(int x,int c){while(x<=n)sum[x]+=c,x+=x&-x;}
inline int ask(int x){int an=;while(x)an+=sum[x],x-=x&-x;return an;} inline int getid(int x){return lower_bound(has+,has+cnt+,x)-has;} int main(){
scanf("%d",&t);
while(t--)
{
scanf("%d",&nn),ed=hed=;
memset(sum,,sizeof(sum));
F(i,,nn)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
has[++hed]=x1,has[++hed]=x2,has[++hed]=y1,has[++hed]=y2;
if(x1==x2)s[++ed].x=x1,s[ed].op=,s[ed].l=min(y1,y2),s[ed].r=max(y2,y1);
else{
s[++ed].x=min(x1,x2),s[ed].op=,s[ed].l=y1,s[ed].r=y1;
s[++ed].x=max(x1,x2),s[ed].op=,s[ed].l=y2,s[ed].r=y2;
}
}
sort(has+,has++hed),cnt=unique(has+,has++hed)-has,n=hed;
F(i,,ed)s[i].x=getid(s[i].x),s[i].l=getid(s[i].l),s[i].r=getid(s[i].r);
sort(s+,s++ed);
ll ans=;
F(i,,ed)if(s[i].op==)add(s[i].l,);
else if(s[i].op==)ans+=ask(s[i].r)-ask(s[i].l-);
else add(s[i].r,-);
printf("%lld\n",ans);
}
return ;
}

hdu_5862_Counting Intersections(扫描线)的更多相关文章

  1. HDU 5862 Counting Intersections 扫描线+树状数组

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Counting Intersections Time Limit: 12000/ ...

  2. HDU 5862 Counting Intersections (离散化+扫描线+树状数组)

    题意:给你若干个平行于坐标轴的,长度大于0的线段,且任意两个线段没有公共点,不会重合覆盖.问有多少个交点. 析:题意很明确,可是并不好做,可以先把平行与x轴和y轴的分开,然后把平行y轴的按y坐标从小到 ...

  3. hdu-5862 Counting Intersections(线段树+扫描线)

    题目链接: Counting Intersections Time Limit: 12000/6000 MS (Java/Others)     Memory Limit: 65536/65536 K ...

  4. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

  5. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  6. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  7. HUD 4007 [扫描线][序]

    /* 大连热身B题 不要低头,不要放弃,不要气馁,不要慌张 题意: 坐标平面内给很多个点,放置一个边长为r的与坐标轴平行的正方形,问最多有多少个点在正方形内部. 思路: 按照x先排序,然后确定x在合法 ...

  8. Atitit 图像扫描器---基于扫描线

    Atitit 图像扫描器---基于扫描线 调用范例 * @throws FileExistEx */ public static void main(String[] args) throws Fil ...

  9. 扫描线+堆 codevs 2995 楼房

    2995 楼房  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 地平线(x轴)上有n个矩(lou)形(fan ...

随机推荐

  1. openstack trove,使pylint忽略错误

    一.什么是pylint Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码. Pylint 是一个 Python 工具,除了 ...

  2. JAVA-代理学习一之JDK实现

    代理的实现依赖于反射,建议不太懂反射的童鞋先看看反射相关的知识点. 代理可以理解为对实际调用方法的一种能力的加强. 代理分为静态代理和动态代理: <1> 静态代理示例 接口MyInterf ...

  3. java list<string>集合 传递值给js的数组

    转载地址:http://blog.sina.com.cn/s/blog_611f65fd0100msc6.html. 1.Action 中代码              List result = n ...

  4. RedisDesktopManager

    下载地址: https://github.com/uglide/RedisDesktopManager/releases

  5. BIEE基本函数

    一,TRIM ,去除空字符 TRIM(EXPR) SUBSTRING("UT TIME"."月份" FROM 6 FOR 2) 1.AGGREGATE AT 此 ...

  6. Kmplayer播放器 绿色免安装版 2016 中文版

    软件名称: Kmplayer播放器 绿色免安装版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 42.8MB 图片预览: 软件简介: Kmplayer播放 ...

  7. Information:java: javacTask: 源发行版 1.8 需要目标发行版 1.8

    1,Project Structure里确认两个地方:Project sdk以及project language level 2,Project Structure->Modules里Sourc ...

  8. PHP静态延迟绑定简单示例

    没怎么用过这个新特性,其实也不算新啦,试试吧,现在静态类的继承很方便了 <?php class A { protected static $def = '123456'; public stat ...

  9. c++ 随手记

    强类型的理解 先定义一些基础概念 Program Errors trapped errors.导致程序终止执行,如除0,Java中数组越界访问 untrapped errors. 出错后继续执行,但可 ...

  10. SQL2008将服务器的数据库表数据插入到本地数据库

    一,配置参数 exec sp_configure reconfigure exec sp_configure RECONFIGURE 若不配置参数会出现,提示这个错误: SQL Server 阻止了对 ...