(困难) CF 484E Sign on Fence,整体二分+线段树
Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence of n panels of 1 meter width and of arbitrary height. The i-th panel's height is hi meters. The adjacent planks follow without a gap between them.
After Bizon painted the fence he decided to put a "for sale" sign on it. The sign will be drawn on a rectangular piece of paper and placed on the fence so that the sides of the sign are parallel to the fence panels and are also aligned with the edges of some panels. Bizon the Champion introduced the following constraints for the sign position:
- The width of the sign should be exactly w meters.
- The sign must fit into the segment of the fence from the l-th to the r-th panels, inclusive (also, it can't exceed the fence's bound in vertical direction).
The sign will be really pretty, So Bizon the Champion wants the sign's height to be as large as possible.
You are given the description of the fence and several queries for placing sign. For each query print the maximum possible height of the sign that can be placed on the corresponding segment of the fence with the given fixed width of the sign.
纪念人生第一道Div 1的E题,终于搞定了。
题目就是N个值的M次询问,然后对于一个询问来说就是二分答案。对于M次询问的话,想到了整体二分。然后数据结构的话就是区间合并的线段树,比较经典的问题。
代码如下:
// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2016年01月22日 星期五 17时06分36秒
// File Name : L_1.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=<<; struct OPE { int l,r,w,id; } ope[MaxN],to1[MaxN],to2[MaxN]; int N,M;
int ans[MaxN]; /////////////////////////////// #define lc (po<<1)
#define rc ((po<<1)|1)
#define lson L,M,lc
#define rson M+1,R,rc struct ANS {
int num,lnum,rnum;
ANS(int a=,int b=,int c=):num(a),lnum(b),rnum(c) {}
}; ANS BIT[MaxN<<]; ANS merge(int len1,int len2,ANS a1,ANS a2) {
ANS ret;
ret.num=max(max(a1.rnum+a2.lnum,a1.num),a2.num);
ret.lnum=(len1==a1.num) ? len1+a2.lnum : a1.lnum;
ret.rnum=(len2==a2.num) ? len2+a1.rnum : a2.rnum;
return ret;
} inline void pushUP(int len1,int len2,int po) {
BIT[po]=merge(len1,len2,BIT[lc],BIT[rc]);
} void update(int up,int ut,int L,int R,int po) {
if(L==R) {
BIT[po].num=BIT[po].lnum=BIT[po].rnum=ut;
return;
} int M=(L+R)>>;
if(up<=M) update(up,ut,lson);
else update(up,ut,rson); pushUP(M-L+,R-M,po);
} ANS query(int ql,int qr,int L,int R,int po) {
if(ql<=L && qr>=R) return BIT[po]; int M=(L+R)>>;
if(ql>M) return query(ql,qr,rson);
if(qr<=M) return query(ql,qr,lson); return merge(M-ql+,qr-M,query(ql,M,lson),query(M+,qr,rson)); // !!!
} /////////////////////////////// int tmp[MaxN]; void getans(int ql,int qr,int L,int R) {
if(ql>qr) return;
if(L==R) {
for(int i=ql;i<=qr;++i) ans[ope[i].id]=L;
return;
} int M=(L+R+)>>;
for(int i=ql;i<=qr;++i)
if(!ope[i].id && ope[i].w>=M)
update(ope[i].l,,,N,),tmp[i]=;
else if(ope[i].id && ope[i].w<=query(ope[i].l,ope[i].r,,N,).num)
tmp[i]=;
else tmp[i]=; int cou1=,cou2=;
for(int i=ql;i<=qr;++i)
if(tmp[i]) {
to2[cou2++]=ope[i];
if(tmp[i]==) update(ope[i].l,,,N,);
}
else to1[cou1++]=ope[i]; int t=ql;
for(int i=;i<cou1;++i) ope[t++]=to1[i];
for(int i=;i<cou2;++i) ope[t++]=to2[i]; getans(ql+cou1,qr,M,R); // !!! 大的那些要被用到,在小的里面。 for(int i=ql+cou1;i<=qr;++i)
if(!ope[i].id)
update(ope[i].l,,,N,); getans(ql,ql+cou1-,L,M-);
} /////////////////////////////// int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int t,cou=;
scanf("%d",&N);
for(int i=;i<=N;++i) {
scanf("%d",&t);
ope[++cou].w=t;
ope[cou].l=i;
ope[cou].id=;
}
scanf("%d",&M);
for(int i=;i<=M;++i) {
ope[++cou].id=i;
scanf("%d %d %d",&ope[cou].l,&ope[cou].r,&ope[cou].w);
}
getans(,cou,,);
for(int i=;i<=M;++i) printf("%d\n",ans[i]); return ;
}
(困难) CF 484E Sign on Fence,整体二分+线段树的更多相关文章
- 【BZOJ-3110】K大数查询 整体二分 + 线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6265 Solved: 2060[Submit][Sta ...
- CF 484E - Sign on Fence
E. Sign on Fence time limit per test 4 seconds memory limit per test 256 megabytes input standard in ...
- 【LUOGU???】WD与地图 整体二分 线段树合并
题目大意 有一个简单有向图.每个点有点权. 有三种操作: 修改点权 删除一条边 询问和某个点在同一个强连通分量中的点的前 \(k\) 大点权和. \(n\leq 100000,m,q\leq 2000 ...
- P5163-WD与地图【tarjan,整体二分,线段树合并】
正题 题目链接:https://www.luogu.com.cn/problem/P5163 题目大意 给出\(n\)个点\(m\)条有向边,点有权值,要求支持操作 删除一条边 修改一个点的权值 求一 ...
- BZOJ 3110 [ZJOI2013]K大数查询 (整体二分+线段树)
和dynamic rankings这道题的思想一样 只不过是把树状数组换成线段树区间修改,求第$K$大的而不是第$K$小的 这道题还有负数,需要离散 #include <vector> # ...
- BZOJ3110 K大数查询 【线段树 + 整体二分 或 树套树(非正解)】
Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
- 【BZOJ4009】[HNOI2015]接水果 DFS序+整体二分+扫描线+树状数组
[BZOJ4009][HNOI2015]接水果 Description 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果.由于她已经DT FC 了The big black, ...
- BZOJ 4009: [HNOI2015]接水果 (整体二分+扫描线 树状数组)
整体二分+扫描线 树状数组 具体做法看这里a CODE #include <cctype> #include <cstdio> #include <cstring> ...
随机推荐
- hdu_4714_Tree2cycle(树形DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意:给你N个点N-1条边,形成一个树,让你拆树,并连接成一个环,每拆一次,连接一次,消耗1,问 ...
- Entity Framework技巧系列之十二 - Tip 46 - 50
提示46. 怎样使用Code-Only排除一个属性 这次是一个真正简单的问题,由StackOverflow上这个问题引出. 问题: 当我们使用Code-Only把一个类的信息告诉Entity F ...
- 【伪一周小结(没错我一周就做了这么点微小的工作)】HDOJ-1241 Oil Deposits 初次AC粗糙版对比代码框架重构版
2016 11月最后一周 这一周复习了一下目前大概了解的唯一算法--深度优先搜索算法(DFS).关于各种细节的处理还是极为不熟练,根据题意判断是否还原标记也无法轻松得出结论.不得不说,距离一个准ACM ...
- Spring Security-用户密码自定义加密
public class SunPasswordEncoder implements PasswordEncoder{ //@实现加密的方法,既将明文转换为密文的方法 public String en ...
- Linux启动流程详解【转载】
在BIOS阶段,计算机的行为基本上被写死了,可以做的事情并不多:一般就是通电.BIOS.主引导记录.操作系统这四步.所以我们一般认为加载内核是linux启动流程的第一步. 第一步.加载内核 操作系统接 ...
- C陷阱与缺陷 第一章
1. 使用 e1=e2的赋值方式 作为 条件语句内部的判断,请使用显示的判断 不使用: if( x =y ) foo(); 而使用: ) foo(); 2. 注意编码规范,一定要在赋值号 “=”两边, ...
- xib storyboard
initWithNibName加载xib或者storyboard BLEViewController *controller = [[BLEViewController alloc] initWith ...
- Inno Setup入门(九)——修改安装过程中的文字显示
前面说到过可以使用不用的语言文件实现不同的显示方式,方便与国际接轨,事实上即使没有语言文件也可以实现修改.[Messages]段用于定义安装程序和卸载程序中显示的消息.一般不需要创建 [Message ...
- 非root启动80端口
Linux非root用户如何使用80端口启动程序 默认情况下Linux的1024以下端口是只有root用户才有权限占用,我们的tomcat,apache,nginx等等程序如果想要用普通用户来占用 ...
- CocoaAsyncSocket框架的简单封装
在iOS开发中使用socket(CFNetwork),一般都是用第三方库AsyncSocket. 参考博客:http://my.oschina.net/worldligang/blog/396881? ...