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...... 测试题目 对于所有的 ...
随机推荐
- 【python】NLTK好文
From:http://m.blog.csdn.net/blog/huyoo/12188573 nltk是一个python工具包, 用来处理和自然语言处理相关的东西. 包括分词(tokenize), ...
- maven + hessian 简单样例
项目结构例如以下: pom.xml 内容: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&quo ...
- 计算某个目录下所有文件的MD5值
#!/usr/bin/env python #-*- coding:utf-8 -*- ''' 计算某个目录下所有文件的MD5值 ''' import os import sys import has ...
- Spring源码从开始到放弃(一)
参考<Spring技术内幕>分析. github上面有spring的源码(https://github.com/spring-projects/spring-framework) spri ...
- php文件
php文件系统函数: http://www.w3school.com.cn/php/php_ref_filesystem.asp
- 【BZOJ1050】[HAOI2006]旅行comf 并查集
[BZOJ1050][HAOI2006]旅行comf Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<300 ...
- PHP 基础知识代码总结
一.PHP基础语法 变量到数组 <?php //phpinfo(); /* 变量 $a=1;//不分配空间 echo "\$a=".$a; echo "<br ...
- 自动生成项目的Makefile文件
自动生成项目的Makefile文件 理论基础 跟我一起写 Makefile: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4 ...
- kibana5.6源码分析2
1.启动shell脚本:/bin/kibana; js脚本:/src/cli/cli.js; 2.服务端入口:/src/server/kbn_server.js.使用的web框架为hapi.js. ...
- 2017 Multi-University Training Contest - Team 7
HDU6121 Build a tree 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题目意思:一棵 n 个点的完全 k 叉树,结点标号从 ...