HDU5862 Counting Intersections
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的更多相关文章
- hdu-5862 Counting Intersections(线段树+扫描线)
题目链接: Counting Intersections Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- hdu 5862 Counting Intersections
传送门:hdu 5862 Counting Intersections 题意:对于平行于坐标轴的n条线段,求两两相交的线段对有多少个,包括十,T型 官方题解:由于数据限制,只有竖向与横向的线段才会产生 ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- Counting Intersections
Counting Intersections Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU 5862 Counting Intersections 扫描线+树状数组
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Counting Intersections Time Limit: 12000/ ...
- HDU 5862 Counting Intersections(离散化 + 树状数组)
Counting Intersections Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- Hdu 5862 Counting Intersections(有n条线段,每一条线段都是平行于x轴或者y轴,问有多少个交点+树状数组区间求和单点跟新)
传送门:Hdu 5862 Counting Intersections 题意:有n条线段,每一条线段都是平行于x轴或者y轴,问有多少个交点 分析: 基本的操作流程是:先将所有的线段按照横树坐标x按小的 ...
- 【HDU5862】Counting Intersections
题意 有n条线段,且都平行于坐标轴.对于每条线段,给出两个端点的坐标.问一共有多少个线段的交点. 分析 最最简单的扫描法了.用线段树或者树状数组都可以. 由题目可知,线段只有两种,要么平行于x轴要么平 ...
随机推荐
- StringBuffer是可变的还是不可变的?
前言:我们知道String类的修饰符是final,其char[] value也是由final修饰的,每次给String变量赋一个新值,都会创建一个新的String对象,很多有涉及到字符串本身的改变都是 ...
- java Vamei快速教程17 多线程
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 多线程 多线程(multiple thread)是计算机实现多任务并行处理的一种方 ...
- 比特币中P2SH(pay-to-script-hash)多重签名的锁定脚本和解锁脚本
P2SH(pay-to-script-hash)多重签名的脚本 P2SH是多重签名的一种应用形式.在P2SH的交易中,多了一个Redeem Script的概念,称为赎回脚本.当向P2SH脚本的地址转账 ...
- NYOJ-596-谁是最好的Coder
原题链接 谁是最好的Coder 时间限制:1000 ms | 内存限制:65535 KB 难度:0 描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所以他 ...
- JQUERY操作JSON数组添加新的属性和值
语法: var data = {}; data["Order"] =order; data["Sort"] = sort; 但是需要注意的是,如果data后面还 ...
- Java中ArrayList的对象引用问题
前言事件起因是由于同事使用ArrayList的带参构造方法进行ArrayList对象复制,修改新的ArrayList对象中的元素(对象)的成员变量时也会修改原ArrayList中的元素(对象)的成员变 ...
- PAT (Basic Level) Practise (中文)- 1003. 我要通过!(20)
http://www.patest.cn/contests/pat-b-practise/1003 “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入 ...
- vscode wepy 用户自定义代码片段
VSCode wepy 自定义代码片段 { "wepy-page": { "prefix": "wepy", "body" ...
- 1048: [HAOI2007]分割矩阵
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1184 Solved: 863[Submit][Status][Discuss] Descripti ...
- Oracle - 存储过程、函数、包的使用练习-雇员
--存储过程范例:得到雇员表 emp 的记录数 begin --说明:若过程中要向外抛异常,请使用 exception when others then raise; 这个抛出的异常在程序里是可以捕获 ...