【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树
【BZOJ3958】[WF2011]Mummy Madness
Description
.jpg)
Input
Output
Sample Input
-3 5
3 4
-6 -2
1 -5
1
0 -1
-1
Sample Output
Case 2: never
HINT
题解:容易想到二分答案mid,因为如果在mid时刻能抓到以后也一直是抓到的。在mid时刻,人可能到达的位置是一个正方形,每个木乃伊能到达的位置也是正方形,我们可以发现如果在mid时刻被抓了,当且仅当木乃伊的移动位置将人的移动位置完全覆盖了。所以我们只需要判断若干个矩形的并是否能覆盖给定矩形即可,用扫描线+线段树可以实现。
但是本人从来没写过矩形并啊,这里学习一发:
考虑标记永久化,我们对于线段树上的每个节点维护cnt:区间中被覆盖的位置个数,sum:当前区间被覆盖了几层。其中cnt相当于值,可以pushup,而sum相当于标记,但不能pushdown。如果一个区间的sum>0,我们令cnt=r-l+1;否则cnt=cnt[lson]+cnt[rson]。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=100010;
int n,tot;
struct node
{
int l,r,x,k;
node() {}
node(int a,int b,int c,int d) {l=a,r=b,x=c,k=d;}
}p[maxn<<1];
int x[maxn],y[maxn],s1[maxn*80],s[maxn*80];
bool tc[maxn*80];
bool cmp(const node &a,const node &b)
{
return a.x<b.x;
}
void updata(int l,int r,int x,int a,int b,int c)
{
if(l!=r&&tc[x]) tc[lson]=tc[rson]=1,s1[lson]=s1[rson]=s[lson]=s[rson]=tc[x]=0;
if(a<=l&&r<=b)
{
s[x]+=c;
if(s[x]) s1[x]=r-l+1;
else if(l!=r) s1[x]=s1[lson]+s1[rson];
else s1[x]=0;
return ;
}
int mid=(l+r)>>1;
if(a<=mid) updata(l,mid,lson,a,b,c);
if(b>mid) updata(mid+1,r,rson,a,b,c);
if(s[x]) s1[x]=r-l+1;
else if(l!=r) s1[x]=s1[lson]+s1[rson];
else s1[x]=0;
}
inline bool check(int mid)
{
int i;
tot=0;
for(i=1;i<=n;i++)
{
if(x[i]+mid<-mid||x[i]-mid>mid||y[i]+mid<-mid||y[i]-mid>mid) continue;
p[++tot]=node(max(-mid,y[i]-mid),min(mid,y[i]+mid),max(-mid,x[i]-mid),1);
p[++tot]=node(max(-mid,y[i]-mid),min(mid,y[i]+mid),min(mid+1,x[i]+mid+1),-1);
}
sort(p+1,p+tot+1,cmp);
tc[1]=1,s[1]=s1[1]=0;
p[0].x=-mid,p[tot+1]=node(0,0,mid+1,0);
for(i=1;i<=tot+1;i++)
{
if(p[i].x>p[i-1].x&&s1[1]<2*mid+1) return 1;
updata(-mid,mid,1,p[i].l,p[i].r,p[i].k);
}
return 0;
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
return ret*f;
}
int main()
{
for(int cas=1;;cas++)
{
n=rd();
if(n==-1) return 0;
int i,l=0,r=1000001,mid;
for(i=1;i<=n;i++) x[i]=rd(),y[i]=rd();
while(l<r)
{
mid=(l+r)>>1;
if(check(mid)) l=mid+1;
else r=mid;
}
printf("Case %d: ",cas);
if(l==1000001) printf("never\n");
else printf("%d\n",l);
}
}//4 -3 5 3 4 -6 -2 1 -5 1 0 -1 -1
【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树的更多相关文章
- 【BZOJ 3958】 3958: [WF2011]Mummy Madness (二分+扫描线、线段树)
3958: [WF2011]Mummy Madness Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 96 Solved: 41 Descripti ...
- HDU 3642 - Get The Treasury - [加强版扫描线+线段树]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- [BZOJ4552][TJOI2016&&HEOI2016]排序(二分答案+线段树/线段树分裂与合并)
解法一:二分答案+线段树 首先我们知道,对于一个01序列排序,用线段树维护的话可以做到单次排序复杂度仅为log级别. 这道题只有一个询问,所以离线没有意义,而一个询问让我们很自然的想到二分答案.先二分 ...
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...
- 【bzoj4491】我也不知道题目名字是什么 离线扫描线+线段树
题目描述 给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串 输入 第一行n,表示A数组有多少元素接下来一行为n个整数A[i]接下来一个整数Q,表示询问数 ...
- hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积
题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...
- P3722 [AH2017/HNOI2017]影魔(单调栈+扫描线+线段树)
题面传送门 首先我们把这两个贡献翻译成人话: 区间 \([l,r]\) 产生 \(p_1\) 的贡献当且仅当 \(a_l,a_r\) 分别为区间 \([l,r]\) 的最大值和次大值. 区间 \([l ...
- poj1151 Atlantis——扫描线+线段树
题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...
- Codeforces 460C 二分结果+线段树维护
发现最近碰到好多次二分结果的题目,上次多校也是,被我很机智的快速过了,这个思想确实非常不错.在正面求比较难处理的时候,二分结果再判断是否有效往往柳暗花明. 这个题目给定n个数字的序列,可以操作m次,每 ...
随机推荐
- The declared package "com.dao" does not match the expected package "src.com.dao"
今天把项目代码上传到svn后出现例如以下错误:The declared package "com.dao" does not match the expected package ...
- (转)使用AfxGetMainWnd函数的一个心得
作者:朱金灿 来源:http://blog.csdn.net/clever101/ 使用AfxGetMainWnd函数获取MFC程序中的主框架类指针是一个常用作法.但是你会发现这一做法有时也会失灵.不 ...
- 使用Grunt构建任务管理脚本(转)
Grunt是构建Web开发的一个系统,但它创建比较困难.在这个指南中,你将学会如何配置Grunt创建一个现代的Web项目.当你完成教程中的配置之后,你的Gruntfile将具有: 从源目录中向目标目录 ...
- android中文api(79)——Gallery
前言 本章内容是 android.widget.Gallery,版本为Android 2.3 r1,翻译来自"henly.zhang",欢迎大家访问他的博客:http://www. ...
- 学习:List的扁平化 和 拼接
一.list_to_binary/1的参数:iolist类型的. 二.lists:concat(Things) -> string() Types: Things = [Thing] Thing ...
- jquery validate自定义规则
//检查身份证号码是否存在 $.validator.addMethod("checkIDCardExist", function (value, element) { if ($( ...
- Java基础--常用IO流使用实例
读取文本文件: private static void ioDemo1() { try { FileReader fileReader = new FileReader("C:\\Users ...
- Android学习笔记——Menu(三)
知识点 今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习. Popup Menu(弹出式菜单) 弹出式菜单是一种固定在View上的菜单模型.主要用于以下三种情况: 为特定的内容提 ...
- EasyTouch的使用官方文档操作步骤
对于移动平台上的RPG类的游戏,我们常用虚拟摇杆来控制人物角色的行走和一些行为,相信我们对它并不陌生,之前尝试了EasyTouch2.5,发现并没有最新版的3.1好用,2.5版本的对于自适应没有做的很 ...
- linux下 redis 启动
启动文件 startredis.sh : nohup /data/redis/bin/redis-server /data/redis/etc/redis.conf & 关闭文件 stopr ...