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 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
3
0 0 5 5
0 5 5 0
0 5 5 0
9
1
0 0 4 3
6
2
0 0 4 3
1 0 3 3
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的更多相关文章
- 2016-2017 National Taiwan University World Final Team Selection Contest
A. Hacker Cups and Balls 二分答案,将$\geq mid$的数看成$1$,$<mid$的数看成$0$,用线段树进行区间排序检查即可.时间复杂度$O(n\log^2n)$. ...
- 2016-2017 National Taiwan University World Final Team Selection Contest (Codeforces Gym) 部分题解
D 考虑每个点被删除时其他点对它的贡献,然后发现要求出距离为1~k的点对有多少个. 树分治+FFT.分治时把所有点放一起做一遍FFT,然后减去把每棵子树单独做FFT求出来的值. 复杂度$nlog^ ...
- 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 ...
- 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 ...
- sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)
The Android University ACM Team Selection Contest Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里 ...
- 【转】2016/2017 Web 开发者路线图
链接:知乎 [点击查看大图] 原图来自LearnCodeAcademy最火的视频,learncode是YouTube上最火的Web开发教学频道,介绍包括HTML/CSS/JavaScript/Subl ...
- Moscow Pre-Finals Workshop 2016. National Taiwan U Selection
A. As Easy As Possible 每个点往右贪心找最近的点,可以得到一棵树,然后倍增查询即可. 时间复杂度$O((n+m)\log n)$. #include <bits/stdc+ ...
- Mindjet MindManager 2016/2017 折腾记录
https://community.mindjet.com/mindjet/topics/ensure-2017-64-bit-version-installation Mindmanager sho ...
- [SinGuLaRiTy] COCI 2016~2017 #5
[SinGuLaRiTy-1012] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 最近神犇喜欢考COCI...... 测试题目 对于所有的 ...
随机推荐
- WPF 在TextBox失去焦点时检测数据,出错重新获得焦点解决办法
WPF 在TextBox失去焦点时检测数据,出错重新获得焦点解决办法 在WPF的TextBox的LostFocus事件中直接使用Focus()方法会出现死循环的问题 正确的使用方式有2中方法: 方法一 ...
- python3----练习......
# 上行遍历 soup = BeautifulSoup(demo, 'html.parser') for parent in soup.a.parents: if parent is None: pr ...
- iOS开发之-- textview 光标起始位置偏移
使用textview的时候,会发生光标偏移的情况,其实是因为iOS7里导航栏,状态栏等有个边缘延伸的效果在. 把边缘延伸关掉就好了.代码如下 //取消iOS7的边缘延伸效果(例如导航栏,状态栏等等) ...
- 说说M451例程之PWM的寄存器讲解
M451提供了两路PWM发生器.每路PWM支持6通道PWM输出或输入捕捉.有一个12位的预分频器把时钟源分频后输入给16位的计数器,另外还有一个16位的比较器.PWM计数器支持向上,向下,上下计数方式 ...
- 面试题思考:Java RMI与RPC,JMS的比较
RPC:(Remote Procedure Call) 被设计为在应用程序间通信的平台中立的方式,它不理会操作系统之间以及语言之间的差异. 支持多语言 RMI:(Remote Method Invo ...
- 写一个SingleTon,(饿最终、懒同步)
1.饿汉式: public class SingleTon { private SingleTon(){ } private final static SingleTon instance = new ...
- webpack配置(一)
这里再配置的时候走了些弯路,现在,把配置前的准备工作做好很重要: 首先,安装node.js,当然,npm也就有了: 其次,安装xampp,主要是为了配置Apache: 安装好后,xampp---htd ...
- FluentNhibernate 不支持存储过程
一直以为没有使用FN进行存储过程的操作,这次因为后台首页想统计下数据,就利用了存储过程,但在使用中却发现FN目前还不支持存储过程(点击查看官方),没有办法,只能利用Fluent Configurati ...
- 【BZOJ2783】[JLOI2012]树 DFS+栈+队列
[BZOJ2783][JLOI2012]树 Description 在这个问题中,给定一个值S和一棵树.在树的每个节点有一个正整数,问有多少条路径的节点总和达到S.路径中节点的深度必须是升序的.假设节 ...
- HTML代码大全
1.html的简介* 什么是html?- HyperText Markup Language:超文本标记语言,网页语言** 超文本:超出文本的范畴,使用html可以轻松实现这样操作** 标记:html ...