题目:

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. 【python】NLTK好文

    From:http://m.blog.csdn.net/blog/huyoo/12188573 nltk是一个python工具包, 用来处理和自然语言处理相关的东西. 包括分词(tokenize), ...

  2. JQuery------实现点击左右按钮,切换图片功能

    如图: 代码: html @*商品主图片*@ <div class="product-img" style="display:table-cell;width:40 ...

  3. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

  4. Linux中使用SecureCRT上传、下载文件命令sz与rz用法实例

    来自:http://www.jb51.net/LINUXjishu/163820.html 其中,对于sz和rz的理解与记忆我用了如下的方法(因为很多时候容易搞混):sz中的s意为send(发送),告 ...

  5. java 实现对指定目录的文件进行下载

    @RequestMapping("/exportDocument") @ResponseBody public void exportDocument(HttpServletReq ...

  6. Delphi 编译/链接过程

     

  7. SSH电力项目一 搭建Hibernate框架

    Hibernate所需要的基本文件: ElectText.java ElecText.hbm.xml hibernate.cfg.xml 第一步:创建测试表Elec_Text: create tabl ...

  8. 解决<pre>标签里的文本换行(兼容IE, FF和Opera等)

      我们都知道<pre> 标签可定义预格式化的文本,一个常见应用就是用来表示计算机的源代码.被包围在 pre 元素中的文本通常会保留空格和换行符,但不幸的是,当你在<pre>标 ...

  9. 170425、centos安装mysql5.6数据库

    # rpm -qa | grep mysql ## 查看该操作系统上是否已经安装了 mysql 数据库, 有的话,可以通过 rpm -e 命令 或者 rpm -e --nodeps 命令来卸载掉 # ...

  10. 170417、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    前言:互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服 ...