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