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 ;
}

最新文章

  1. sublime3侧边栏颜色修改,推荐主题
  2. Python中的range函数用法
  3. Android LruCache(Picasso内存缓存)
  4. css2----清除浮动
  5. SQL 触发器 instead of | insert
  6. 2013MPD上海6.22 PM 陆宏杰:通往卓越管理的阶梯 &amp; 6.23AM Ray Zhang 产品创新管理的十八般武艺
  7. 一些站点使用的服务器软件、js 框架大收集 [ 整理中 ]
  8. Intent的4种传值方法总结
  9. VBS生成随机数
  10. python的交代一
  11. asterisk错误排查
  12. appendGrid的使用
  13. TCP粘包拆包问题
  14. Hybris安装和各个Extention简单介绍
  15. 用JAVA代码获取Weblogic配置的JNDI 数据源连接
  16. EF Core Model更新迁移
  17. [java]类初始化挺有意思的题目
  18. Spring_AOP 实现原理与 CGLIB 应用
  19. 一个完整的成年果蝇大脑的电子显微镜图谱 | A Complete Electron Microscopy Volume of the Brain of Adult Drosophila melanogaster
  20. Azure 中虚拟机的区域和可用性

热门文章

  1. 将腾讯视频客户端缓冲的文件转换为一个MP4格式文件
  2. 远程登录事件ID
  3. linux 命令——30 chown (转)
  4. Aizu 0121 Seven Puzzle(变进制数的完美hash)
  5. thymeleaf获取当前时间并格式化输出
  6. java基础编程——用两个栈来实现一个队列
  7. C#冒泡排序程序
  8. JS控制台的使用
  9. Vmware 不能上网
  10. BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁(dp)