题目链接:

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
题意:
 
求这些与坐标轴平行的线段的交点有多少个;
 
思路:
 
差不多就是原来的一道CF的原题,传送门
交点的个数就是每个线段覆盖的点数的和减去所有线段覆盖的点,而所有的线段覆盖的点数就可以用扫描线算法变成求面积;
 
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+4;
int n,x1,x2,y3,y2,rec[2*N],num;
struct no
{
int l,r,h,flag;
};
no line[8*N];
struct nod
{
int l,r,cover;
ll sum;
};
nod tree[8*N];
int cmp(no x,no y)
{
return x.h<y.h;
}
void build(int node,int L,int R)
{
tree[node].l=L,tree[node].r=R;
tree[node].cover=tree[node].sum=0;
if(L>=R)return ;
int mid=(L+R)>>1;
build(2*node,L,mid);
build(2*node+1,mid+1,R);
}
void Pushup(int node)
{
if(tree[node].cover)
{
tree[node].sum=rec[tree[node].r+1]-rec[tree[node].l];
}
else
{
if(tree[node].l==tree[node].r)tree[node].sum=0;
else tree[node].sum=tree[2*node].sum+tree[2*node+1].sum;
}
}
void update(int node,int L,int R,int x)
{
if(L<=tree[node].l&&R>=tree[node].r)
{
tree[node].cover+=x;
Pushup(node);
return ;
}
int mid=(tree[node].l+tree[node].r)>>1;
if(L>mid) update(2*node+1,L,R,x);
else if(R<=mid)update(2*node,L,R,x);
else
{
update(2*node,L,mid,x);
update(2*node+1,mid+1,R,x);
}
Pushup(node);
}
int bi(int x)
{
int L=1,R=num-1,mid;
while(L<=R)
{
mid=(L+R)>>1;
if(rec[mid]==x)return mid;
else if(rec[mid]>x)R=mid-1;
else L=mid+1;
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ll sum=0;
int cnt=1;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d%d",&x1,&y3,&x2,&y2);
if(x1>x2)swap(x1,x2);
if(y3>y2)swap(y3,y2);
rec[cnt] = line[cnt].l = x1;
line[cnt].r = x2+1;
line[cnt].h = y3;
line[cnt++].flag = 1;
line[cnt].l = x1;
rec[cnt] = line[cnt].r = x2+1;
line[cnt].h = y2+1;
line[cnt++].flag = -1;
if(x1==x2)sum=sum+abs(y2-y3)+1;
else sum=sum+abs(x1-x2)+1;
}
sort(line+1,line+cnt,cmp);
sort(rec+1,rec+cnt);
num = 2;
for(int i = 2;i < cnt;i++)
{
if(rec[i]!=rec[i-1])rec[num++]=rec[i];
}
build(1,1,num-1);
ll ans=0;
for(int i = 1;i < cnt-1;i++)
{
int fx = bi(line[i].l);
int fy = bi(line[i].r)-1;
if(fx <= fy)
{
update(1,fx,fy,line[i].flag);
}
ans+=tree[1].sum*(ll)(line[i+1].h-line[i].h);
}
// cout<<ans<<endl;
//cout<<sum<<" ";
printf("%lld\n",sum-ans);
}
return 0;
}

  

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(有n条线段,每一条线段都是平行于x轴或者y轴,问有多少个交点+树状数组区间求和单点跟新)

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

  4. hdu 5862 Counting Intersections

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

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

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

  6. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  7. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  8. HDU 6096 String 排序 + 线段树 + 扫描线

    String Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Problem De ...

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

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

随机推荐

  1. Ant Design 3.0 使用案例

    代码地址如下:http://www.demodashi.com/demo/12309.html 本文适合对象 有过React使用经验. 有过webpack使用经验. 了解node. DEMO使用方式 ...

  2. 基于Repository模式设计项目架构—你可以参考的项目架构设计

    关于Repository模式,直接百度查就可以了,其来源是<企业应用架构模式>.我们新建一个Infrastructure文件夹,这里就是基础设施部分,EF Core的上下文类以及Repos ...

  3. tomcat报错: Error parsing HTTP request header

    Error parsing HTTP request header 在服务器上面集成项目的时候,tomcat报错,在往上面查找是因为eclipse运行的tomcat和服务器上面的tomcat版本不一致 ...

  4. 多域名THINKPHP利用MEMCACHE方式共享SESSION数据(转)

    一.问题起源 稍大一些的网站,通常都会有好几个服务器,每个服务器运行着不同功能的模块,使用不同的二级域名,而一个整体性强的网站,用户系统是统一的,即一套用户名.密码在整个网站的各个模块中都是可以登录使 ...

  5. Bootstrap学习速查表(三) 表单

    表单中常见的元素主要包括:文本输入框.下拉选择框.单选按钮.复选按钮.文本域和按钮等. 一.基础表单 1.初始化:对于基础表单,Bootstrap并未对其做太多的定制性效果设计,仅仅对表单内的fiel ...

  6. 结缘mac

    还记得上一次买MacBookPro.是在去年的7月下旬,记得那次是我大学第一次买电脑,那时候刚准备開始研究android.听stormzhang以及android开发界的大佬们对mac开发androi ...

  7. Spark源码分析之八:Task运行(二)

    在<Spark源码分析之七:Task运行(一)>一文中,我们详细叙述了Task运行的整体流程,最终Task被传输到Executor上,启动一个对应的TaskRunner线程,并且在线程池中 ...

  8. 【WPF学习笔记】之如何通过后台C#代码添加(增/删/改按钮)实现对SQLServer数据库数据的更改

    首先,需要连接SQLServer数据库的服务器名称server.数据库名database.数据库用户名uid以及密码pwd,如下图: 然后需要以下数据库SQL代码段,还有一个myHelper.cs代码 ...

  9. Encoding::CompatibilityError: incompatible character encodings: GBK and UTF-8

    直接grunt serve读的css是.tmp/css/main.css 而这个文件不通过build生成出来是这样: /* Encoding::CompatibilityError: incompat ...

  10. Android 异常解决方法【汇总】

    (1)异常:Android中引入第三方Jar包的方法(Java.lang.NoClassDefFoundError解决办法) 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方 ...