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. linux 拆分文件

    split [OPTION]... [INPUT [PREFIX]] :根据行或者大小拆分文件 split file_name :默认把文件file_name拆分成xaa,xab,xac,...... ...

  2. HDU_1166_敌兵布阵

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. node遍历给定目录下特定文件,内容合并到一个文件

    遍历目录用了fs.readdir这个异步方法,得到当前目录下所有的文件和目录的一个数组.然后判断: if文件,并且后缀符合设定的规则(本文例子是符合后缀ts,js)直接用同步方法写入, if目录,继续 ...

  4. react 中样式私有

    解决的问题,两个组件之间  有相同的class名,造成其中一个无法按预期的显示. import React, { Component } from 'react' import styles from ...

  5. idea之查看类的上下级继承关系

  6. notepad++使用NppFTP连接linux,编写shell脚本无法保存上传的问题

    下载安装NppFTP插件之后,重启打开notepad++连接到linux主机,之后进行编辑shell脚本,出现无法保存上传至linux主机的问题. 分析的原因:可能的原因是Windows防火墙阻止了应 ...

  7. Python学习——filter&map

    filter&map 1.filter函数 filter()函数用于过滤序列,过滤掉不符合条件的元素,Python3以后返回一个迭代器对象(可以用list()转化为列表查看). filter( ...

  8. [bzoj2242][SDOI2011][计算器] (Baby-Step-Giant-Step+快速幂+exgcd)

    Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...

  9. 【Java基础】基本类型与运算【重要】

    0.   Java基本数据类型 Java的位运算(bitwise operators)直接对整数类型的位进行操作,这些整数类型包括long.int.short.char和 byte,位运算符具体如下表 ...

  10. Bellman_ford 算法 Currency Exchange POJ1860

    Bellman_ford算法用于寻找正环或者负环! 算法导论: 24.1 The Bellman-Ford algorithm The Bellman-Ford algorithm solves th ...