hdu_5862_Counting Intersections(扫描线)
题目链接: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(扫描线)的更多相关文章
- HDU 5862 Counting Intersections 扫描线+树状数组
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Counting Intersections Time Limit: 12000/ ...
- HDU 5862 Counting Intersections (离散化+扫描线+树状数组)
题意:给你若干个平行于坐标轴的,长度大于0的线段,且任意两个线段没有公共点,不会重合覆盖.问有多少个交点. 析:题意很明确,可是并不好做,可以先把平行与x轴和y轴的分开,然后把平行y轴的按y坐标从小到 ...
- hdu-5862 Counting Intersections(线段树+扫描线)
题目链接: Counting Intersections Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- HUD 4007 [扫描线][序]
/* 大连热身B题 不要低头,不要放弃,不要气馁,不要慌张 题意: 坐标平面内给很多个点,放置一个边长为r的与坐标轴平行的正方形,问最多有多少个点在正方形内部. 思路: 按照x先排序,然后确定x在合法 ...
- Atitit 图像扫描器---基于扫描线
Atitit 图像扫描器---基于扫描线 调用范例 * @throws FileExistEx */ public static void main(String[] args) throws Fil ...
- 扫描线+堆 codevs 2995 楼房
2995 楼房 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 地平线(x轴)上有n个矩(lou)形(fan ...
随机推荐
- CUDA学习ing..
0.引言 本文记载了CUDA的学习过程~刚开始接触GPU相关的东西,包括图形.计算.并行处理模式等,先从概念性的东西入手,然后结合实践开始学习.CUDA感觉没有一种权威性的书籍,开发工具变动也比较快, ...
- shell 文件中列的整合成一个文件
原文件 第一种方法 [root@wxb- jt]# paste -d "," b c d ,q, , ,e, ,r, ,t, [root@wxb- jt]# paste b c d ...
- mysql中You can't specify target table for update in FROM clause
使用mysql在删除表中重复记录 delete from user where username in (select user name form(select username from user ...
- jQ内容的强大,后面继续跟进...
<script type="text/javascript" src="jQ/jquery.js"></script> <scri ...
- Linux下的数据监控工具
Vmstat Vmstat,virtual memmory statistics(虚拟内存统计),主要是对操作系统的内存信息.进程状态.cpu活动等进行监视,但是它不能对某个进程进行深入的分析. Pr ...
- RubyMine 2016.1 下载 附注册激活码 破解版方法
注册破解方法: 在要求输入注册的界面选择激活码,然后粘贴以下注册码: 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIj ...
- <hdu - 1600 - 1601> Leftmost Digit && Rightmost Digit 数学方法求取大位数单位数字
1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * ...
- 使用python修改QQ密保(脚本)
一.基于以下目的: 1.为了增加对Http协议理解能力,对QQ密保修改的请求进行了分析 2.为了锻炼python的编写能力 3.对web综合知识的理解 花了点时间写了这个脚本,下面介绍脚本的过程 二. ...
- SVN 版本控制工具
1,安装完服务端VisualSVN Server和客户端TortoiseSVN 后,随便在一个文件夹下,右键,会看到有SVN checkout 选项,这个选项只有在第一次在仓库下下载的时候会用到: 1 ...
- HTML5历史管理
边看视频边做的练习,随机显示数字,分别使用history和hash来实现历史管理 <!doctype html> <html> <head> <meta ch ...