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 ;
}

最新文章

  1. hiho一下123周 后缀数组四&#183;重复旋律
  2. No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource.
  3. 「C语言」文件的概念与简单数据流的读写函数
  4. SVN服务器搭建和使用(二)(转载)
  5. 注册页面的简单搭建(H5)
  6. vs2012相关配置
  7. Xcode 插件开发
  8. boostrap-非常好用但是容易让人忽略的地方------clearfix
  9. wp8数据存储--独立存储文件 【转】
  10. jsp映射为其他地址上去 ???
  11. CSS弹性盒布局(display:flex)
  12. 贝塞尔曲线UIBezierPath简单使用
  13. Jmeter(十七)Logic Controllers 之 Interleave Controller
  14. Sevlet处理HTML表单
  15. (转)Kangle配置文件
  16. Nginx 灰度实现方式(支持纯灰度,纯生产,50度灰及更多比例配置)
  17. DevExpress GridView 显示行号
  18. NetCore入门篇:(二)Net Core项目创建
  19. SpringCloud之服务注册-eureka
  20. [xsy2978]Product of Roots

热门文章

  1. jmeter进行http压力测试(图文小教程)
  2. 「6月雅礼集训 2017 Day11」jump
  3. bzoj 1878: [SDOI2009]HH的项链 ——树状数组+ 差分
  4. div圆角
  5. js_开发小技巧记录(一)
  6. Python中的subprocess模块
  7. 有向有权图的最短路径算法--Dijkstra算法
  8. POJ1014(多重背包)
  9. Intel call指令
  10. 用tkinter实现的gui小工具