题目:
Statements

Dreamoon likes algorithm competitions very much. But when he feels crazy because he cannot figure out any solution for any problem in a competition, he often draws many meaningless straight line segments on his calculation paper.

Dreamoon's calculation paper is special: it can be imagined as the plane with Cartesian coordinate system with range [0, 2000] × [0, 2000] for the coordinates. The grid lines are all lines of the form x = c or y = c for every integer c between 0 and 2000, inclusive. So, the grid contains 2000 × 2000 squares.

Now, Dreamoon wonders how many grid squares are crossed by at least one of the lines he drew. Please help Dreamoon find the answer. Note that, to cross a square, a segment must contain an interior point of the square.

Input

The first line of input contains an integer N denoting the number of lines Dreamoon draw. The i-th line of following N lines contains four integers xi1, yi1, xi2, yi2, denoting that the i-th segment Dreamoon drew is a straight line segment between points (xi1, yi1) and (xi2, yi2).

  • 1 ≤ N ≤ 2 × 103
  • 0 ≤ xi1, yi1, xi2, yi2 ≤ 2 × 103
  • the lengths of all line segments in input are non-zero

Output

Output one integer on a single line: how many grid squares are crossed by at least one of the line segments which Dreamoon drew.

Example

Input
3
0 0 5 5
0 5 5 0
0 5 5 0
Output
9
Input
1
0 0 4 3
Output
6
Input
2
0 0 4 3
1 0 3 3
Output
6

思路:
  直接通过枚举y来判断直线经过哪些方格,vis标记下即可。
  这题好像精度挺重要的。。。。
 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int vis[][];
struct Point
{
double x,y;
};
void solve(void)
{
Point st,se;
scanf("%lf%lf%lf%lf",&st.x,&st.y,&se.x,&se.y);
if(fabs(st.x-se.x)<eps||fabs(st.y-se.y)<eps) return;
if(st.y>se.y) swap(st,se);
double k=(se.x-st.x)/(se.y-st.y),b=st.x-k*st.y;
double tmp;
if(k>)
{
for(int i=se.y-,mx,mi;i>=st.y;i--)
{
tmp=(i+)*k+b;
mi=floor(i*k+b);
if(fabs(tmp-floor(tmp))<=eps)mx=max(,(int)tmp-);
else mx=(int)tmp;
if(mi<) while();
while(mi<=mx)
vis[mi++][i]=;
}
}
else
{
for(int i=st.y+,mx,mi;i<=se.y;i++)
{
tmp=(i-)*k+b;
mi=floor(i*k+b);
if(fabs(tmp-floor(tmp))<=eps)mx=max(,(int)tmp-);
else mx=(int)tmp;
if(mi<) while();
while(mi<=mx)
vis[mi++][i-]=;
}
}
}
int main(void)
{
int n,cnt=;
scanf("%d",&n);
for(int i=;i<=n;i++)
solve();
for(int i=;i<;i++)
for(int j=;j<;j++)
cnt+=vis[i][j];
printf("%d\n",cnt);
return ;
}

2016-2017 National Taiwan University World Final Team Selection Contest C - Crazy Dreamoon的更多相关文章

  1. 2016-2017 National Taiwan University World Final Team Selection Contest

    A. Hacker Cups and Balls 二分答案,将$\geq mid$的数看成$1$,$<mid$的数看成$0$,用线段树进行区间排序检查即可.时间复杂度$O(n\log^2n)$. ...

  2. 2016-2017 National Taiwan University World Final Team Selection Contest (Codeforces Gym) 部分题解

      D 考虑每个点被删除时其他点对它的贡献,然后发现要求出距离为1~k的点对有多少个. 树分治+FFT.分治时把所有点放一起做一遍FFT,然后减去把每棵子树单独做FFT求出来的值. 复杂度$nlog^ ...

  3. 2016-2017 National Taiwan University World Final Team Selection Contest J - Zero Game

    题目: You are given one string S consisting of only '0' and '1'. You are bored, so you start to play w ...

  4. 2016-2017 National Taiwan University World Final Team Selection Contest A - Hacker Cups and Balls

    题目: Dreamoon likes algorithm competitions very much. But when he feels crazy because he cannot figur ...

  5. sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)

    The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里 ...

  6. 【转】2016/2017 Web 开发者路线图

    链接:知乎 [点击查看大图] 原图来自LearnCodeAcademy最火的视频,learncode是YouTube上最火的Web开发教学频道,介绍包括HTML/CSS/JavaScript/Subl ...

  7. Moscow Pre-Finals Workshop 2016. National Taiwan U Selection

    A. As Easy As Possible 每个点往右贪心找最近的点,可以得到一棵树,然后倍增查询即可. 时间复杂度$O((n+m)\log n)$. #include <bits/stdc+ ...

  8. Mindjet MindManager 2016/2017 折腾记录

    https://community.mindjet.com/mindjet/topics/ensure-2017-64-bit-version-installation Mindmanager sho ...

  9. [SinGuLaRiTy] COCI 2016~2017 #5

    [SinGuLaRiTy-1012] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 最近神犇喜欢考COCI...... 测试题目 对于所有的 ...

随机推荐

  1. linux下MySQL与jdk安装

    允许本地访问端口:iptables -P OUTPUT ACCEPT1.yum install mysql 2.yum install mysql-server 3.删除MySQL数据库中user表中 ...

  2. GCC命令基础

    gcc命令使用GNU推出的基于C/C++的编译器,是开放源代码领域应用最广泛的编译器,具有功能强大,编译代码支持性能优化等特点, 现在很多程序员都应用GCC,怎样才能更好的应用GCC. 目前,GCC可 ...

  3. JQuery------图片幻灯片插件

    下载地址: http://www.jq22.com/jquery-info36

  4. linux 命令集合收集(ubuntu)

    1.查看ubuntu版本号:cat /etc/issue  或者sudo lsb_release -a 2.查看内核版本信息:uname -a ,显示为4.4.0 x86_64版本内核

  5. mysql5.5的安装配置

    1.wget http://120.52.72.23/cdn.mysql.com/c3pr90ntc0td//Downloads/MySQL-5.5/mysql-5.5.50-linux2.6-x86 ...

  6. C语言字符串的输入输出

    字符串的输出 在C语言中,输出字符串的函数有两个: puts():直接输出字符串,并且只能输出字符串. printf():通过格式控制符 %s 输出字符串.除了字符串,printf() 还能输出其他类 ...

  7. css做鼠标指向图片图片放大但边框不放大

    这是一个圆形边框做的效果 HTML <div class="circle-wrapper"> <img src="" > </di ...

  8. 第一次广搜!HDU1548--A Strange Lift

    一上来看见题目就用了深搜(因为只会深搜)果断内存超限(据说时间也会超限)无奈只好开始用广搜 其实广搜的思路和深搜有很多类似的地方 不过实现的过程中用到了队列 因此有点难以理解(好吧我个人认为) 这题是 ...

  9. R语言中聚类确定最佳K值之Calinsky criterion

    Calinski-Harabasz准则有时称为方差比准则 (VRC),它可以用来确定聚类的最佳K值.Calinski Harabasz 指数定义为: 其中,K是聚类数,N是样本数,SSB是组与组之间的 ...

  10. 20165330 2017-2018-2《Java程序设计》课程总结

    20165330 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:做中学learning by doing个人感想及学习基础 ...