Counting Intersections

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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
分析:对于横向线段只保留两个端点并记录类型,对竖向线段保留整个线段;
   离散化并排序,遍历竖向线段,对横向端点树状数组更新;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=2e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,x[maxn],y[maxn],p[maxn],xcnt,ycnt,id,idx;
ll ans;
multiset<int>s;
struct node
{
int x,low,high;
bool operator<(const node&p)const
{
return x<p.x;
}
}a[maxn];
struct node1
{
int x,y,type;
bool operator<(const node1&p)const
{
return x<p.x;
}
}g[maxn];
int get(int x)
{
int sum=;
for(int i=x;i;i-=(i&(-i)))
sum+=p[i];
return sum;
}
void add(int x,int y)
{
for(int i=x;i<=maxn-;i+=(i&(-i)))
p[i]+=y;
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
xcnt=ycnt=id=idx=;
ans=;
memset(p,,sizeof(p));
scanf("%d",&n);
rep(i,,n)
{
int b,c,d,e;
scanf("%d%d%d%d",&b,&c,&d,&e);
if(c>e)swap(c,e);
if(b>d)swap(b,d);
if(b==d)
{
x[++xcnt]=a[++id].x=b;
y[++ycnt]=a[id].low=c;
y[++ycnt]=a[id].high=e;
}
else
{
x[++xcnt]=b;
x[++xcnt]=d;
y[++ycnt]=c;
g[++idx].x=b;
g[idx].y=c;
g[idx].type=;
g[++idx].x=d;
g[idx].y=e;
g[idx].type=;
}
}
sort(x+,x+xcnt+);
sort(y+,y+ycnt+);
sort(a+,a+id+);
sort(g+,g+idx+);
int num1=unique(x+,x+xcnt+)-x-;
int num2=unique(y+,y+ycnt+)-y-;
rep(i,,id)
{
a[i].x=lower_bound(x+,x+num1+,a[i].x)-x;
a[i].low=lower_bound(y+,y+num2+,a[i].low)-y;
a[i].high=lower_bound(y+,y+num2+,a[i].high)-y;
}
rep(i,,idx)
{
g[i].x=lower_bound(x+,x+num1+,g[i].x)-x;
g[i].y=lower_bound(y+,y+num2+,g[i].y)-y;
}
int now=;
rep(i,,id)
{
while(now<=idx&&g[now].x<=a[i].x)
{
if(g[now].type==)add(g[now].y,);
else {
if(g[now].x!=a[i].x)
add(g[now].y,-);
else s.insert(g[now].y);
}
now++;
}
ans+=get(a[i].high)-get(a[i].low-);
for(int x:s)add(x,-);
s.clear();
}
printf("%lld\n",ans);
}
//system("Pause");
return ;
}

Counting 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(离散化 + 树状数组)

    Counting Intersections Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

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

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

  7. hdu-5862 Counting Intersections(线段树+扫描线)

    题目链接: Counting Intersections Time Limit: 12000/6000 MS (Java/Others)     Memory Limit: 65536/65536 K ...

  8. 2016暑假多校联合---Counting Intersections

    原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need t ...

  9. 【HDU5862】Counting Intersections

    题意 有n条线段,且都平行于坐标轴.对于每条线段,给出两个端点的坐标.问一共有多少个线段的交点. 分析 最最简单的扫描法了.用线段树或者树状数组都可以. 由题目可知,线段只有两种,要么平行于x轴要么平 ...

随机推荐

  1. Mac下截图快捷键

    Cmd+Shift+3:全屏截图:截取整个屏幕,保存截图至桌面文件夹.Cmd+Shift+4:区域截图:鼠标光标变成带坐标的小十字,通过拖拽截取特定区域,保存截图至桌面文件夹.Cmd+Shift+4 ...

  2. Underscore.js 的模板功能介绍与应用

    Underscore是一个非常实用的JavaScript库,提供许多编程时需要的功能的支持,他在不扩展任何JavaScript的原生对象的情况下提供很多实用的功能,需要了解的朋友可以详细参考下   U ...

  3. flash 右键菜单隐藏与修改

    来源:http://blog.sina.com.cn/s/blog_7264c84401014fmd.html import flash.ui.ContextMenu;import flash.ui. ...

  4. UIWebView是什么

    UIWebView类是用来显示网络内容.要使用它,可以简单的创造一个UIWebView对象,放置到窗口上,并且发送一个指向网络内容的请求.通过这个类,可以控制网页历史的前进後退,也可以通过程序去控制网 ...

  5. myeclipse8.6安装svn插件

    1.从官方网站下载site-1.6.16.zip,网址:subclipse.tigris.org: 2.将解压出来的features与plugins,复制到任意目录:Genuitec/MyEclips ...

  6. 分布式事务实现-Spanner

    Spanner要满足的external consistency 是指:后开始的事务一定可以看到先提交的事务的修改.所有事务的读写都加锁可以解决这个问题,缺点是性能较差.特别是对于一些workload中 ...

  7. 启动apache服务时报错【the requested operation has failed】

    想要解决错误,首先要找到错误的原因. 使用ApacheMonitor.exe启动apache服务看不到任何错误的原因. 找到问题原因:cmd--命令端--切换到apache的bin目录,执行如下命令: ...

  8. zookeeper集群实例

    zookeeper是什么 Zookeeper,一种分布式应用的协作服务,是Google的Chubby一个开源的实现,是Hadoop的分布式协调服务,它包含一个简单的原语集,应用于分布式应用的协作服务, ...

  9. postfix日志分析pflogsumm

    1.pflogsumm功能:统计接收.投递.转发.延时.反弹.拒绝.保留.丢弃的邮件统计发件人和收件人 统计发送和接受方主机/域名 统计SMTPD连接数...... 2.安装# yum install ...

  10. # 泰语字符串字符分割 --- UTF-8编码格式

    1.泰语编码格式 泰语用的编码格式是:ISO 8859-11,这个是Latin编码系列,是从"ISO-8859-1"发展过来的,采用的是8bit一个字,所以泰语中的英文字母或者数字 ...