题目链接: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. Intellij 快捷键

    11ctrl+F12  类似于eclipse 的outline  显示出当前类的所有方法. ctrl+H 查看当前类或者方法的集成结构 ctrl+E 查看最近使用的文件 Ctrl+J 自动完成提示 C ...

  2. 双数组trie树的基本构造及简单优化

    一 基本构造 Trie树是搜索树的一种,来自英文单词"Retrieval"的简写,可以建立有效的数据检索组织结构,是中文匹配分词算法中词典的一种常见实现.它本质上是一个确定的有限状 ...

  3. 拿来之笔 希望铭记 笔记 出处 http://www.jianshu.com/p/acb8885283dc

    最近有机会对不同岗位的应聘者进行面试,其中有架构师.技术经理.开发岗位.谈谈几个印象深刻的. 面试者一,女性.重点大学硕士,从事软件技术工作十四年,应聘架构师岗位.按照套路问了下对于软件架构的认识和理 ...

  4. Ajax实现页面动态加载,添加数据

    前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Products ...

  5. could not resolve property问题(ssh框架)

    could not resolve property不能解析属性问题, 刚开始把hql语句中的"from User user where user.user_name = '"+u ...

  6. HTTPClient网络异常:java.lang.IllegalStateException: Content has been consumed

    在对代码进行重构时候,出现了一个异常,代码的网络请求使用的是HTTPClient: 但是其实代码中没有添加什么,只是添加了两句log: 后来发现是因为将EntityUtils.toString()方法 ...

  7. Python学习笔记——基础篇【第六周】——hashlib模块

    常用模块之hashlib模块 用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 import ...

  8. [DP之普通系列]

    noip快要来了 要练练dp 难度也挺接近 还是挺好的 [Usaco2013 Nov]Pogo-Cow 这一道题要下一段大于这一段 所以的话我们就要记录每一段的状态 F[i,j]=F[j,k]+A[i ...

  9. ActiveMQ in Action(7) - Wildcards

    关键字: activemq 2.6.7 Wildcards    Wildcards用来支持联合的名字分层体系(federated name hierarchies).它不是JMS规范的一部分,而是A ...

  10. 正确的IP地址正则表达式

    ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)