题目:
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. 分页技巧__设计分页用的QueryHelper辅助对象

    分页技巧__设计分页用的QueryHelper辅助对象 FROM 必须 实体名 WHERE  可选 条件1 AND 条件2 AND ... 条件n ORDER BY 可选 属性1, 属性2, ... ...

  2. Docker(1)在CentOS上的安装与卸载

     一. Docker的安装 CentOS7 上安装: 1. 卸载旧版本 $ sudo yum remove docker \ docker-client \ docker-client-latest ...

  3. android硬件返回

    1.第一种 @Override    public boolean onKeyUp(int keyCode, KeyEvent event) {        //点击回退键        if(Ke ...

  4. index封装

    就是求元素在父级当中的位置: 思路: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  5. powerdesigner 修改name时code不修改

    o

  6. 160308、java排序(形如1.1、1.2.1)

    package com.rick.sample;   import java.util.ArrayList; import java.util.Collections; import java.uti ...

  7. PhoneGap在Mac环境下使用问题小结

    PhoneGap在Mac环境下使用问题小结1,问题一:在执行命令phonegap run ios出现“Cordova needs ios-sim version 1.7 or greater, you ...

  8. Android 中各种权限深入体验及详解

    Android 中各种权限深入体验及详解 分类: Android2012-07-15 19:27 2822人阅读 评论(0) 收藏 举报 androidpermissionsinstallersyst ...

  9. Gartner提出的7种多租户模型

    下面,我们就来看看在SaaS应用搭建过程中,可以采用什么样的多租户模型.从而能较为清晰地了解未来使用PaaS平台开发的SaaS,可以为用户提供哪些多租户的服务.        Gartner提出了7种 ...

  10. 剑指Offer——数字在排序数组中出现的次数

    题目描述: 统计一个数字在排序数组中出现的次数. 分析: 二分变形.二分查找最左边和最右边k的位置,然后相减加一就是结果. 代码: class Solution { public: int GetNu ...