题目:

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. iOS开发之--当遇到tableView整体上移时的解决方案

    方案一在使用了navigationController后,当界面进行跳转往返后,时而会出现tableView上移的情况,通常会自动上移64个像素,那么这种情况,我们可以关闭tableView的自动适配 ...

  2. C语言条件运算符

    如果希望获得两个数中最大的一个,可以使用 if 语句,例如: if(a>b){ max = a; }else{ max = b; } 不过,C语言提供了一种更加简单的方法,叫做条件运算符,语法格 ...

  3. 【BZOJ3881】[Coci2015]Divljak fail树+树链的并

    [BZOJ3881][Coci2015]Divljak Description Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操 ...

  4. LeetCode 笔记系列五 Generate Parentheses

    题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  5. 利用jsPerf优化Web应用的性能

    在前端开发的过程中,掌握好浏览器的特性进行有针对性的性能调优是一项基本工作,jsperf.com是一个用来发布基于HTML的针对性能比较的测试用例的网站,你可以在jsPerf上在线填写和运行测试用例, ...

  6. Angular2+学习第2篇 cli 环境搭建过程

    Angular-cli是angular团队针对Angular2提供的脚手架,用于环境搭建,运行等:具体参考Angular-cli GitHub Angular的启动过程,需要先回答三个问题: 启动时加 ...

  7. 管理源代码的工具SVN与GIT

    如何看待源代码 源代码是公司的重要资产 对应软件公司来说,源代码相当于固定资产>人才 所以源代码管理对于公司来说是最重要的事物之一 一.管理源代码的工具 SVN:集中式的源代码管理工具,通常必须 ...

  8. sevlet实现反盗链

    有时候为了网站的版权和安全问题,我们需要为我们的网站应用设置防盗链,这样可以保证我们网站的一些资源的安全性.防盗链的主要是通过获取http的请求头referer的信息来和我们的网站地址做对比,如果相同 ...

  9. hbctf---whatiscanary学习

    题目中除了能栈溢出实在找不到其他能泄露信息的地方了.而且也没法修改GOT表,始终绕不过stack_chk_fail函数.感到无从下手.只到官方给WP了,才觉得自己基础太过浅薄了. 如果我们仔细观察崩溃 ...

  10. nsq小试牛刀-0.3.0 API变更

    NSQ是由知名短链接服务商bitly用Go语言开发的实时消息处理系统,具有高性能.高可靠.无视单点故障等优点,是一个非常不错的新兴的消息队列解决方案. nsg易于配置和部署,所有参考都通过命令行指定, ...