Counting Intersections

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 51    Accepted Submission(s): 18

Problem Description
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.

 
Input
The 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.

 
Output
For 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
 
 对于横着的线,遇到左端点+1 遇到右端点-1  遇到竖着的线求和。
 
/* ***********************************************
Author :guanjun
Created Time :2016/8/18 14:20:26
File Name :p1006.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct node{
int type,x,y,y2;
}nod[maxn*];
int a[maxn*];
int Maxn;
bool cmp(node a,node b){
if(a.x==b.x)return a.type<b.type;
return a.x<b.x;
}
int c[maxn*];
int lowbit(int i){
return i&(-i);
}
void add(int i,int d){
while(i<=Maxn){
c[i]+=d;
i+=lowbit(i);
}
}
int sum(int i){
int ans=;
while(i>){
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
map<int,int>mp;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--){
mp.clear();
int n,tot=;
int all=;
scanf("%d",&n);
for(int i=;i<=n;i++){
int x1,x2,y1,y2;
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
if(x1==x2){
if(y1>y2)swap(y1,y2);
nod[tot++]={,x1,y1,y2};
a[all++]=y1;
a[all++]=y2;
}
else{
if(x1>x2)swap(x1,x2);
nod[tot++]={,x1,y1,};
nod[tot++]={,x2+,y2,-};
a[all++]=y1;
}
}
sort(a+,a+all);
int cnt=;
for(int i=;i<=all;i++){
if(!mp[a[i]])mp[a[i]]=++cnt;
}
Maxn=cnt+;
sort(nod,nod+tot,cmp);
ll ans=;
cle(c);
for(int i=;i<tot;i++){
if(nod[i].type==){
int xpy=mp[nod[i].y];
add(xpy,nod[i].y2);
}
else{
int xpl=mp[nod[i].y];
int xpr=mp[nod[i].y2];
ans+=sum(xpr)-sum(xpl-);
}
}
printf("%lld\n",ans);
}
return ;
}

HDU 5862Counting Intersections的更多相关文章

  1. hdu 5862 Counting Intersections

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

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

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

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

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

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

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

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

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

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

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

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

    题意:给你若干个平行于坐标轴的,长度大于0的线段,且任意两个线段没有公共点,不会重合覆盖.问有多少个交点. 析:题意很明确,可是并不好做,可以先把平行与x轴和y轴的分开,然后把平行y轴的按y坐标从小到 ...

  8. hdu 1086(计算几何入门题——计算线段交点个数)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2 ...

  9. HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

随机推荐

  1. (转)Eclipse在线配置Hibernate Tools

    http://blog.csdn.net/yerenyuan_pku/article/details/52733403 查看Eclipse版本 Eclipse针对程序开发有很多发行版本,除了开发语言的 ...

  2. Mybatis学习总结二

    Mapper动态代理开发方式 实现原理: Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象. Mapper接口开发需 ...

  3. mongodb--命令练习

    windows10安装下载mongodb # 官网或镜像地址下载mongodb exe 二进制安装包 # 安装时选用custorm 去除勾选compass可视化工具 # 安装完成创建data文件,修改 ...

  4. 虚拟机下Linux网络配置

    之前配置好了linux系统,在网络这块我用的是桥接模式. 现在分享一下使用虚拟机桥接模式配置Linux网络的过程. 一.首先配置外网的本地Ip地址. 二.配置Linux 网络链接 1.打开linux网 ...

  5. Swing之登录界面

    import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Login extends JFrame ...

  6. Python进阶-打包程序为exe

    操作系统:win7 x64 运行环境:Python3.5 安装PyInstaller 第一步:下载PyInstaller https://github.com/pyinstaller/pyinstal ...

  7. BC in fluent

    Boundary conditions in Fluent Table of Contents 1. Boundary Conditions (BC) 1.1. Turbulence Paramete ...

  8. 51NOD 1385凑数字(找规律?)

    >>点击进入原题测试<< 思路:这个题是真的想了蛮久,枚举了一下前一百就发现了规律,要想最短的话就是要构建1234567890这个字符串:刚开始找到的规律从1开始枚举到N,每满 ...

  9. 以位为单位存储标志-共用体-union

    一.程序的结构如下: typedef union _KEYST     {         struct         {             uint8 Key1_Flag :1;//表示第0 ...

  10. 【Codeforces 923A】Primal Sport

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 考虑怎么得到数字x2=N,假设是质数p的倍数 那么x1肯定在x2-p+1~x2这个范围内才行 因为p的倍数要刚好大于等于x1, 所以x1肯定是 ...