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. 如何在InstallShield的MSI工程中调用Merge Module的Custom Action

    使用InstallShield创建了合并模块安装程序,定义自定义活动,可如何调用却不太清楚,网上也就找到这点信息,还是没有成功,到底该在什么地方执行合并模块的自定义活动? http://1662487 ...

  2. java核心技术 要点笔记1

    第1章 1.java特性 简单性,java语法是一个C++语法的纯净版本. 面向对象,java将重点放在数据和对象的接口上.java与C++的主要不同点在于多继承,在java中实现多继承的机制是采用接 ...

  3. 【转】转自微信公众号 JavaScript 复杂判断的更优雅写法

    与微信公众号看到一篇js复杂判断的文章,对我启发很大,故转到博客园以供后期不断学习并应用于项目.原文地址:https://mp.weixin.qq.com/s/ClFDRj4MnAxv1dJ5VWKS ...

  4. 记Tea使用中遇到的问题及解决过程

    学习Markdown时,在小众软件看到一个叫Tea的软件.UI设计是简约风格:"所见即所得"的Markdown:支持插件等原因让我选择去尝试这杯"茶". 最近一 ...

  5. 使TextBox的内容换行

    首先你把TextBox控件的MultiLine属性设置为True,然后把TextBox控件的Text属性根据程序需要,在需要换行的地方加入\r\n这样就可实现换行了

  6. 防止sql注入方法 如何防止java中将MySQL的数据库验证密码加上 ' or '1'= '1 就可以出现万能密码 的PreparedStatement

    package com.swift; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepar ...

  7. linux中管道(pipe)一谈

    /*********************************************** 管道(pipe)是Linux上进程间通信的一种方式,其是半双工(数据流只能在一个方向上流动(还需要经过 ...

  8. bat 服务启动脚本

    当电脑上有多个数据库(特别是Oracle,占用内存大,所以我都是设置为手动启动的,或者想在电脑上运行一下其他UI类软件或玩些游戏的时候也需要暂时关掉,奈何我这渣机(V_V))需要启动或停止的时候,就用 ...

  9. mysql--连接查询(内外连接)

    连接查询又称多表查询,查询到的字段来自于多个表中的数据. 一. 连接查询的分类和语法 1.分类 按标准分: 92标准:只支持内连接 99标准:支持内连接和.外连接和全外连接 功能进行分类: 内连接:i ...

  10. gitLab 服务器搭建 (自己服务器上搭建gitLab)

    环境 lunix(ubuntu) 1:添加文件 在   /etc/apt/sources.list.d/gitlab-ce.list 中添加一行 deb https://mirrors.tuna.ts ...