题目:
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 ;
}

最新文章

  1. 【腾讯优测干货分享】如何降低App的待机内存(四)——进阶:内存原理
  2. 数据结构与算法之链表-javascript实现
  3. 【传递智慧】C++基础班公开课第六期培训
  4. C语言中strdup函数使用方法
  5. boost 无锁队列
  6. Zookeeper 4、Zookeeper开发
  7. gcc manual
  8. 【Win10】刷新DNS缓存
  9. Tcpdump安装使用
  10. 1、java的数据类型
  11. unity中使用www的库读取数据里面的数据
  12. servlet之ServletRequest与ServletResponse (三)
  13. swoole之代码热更新实现 转自https://blog.csdn.net/nep_tune/article/details/81329918
  14. Storm实现实时大数据分析(storm介绍,与Hadoop比较,)
  15. wait()和sleep()的区别
  16. linux,mac安装sentry
  17. teradata 字符串数据合并 在concat()函数无法使用的情况下
  18. 2018 Multi-University Training Contest 9 Solution
  19. 转: oracle中schema指的是什么?
  20. Android-获取网络图片设置壁纸

热门文章

  1. django admin后台css样式丢失
  2. 剑指 offer set 20 打印出和为 s 的连续正序序列
  3. HttpModule,HttpContext,HttpHandler
  4. python基础之3
  5. python裁剪base64编码的图片
  6. Date 日期格式化
  7. 170421、maven自定义变量及属性
  8. CH5E01 乌龟棋【线性DP】
  9. 修改Linux的基本配置(如主机名、ip等)
  10. JavaScript中的原型与原型链