Counting Intersections

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

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
Author
BUPT
Source

【分析】给你一些与坐标轴平行的线段,问有多少对线段相交。

对于这种N*N可以办到但是超时的统计问题,一般都树状数组来统计。先将坐标离散化,然后横向线段存两个端点的横坐标,纵向的存一个横   坐标,然后排序,统计。若遇到一条横向线段的左端点,则纵坐标向上lowbit加一,若遇到纵向线段,统计这条线段的累加值,若遇到横向线     段的右端点,纵坐标向上lowbit减一,即删除,因为它已经没有贡献了。

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define met(a,b) memset(a,b,sizeof a)
#define inf 10000000
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N = 4e5+;
const double eps = 1e-;
int n,sum[N],m,cnt;
ll ans;
int lazy[N],a[N],mi[N],ma[N];
struct Line{
int u,y,z;
Line(int u=,int y=,int z=):u(u),y(y),z(z){}
bool operator <(const Line f)const{
return u<f.u||u==f.u&&z<f.z;
}
};
vector<Line>r,c,q;
void init(){
cnt=ans=;
met(a,);
r.clear();c.clear();q.clear();
}
void add(int x,int num){
for(int i=x;i<N;i+=i&(-i)){
a[i]+=num;
}
}
int query(int x){
int ret=;
for(int i=x;i>=;i-=i&(-i)){
ret+=a[i];
}
return ret;
}
int main() {
int T,x,y,xx,yy;
scanf("%d",&T);
while(T--){
init();
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d%d%d",&x,&y,&xx,&yy);
mi[++cnt]=x;mi[++cnt]=xx;
mi[++cnt]=y;mi[++cnt]=yy;
if(x==xx){
if(y>yy)swap(y,yy);
c.pb(Line(x,y,yy));
}
if(y==yy){
if(x>xx)swap(x,xx);
r.pb(Line(y,x,xx));
}
}
sort(mi+,mi++cnt);
cnt=unique(mi+,mi++cnt)-mi-;
for(int i=;i<c.size();i++){
c[i].u=lower_bound(mi+,mi++cnt,c[i].u)-mi;
c[i].y=lower_bound(mi+,mi++cnt,c[i].y)-mi;
c[i].z=lower_bound(mi+,mi++cnt,c[i].z)-mi;
q.pb(Line(c[i].u,i,));
}
for(int i=;i<r.size();i++){
r[i].u=lower_bound(mi+,mi++cnt,r[i].u)-mi;
r[i].y=lower_bound(mi+,mi++cnt,r[i].y)-mi;
r[i].z=lower_bound(mi+,mi++cnt,r[i].z)-mi;
q.pb(Line(r[i].y,i,));
q.pb(Line(r[i].z,i,));
}
sort(q.begin(),q.end());
for(Line s:q){
if(s.z==)add(r[s.y].u,);
else if(s.z==)ans+=query(c[s.y].z)-query(c[s.y].y-);
else add(r[s.y].u,-);
}
printf("%lld\n",ans);
}
return ;
}

HDU 5862 Counting Intersections(离散化 + 树状数组)的更多相关文章

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

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

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

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

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

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

  4. hdu 3015 Disharmony Trees (离散化+树状数组)

    Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

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

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

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

  7. HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)

    6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...

  8. 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...

  9. hdu 5862 Counting Intersections

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

  10. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

随机推荐

  1. LightOJ 1028 - Trailing Zeroes (I) 质因数分解/排列组合

    题意:10000组数据 问一个数n[1,1e12] 在k进制下有末尾0的k的个数. 思路:题意很明显,就是求n的因子个数,本来想直接预处理欧拉函数,然后拿它减n就行了.但注意是1e12次方法不可行.而 ...

  2. spring mvc 注解详解

    1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...

  3. Value does not fall within the expected range 值不在预期的范围内

    用vs2012 打开web.config时,提示如下错误:“Value does not fall within the expected range”; 中文提示:“值不在预期的范围内” 解决方案: ...

  4. vue router mode 设置"hash"与"history"的区别

    router官网的说明如下: ********************************************我是官网说明分隔符--开始**************************** ...

  5. 玩一下易语言 "和"字有多种读音,注定了它的重要性!!

    变量名 类型 静态 数组 备注 拼音 文本型   0   测试的汉字 文本型       有几种发音 整数型       i 整数型       测试用的汉字 = “和” 有几种发音 = 取发音数目 ...

  6. 解决ie9以下下不支持html5和媒体查询(Media Queries)

    ie9以下不支持媒体查询和html5,可以使用补丁完美兼容 1.html5shiv ie6~8不识别html5的新元素,可以通过使用html5shiv来解决 <!--[if lt IE 9]&g ...

  7. hydra 密码破解工具详解

    一.简介 hydra是著名黑客组织thc的一款开源的暴力密码破解工具,可以在线破解多种密码.官 网:http://www.thc.org/thc-hydra,可支持AFP, Cisco AAA, Ci ...

  8. 64_l3

    libguac-client-ssh-0.9.13-3.20170521git6d2cfda...> 23-May-2017 09:58 64570 libguac-client-ssh-0.9 ...

  9. 基础的语法知识(static关键字)

    1.C++中的局部变量.全局变量.局部静态变量.全局静态变量的区别 局部变量(Local variables)与 全局变量: 在子程序或代码块中定义的变量称为局部变量,在程序的一开始定义的变量称为全局 ...

  10. canvas写的地铁地图

    更新: 18-9-21:填了个坑,更新了canvas绘制过程. 根据的是百度提供的坐标,canvas的坐标是大的坐标在后面,所以跟实际生活方向相反. 所以canvas里的北方在下方,实际生活中北方在上 ...