Given some segments which are paralleled to the coordinate axis. You need to count the number of their intersection.

The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments with length 0.

InputThe first line contains an integer T, indicates the number of test case.

The first line of each test case contains a number n(1<=n<=100000), the number of segments. Next n lines, each with for integers, x1, y1, x2, y2, means the two endpoints of a segment. The absolute value of the coordinate is no larger than 1e9. 
OutputFor each test case, output one line, the number of intersection.Sample Input

2
4
1 0 1 3
2 0 2 3
0 1 3 1
0 2 3 2
4
0 0 2 0
3 0 3 2
3 3 1 3
0 3 0 2

Sample Output

4
0

求当前线段与坐标轴平行的直线的交点

可以用扫描线的,扫描线or离散化都是一种很神奇的存在方式

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+;
struct Node
{
int f,x,y,y1;
bool operator <(const Node &R)const
{
return (x==R.x?f<R.f:x<R.x);
}
} a[N];
int Maxn;
int yy[N],c[N];
void add(int x,int n)
{
for(int i=x; i<=Maxn; i+=i&-i)c[i]+=n;
}
int sum(int x)
{
int ans=;
for(int i=x; i>; i-=i&-i)ans+=c[i];
return ans;
}
unordered_map<int,int>M;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
M.clear(),memset(c,,sizeof c);
int n,ctot=,tot=;
scanf("%d",&n);
for(int i=,x1,x2,y1,y2; i<n; i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1==x2)
{
if(y1>y2)swap(y1,y2);
a[++ctot]= {,x1,y1,y2};
yy[++tot]=y1;
yy[++tot]=y2;
}
else
{
if(x1>x2)swap(x1,x2);
a[++ctot]= {,x1,y1,};
a[++ctot]= {,x2+,y2,-};
yy[++tot]=y1;
}
}
sort(yy+,yy+tot+);
Maxn=;
for(int i=; i<=tot; i++)if(!M[yy[i]])M[yy[i]]=++Maxn;
sort(a+,a+ctot+);
long long ans=;
for(int i=; i<=ctot; i++)
{
if(a[i].f)ans+=(sum(M[a[i].y1])-sum(M[a[i].y]-));
else add(M[a[i].y],a[i].y1);
}
printf("%lld\n",ans);
}
return ;
}

HDU5862 Counting Intersections的更多相关文章

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

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

  2. hdu 5862 Counting Intersections

    传送门:hdu 5862 Counting Intersections 题意:对于平行于坐标轴的n条线段,求两两相交的线段对有多少个,包括十,T型 官方题解:由于数据限制,只有竖向与横向的线段才会产生 ...

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

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

  4. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  5. Counting Intersections

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

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

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

  7. HDU 5862 Counting Intersections(离散化 + 树状数组)

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

  8. Hdu 5862 Counting Intersections(有n条线段,每一条线段都是平行于x轴或者y轴,问有多少个交点+树状数组区间求和单点跟新)

    传送门:Hdu 5862 Counting Intersections 题意:有n条线段,每一条线段都是平行于x轴或者y轴,问有多少个交点 分析: 基本的操作流程是:先将所有的线段按照横树坐标x按小的 ...

  9. 【HDU5862】Counting Intersections

    题意 有n条线段,且都平行于坐标轴.对于每条线段,给出两个端点的坐标.问一共有多少个线段的交点. 分析 最最简单的扫描法了.用线段树或者树状数组都可以. 由题目可知,线段只有两种,要么平行于x轴要么平 ...

随机推荐

  1. IOS Block动画

    ● + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)dur ...

  2. Java环境变量搭建(Windows环境)

    变量名:JAVA_HOME 变量值:C:\Program Files (x86)\Java\jdk1.8.0_91        // 要根据自己的实际路径配置 变量名:CLASSPATH 变量值:. ...

  3. c# 科学计数法值转换成正常值,返回字符串

    /// <summary> /// 科学计数法值转换成正常值 /// </summary> /// <param name="value">&l ...

  4. 阿里云服务器下安装LAMP环境(CentOS Linux 6.3) 安装与配置 Apache 服务

    想让我们的阿里云服务器成为一台 Web 服务器,我们需要安装一个 Web 服务器软件,比如 Apache ,或者 Nginx 等等.下面我们就一起来安装一个 Apache 服务. 我们可以使用 yum ...

  5. win10搭建FTP服务器

    下面就给大家讲解Win10搭建FTP服务器的详细操作方法. 1.首先,我们在Cortana中搜索控制面板并进入: 2.在控制面板-程序中,点击启用或关闭Windows功能: 3.在FTP服务器.Web ...

  6. PHP 批量操作 Excel

    自己封装了一个批量操作excel文件的方法,通过xls文件地址集合遍历,第三个参数传入一个匿名函数用于每个需求的不同进行的操作,实例中我想要得到列表中含有折字的行,封装成sql语句返回. xls文件超 ...

  7. IIC简介(转载)

    来自:https://www.cnblogs.com/zalebool/p/4214599.html IIC简介: IIC 即Inter-Integrated Circuit(集成电路总线),这种总线 ...

  8. B1066 图像过滤 (15分)

    B1066 图像过滤 (15分) 图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来.现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换. 输入格式 ...

  9. linux 查看CPU内存 网络 流量 磁盘 IO

    使用vmstat命令来察看系统资源情况 在命令行方式下,如何查看CPU.内存的使用情况,网络流量和磁盘I/O? Q: 在命令行方式下,如何查看CPU.内存的使用情况,网络流量和磁盘I/O? A: 在命 ...

  10. [GDOI2016][树链剖分+主席树]疯狂动物城

    题面 Description Nick 是只在动物城以坑蒙拐骗为生的狐狸,儿时受到偏见的伤害,放弃了自己的理想.他被兔子 Judy 设下圈套,被迫与她合作查案,而卷入意想不到的阴谋,历尽艰险后成为搭档 ...