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. Maven -- 在进行war打包时用正式环境的配置覆盖开发环境的配置

    我们的配置文件一般都放在  src/main/resource 目录下. 假定我们的正式环境配置放在 src/main/online-resource 目录下. 那么打成war包时,我们希望用onli ...

  2. poj 2762 tarjan缩点+拓扑序

    2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...

  3. JqGrid自定义(图片)列

    $("#gridTable").jqGrid({ //...其它属性 colModel: [ //...其它列 { name: , align: "center" ...

  4. javascript性能

    1.js文件放在底部 js文件具有阻塞机制,放在头部,需要等待js下载解析完毕之后才能下载渲染页面,因此需要放在底部

  5. Part2-HttpClient官方教程-Chapter7-高级主题(Advanced topics) (HTTP Caching)

    原文链接 7.1 自定义客户端连接 在某些情况下,为了能够处理非标准的.不兼容的行为,可能需要自定义HTTP消息通过网络传输的方式,而不是使用HTTP参数.例如,对于web爬虫,可能有必要迫使Http ...

  6. [NOI2008] [bzoj1061] 志愿者招募

    还是一道费用流的题目.话不多说,进入正题. 题意:给定n个点和m种从l到r覆盖一层的费用,求满足所有点的覆盖层数都大等于权值的最小费用 分析:要做到区间修改,看似比较麻烦. 用差分把区间修改变成单点修 ...

  7. vue路由-编程式导航

    除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现. router.push(location, onComp ...

  8. TCP之Nagle算法与延迟ACK

    (一)Nagle算法 为了减少网络中小分组的数目,减少网络拥塞的情况.Nagle算法要求在一条TCP连接上最多只能有一个未被确认的未完成小分组,在该分组ACK到达之前不能够发送其他的小分组,发送端需要 ...

  9. C json实战引擎 二 , 实现构造部分

    引言 这篇博文和前一篇 C json实战引擎一,实现解析部分设计是相同的,都是采用递归下降分析. 这里扯一点 假如你是学生 推荐一本书 给 大家 自制编程语言 http://baike.baidu.c ...

  10. mybatis-plus的学习

    1.mybatisplus 提供了比较齐全的crud即增删改查,不需要在mapper.xml里写sql可以直接调用 原文链接:http://blog.csdn.net/u014519194/artic ...