题目:

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

思路:

  二分答案,check时,把小于等于二分值的数标记为0,大于的标记为1。

  然后排序就是区间内的0和1进行排序,用线段树维护区间0的个数的话,排序就是把一段区间置为0或1。

  最后根据center为是不是为0.

 #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+; struct node
{
int l,r;
}q[K];
int n,m,a[K],sum[K],ff[K];
void push_down(int o,int l,int r)
{
int mid=l+r>>;
if(ff[o]&&sum[o])
sum[o<<]=mid-l+,sum[o<<|]=r-mid;
else if(ff[o])
sum[o<<]=,sum[o<<|]=;
if(ff[o])
ff[o<<]=ff[o<<|]=;
ff[o]=;
}
void build(int o,int l,int r,int v)
{
ff[o]=;
if(l==r)
{
if(a[l]<=v) sum[o]=;
else sum[o]=;
return ;
}
build(o<<,l,l+r>>,v),build(o<<|,(l+r)/+,r,v);
sum[o]=sum[o<<]+sum[o<<|];
}
void update(int o,int l,int r,int nl,int nr,int x)
{
//if(nl>nr) while(1);
if(l==nl&&r==nr)
{
if(x==) sum[o]=r-l+;
else sum[o]=;
ff[o]=;
return ;
}
push_down(o,l,r);
int mid=l+r>>;
if(nr<=mid) update(o<<,l,mid,nl,nr,x);
else if(nl>mid) update(o<<|,mid+,r,nl,nr,x);
else update(o<<,l,mid,nl,mid,x),update(o<<|,mid+,r,mid+,nr,x);
sum[o]=sum[o<<]+sum[o<<|];
}
int query(int o,int l,int r,int nl,int nr)
{
//if(nl>nr) while(1);
if(l==nl&&r==nr) return sum[o];
push_down(o,l,r);
int mid=l+r>>;
if(nr<=mid) return query(o<<,l,mid,nl,nr);
else if(nl>mid) return query(o<<|,mid+,r,nl,nr);
return query(o<<,l,mid,nl,mid)+query(o<<|,mid+,r,mid+,nr);
}
bool check(int x)
{
build(,,n,x);
for(int i=;i<=m;i++)
if(q[i].l<q[i].r)
{
int cnt=query(,,n,q[i].l,q[i].r);
if(cnt)
update(,,n,q[i].l,q[i].l+cnt-,);
if(q[i].l+cnt<=q[i].r)
update(,,n,q[i].l+cnt,q[i].r,);
}
else if(q[i].l>q[i].r)
{
int cnt=q[i].l-q[i].r+-query(,,n,q[i].r,q[i].l);
if(cnt)
update(,,n,q[i].r,q[i].r+cnt-,);
if(q[i].r+cnt<=q[i].l)
update(,,n,q[i].r+cnt,q[i].l,);
}
return query(,,n,(n+)/,(n+)/)==;
} int main(void)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",a+i);
for(int i=;i<=m;i++)
scanf("%d%d",&q[i].l,&q[i].r);
int l=,r=n,ans=;
while(l<=r)
{
int mid=l+r>>;
if(check(mid)) r=mid-,ans=mid;
else l=mid+;
}
printf("%d\n",ans);
return ;
}

2016-2017 National Taiwan University World Final Team Selection Contest A - Hacker Cups and Balls的更多相关文章

  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 C - Crazy Dreamoon

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

  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. [转]Oracle dbms_random函数用法快速生成多条测试数据

    Java 随机生成中文姓名,手机号,邮编,住址:http://blog.csdn.net/xiaokui_wingfly/article/details/45913885 Java 批量随机生成身份证 ...

  2. 移动ChemDraw结构有什么方法

    ChemDraw软件是一款比较常见的化学绘图软件,化学专业的领域的人常常会用到它.本教程主要是针对新手用户,让其了解一些ChemDraw的一些基本操作,以便其能尽快上手早日用到工作中.下面我们就来给大 ...

  3. 织梦dedecms整合discuz论坛的操作方法

    织梦dedecms和discuz论坛整合主要用途,是让两个系统共享用户数据,同一个用户可以在两个网站都可以登录.在我们制作织梦cms模板的时候,有时需要整合discuz里的东细.本文主要讲解一下ded ...

  4. AWS系列-EC2实例选择镜像

    Centos Ubuntu Redhat 打开EC2控制台,点击启动实例,选择AWS Marketplace Centos.org说明为centos官网镜像 如下图,这种镜像是收费的镜像 Ubuntu ...

  5. easyui上次图片

    easyuiForm提交: 前台代码: <form id="importFileForm" method="post" enctype="mul ...

  6. 编程之美 最长递增子序列 LIS

    1. O(N*logN) 解法 先对序列排序, 然后寻找两个序列的最长公共子序列 2. O(N*N) 的动态规划解法 令 LIST[i] 表示以 i 为结尾的最长子序列的长度, 那么 LIST[J] ...

  7. 在scrollview中双击定点放大的代码

    双击放大是 iPhone 的一个基本操作,第三方程序里引入这一功能的话,主要是在 scrollview 呈现一张图片或者 PDF 页面时,双击可以放大,主要代码如下 - (void)scrollVie ...

  8. 【BZOJ2622】[2012国家集训队测试]深入虎穴 次短路

    [BZOJ2622][2012国家集训队测试]深入虎穴 Description 虎是中国传统文化中一个独特的意象.我们既会把老虎的形象用到喜庆的节日装饰画上,也可能把它视作一种邪恶的可怕的动物,例如“ ...

  9. WingIDE6.0神秘代码

    python2: import string import random import sha BASE16 = '0123456789ABCDEF' BASE30 = '123456789ABCDE ...

  10. poj1015 Jury Compromise【背包】

    Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:32355   Accepted:8722   ...