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. 国外一些好用的UX/UI设计工具和资源介绍

    你今天使用的设计工具也许不再适合以后的网页和APP设计项目了.新的工具不断的推出市场,目标只有一个,让你的工作更快.更容易而且工作成效更好.以下就是各种工具的介绍入口,当您点击标题就会看到各种很好的工 ...

  2. Microsoft Access Engine

    在64位Win7操作系统中安装Microsoft Access Engine的解决方案 原创 2014年01月06日 19:33:56 44847 现在的Win7系统中安装的一般都是32位的Offic ...

  3. 浅谈IFC

    IFC布局规则: 在一个行内格式化上下文中,盒是一个接一个水平放置的,从包含块的顶部开始 这些盒之间的水平margin,border和padding都有效 盒可能以不同的方式竖直对齐:以它们的底部或者 ...

  4. libevent reference Mannual II--library

    FYI: http://www.wangafu.net/~nickm/libevent-book/TOC.html The Libevent Reference Manual: Preliminari ...

  5. HDU - 4810 - Wall Painting (位运算 + 数学)

    题意: 从给出的颜料中选出天数个,第一天选一个,第二天选二个... 例如:第二天从4个中选出两个,把这两个进行异或运算(xor)计入结果 对于每一天输出所有异或的和 $\sum_{i=1}^nC_{n ...

  6. CentOS 6磁盘管理

    1.添加4块8G硬盘, 注:要先添加SCSI控制器,再添加SCSI硬盘 2.查看添加的硬盘 3.fdisk分区交互式命令 d delete a partition——————//删除一个分区 n ad ...

  7. [bzoj3671][Noi2014][随机数生成器] (贪心+位运算+卡空间)

    Description Input 第1行包含5个整数,依次为 x_0,a,b,c,d ,描述小H采用的随机数生成算法所需的随机种子.第2行包含三个整数 N,M,Q ,表示小H希望生成一个1到 N×M ...

  8. js中匿名函数的N种写法

    匿名函数没有实际名字,也没有指针,怎么执行? 关于匿名函数写法,很发散~ +号是让函数声明转换为函数表达式.汇总一下 最常见的用法: 代码如下: (function() {  alert('water ...

  9. 【04】JSONP 教程

    JSONP 教程 Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获取资料,即跨域读取数据. 为什么我们从不同的域 ...

  10. Dubbo 是一个分布式服务框架

    Dubbo 是一个分布式服务框架-----http://www.cnblogs.com/chanshuyi/p/5144288.html