2016暑假多校联合---Counting Intersections
The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments with length 0.
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.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef long long LL;
LL ax[],ay[];
LL c[];
LL Lowbit(LL t)
{
return t&(t^(t-));
}
LL Sum(LL x)
{
LL sum = ;
while(x > )
{
sum += c[x];
x -= Lowbit(x);
}
return sum;
}
void add(LL li,LL d)
{
while(li<)
{
c[li]+=d;
li=li+Lowbit(li);
}
} struct Nodex
{
LL x1,x2;
LL y;
}nodex[]; struct Nodey
{
LL y1,y2;
LL x;
}nodey[]; struct Node
{
LL x;
LL y;
}node[]; bool cmp1(const Nodey s1,const Nodey s2)
{
return s1.x<s2.x;
} bool cmp2(const Node s1,const Node s2)
{
return s1.x<s2.x;
}
map<LL,LL>q1;
map<LL,LL>q2; int main()
{
LL T,n;
scanf("%I64d",&T);
while(T--)
{
q1.clear();
q2.clear();
scanf("%I64d",&n);
LL tot1=,tot2=;
for(LL i=;i<n;i++)
{
LL x1,x2,y1,y2;
scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2);
if(x1==x2)
{
nodey[tot2].y1=min(y1,y2);
nodey[tot2].y2=max(y1,y2);
nodey[tot2++].x=x1;
}
else
{
nodex[tot1].x1=min(x1,x2);
nodex[tot1].x2=max(x1,x2);
nodex[tot1++].y=y1;
}
} LL num1,num2;
for(LL i=;i<tot1;i++)
{
ax[i*]=nodex[i].x1;
ax[i*+]=nodex[i].x2;
}
num1=*tot1;
for(LL i=;i<tot2;i++)
{
ax[num1++]=nodey[i].x;
}
sort(ax,ax+num1);
LL tot=,pre=-;
for(LL i=;i<num1;i++)
{
if(ax[i]!=pre)
{
pre=ax[i];
q1[pre]=++tot;
}
} for(LL i=;i<tot2;i++)
{
ay[i*]=nodey[i].y1;
ay[i*+]=nodey[i].y2;
}
num2=*tot2;
for(LL i=;i<tot1;i++)
ay[num2++]=nodex[i].y;
sort(ay,ay+num2);
tot=,pre=-;
for(LL i=;i<num2;i++)
{
if(ay[i]!=pre)
{
pre=ay[i];
q2[pre]=++tot;
}
} for(LL i=;i<tot1;i++)
{
nodex[i].x1=q1[nodex[i].x1];
nodex[i].x2=q1[nodex[i].x2];
nodex[i].y=q2[nodex[i].y];
}
for(LL i=;i<tot2;i++)
{
nodey[i].y1=q2[nodey[i].y1];
nodey[i].y2=q2[nodey[i].y2];
nodey[i].x=q1[nodey[i].x];
} //for(LL i=0;i<tot1;i++)
//cout<<"x: "<<nodex[i].x1<<" "<<nodex[i].x2<<" "<<nodex[i].y<<endl;
//for(LL i=0;i<tot1;i++)
//cout<<"y: "<<nodey[i].y1<<" "<<nodey[i].y2<<" "<<nodey[i].x<<endl;
sort(nodey,nodey+tot2,cmp1);
tot=;
for(LL i=;i<tot1;i++)
{
node[tot].x=nodex[i].x1;
node[tot++].y=nodex[i].y;
node[tot].x=nodex[i].x2+;
node[tot++].y=-nodex[i].y;
}
sort(node,node+tot,cmp2);
LL j=,k=,sum=;
memset(c,,sizeof(c));
for(LL i=;i<=;i++)
{
while(node[j].x==i&&j<tot)
{
if(node[j].y>)
add(node[j].y,);
else
add(-node[j].y,-);
j++;
}
while(nodey[k].x==i&&k<tot2)
{
sum+=Sum(nodey[k].y2)-Sum(nodey[k].y1-);
k++;
}
if(j>=tot) break;
if(k>=tot2) break;
}
printf("%I64d\n",sum);
}
return ;
}
2016暑假多校联合---Counting Intersections的更多相关文章
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 2016暑假多校联合---Windows 10
2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...
- 2016暑假多校联合---Substring(后缀数组)
2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...
- 2016暑假多校联合---To My Girlfriend
2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...
- 2016暑假多校联合---A Simple Chess
2016暑假多校联合---A Simple Chess Problem Description There is a n×m board, a chess want to go to the po ...
- 2016暑假多校联合---Another Meaning
2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...
- 2016暑假多校联合---Death Sequence(递推、前向星)
原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...
- 2016暑假多校联合---Joint Stacks (STL)
HDU 5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...
- 2016暑假多校联合---GCD
Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...
随机推荐
- Mybatis逆向工程构建项目实例.
2016/11/06更新: 因为有博友可能需要这份代码, 所以我就直接发到百度云上面和大家共享, 如果链接失效请大家留言提示即可.下载地址: http://pan.baidu.com/s/1i57E8 ...
- iOS-ARC项目使用非ARC文件 MRC项目使用ARC文件
SDK4.0引入了ARC,到现在已经好几年了,开始发现有很多项目会混合使用这两个方案.比如: 1.自己的旧项目没有使用ARC,但是引入的第三方库却是使用了ARC的. 2.自己的新项目使用了ARC,但是 ...
- python学习 变量的操作 与 基本数据类型
一变量(本文使用python3.5): #变量:是计算机内存中的一块区域,变量可以存储规定范围内的值,变量可以改变#在python中变量是计算机内存中数据的引用 python的变量没有明显的类型,具体 ...
- js相册展示
自己写来用的,第一版草稿源码+效果图,功能优先,其他的再说,有时间再改进. <script src="http://common.cnblogs.com/script/jquery.j ...
- Findbugs介绍及使用方法
1. 简介 Findbugs 是一个静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题.利用这个工具,就可以在不实际运行程序的情况对软件进行分析.它可以帮助改 ...
- POJ1014 解题报告(DFS)
题目在此:http://poj.org/problem?id=1014 要看清题意呢,题中要求输入的是价值分别为1,2,3,4,5,6的大理石的个数,而不是6块价值为输入数字的大理石!选这个题主要想练 ...
- SQL Server 2014新特性-原生备份加密
注:本篇文章是IT68找我的约稿,原文地址:http://tech.it168.com/a2014/0610/1633/000001633147.shtml SQL Server 2014 ...
- Linux命令之diff
1.命令格式: diff[参数][文件1或目录1][文件2或目录2] 2.命令功能: diff命令能比较单个文件或者目录内容.如果指定比较的是文件,则只有当输入为文本文件时才有效.以逐行的方式,比较文 ...
- Net连接mysql的公共Helper类MySqlHelper.cs带MySql.Data.dll下载
MySqlHelper.cs代码如下: using System; using System.Collections.Generic; using System.Linq; using System. ...
- 使用Javascript监控前端相关数据
项目开发完成外发后,没有一个监控系统,我们很难了解到发布出去的代码在用户机器上执行是否正确,所以需要建立前端代码性能相关的监控系统. 所以我们需要做以下的一些模块: 一.收集脚本执行错误 functi ...