CF 484E - Sign on Fence
4 seconds
256 megabytes
standard input
standard output
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.
The first line of the input contains integer n — the number of panels in the fence (1 ≤ n ≤ 105).
The second line contains n space-separated integers hi, — the heights of the panels (1 ≤ hi ≤ 109).
The third line contains an integer m — the number of the queries (1 ≤ m ≤ 105).
The next m lines contain the descriptions of the queries, each query is represented by three integers l, r and w (1 ≤ l ≤ r ≤ n,1 ≤ w ≤ r - l + 1) — the segment of the fence and the width of the sign respectively.
For each query print the answer on a separate line — the maximum height of the sign that can be put in the corresponding segment of the fence with all the conditions being satisfied.
5
1 2 2 3 3
3
2 5 3
2 5 2
1 5 5
2
3
1
The fence described in the sample looks as follows:

The possible positions for the signs for all queries are given below.
The optimal position of the sign for the first query.
The optimal position of the sign for the second query.
CF上的一道题,给你若干个高度不一的木板,高度为h1...hn,回答m次询问,每次询问(l,r,w)表示l...r区间内,用宽度不小于w的长条覆盖木板,长条的最大高度是多少。
CF给的题解是用函数式线段树,维护各个区间内木板高度的线段树,二分答案然后查询函数式线段树中最大的连续“1”的长度,进行比较。
可以用函数式线段树做的题,一般也可以用整体二分做。相当于一个是在线二分,一个是离线二分。
整体二分的思路是:
二分答案,把高度>mid的木板插入到线段树中,某位置有木板为1,没有为0。
对于询问,统计最大的连续“1”的长度,然后和w比较,长度>=w就往[mid+1,r]区间划分,<w就往[l,mid]区间划分。
这样二分到底层,所有询问的答案也就确定了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#define inf 1000000007
#define maxn 420000
#define maxm 420000 using namespace std; int n,m,tot,cnt;
int a[maxn], ans[maxm]; struct query
{
int id,x,y,z,cur;
query(){}
query(int id,int x,int y,int z,int cur):
id(id),x(x),y(y),z(z),cur(cur){}
}q[maxm],q1[maxm],q2[maxm]; struct segtree
{
int mx[maxn*], lmx[maxn*], rmx[maxn*], size[maxn*];
void update(int x)
{
mx[x]=max(max(mx[x*],mx[x*+]),rmx[x*]+lmx[x*+]);
if (lmx[x*]==size[x*]) lmx[x]=size[x*]+lmx[x*+];
else lmx[x]=lmx[x*];
if (rmx[x*+]==size[x*+]) rmx[x]=size[x*+]+rmx[x*];
else rmx[x]=rmx[x*+];
}
void build(int x,int l,int r)
{
if (l==r)
{
size[x]=;
return ;
}
int mid=(l+r)>>;
build(x*,l,mid);
build(x*+,mid+,r);
size[x]=size[x*]+size[x*+];
}
void add(int x, int l, int r, int pos, int z)
{
if (l==r)
{
mx[x]=lmx[x]=rmx[x]=z;
return ;
}
int mid=(l+r)>>;
if (pos<=mid) add(x*, l, mid, pos, z);
else add(x*+, mid+, r, pos, z);
update(x);
}
int ask(int x, int l, int r, int ll, int rr, int &res)
{
if (ll<=l && r<=rr)
{
int tmp=max(res+lmx[x], mx[x]);
if (rmx[x]==size[x]) res+=size[x];
else res=rmx[x];
return tmp;
}
int mid=(l+r)>>, tmp=;
if (ll<=mid) tmp=max(tmp,ask(x*, l, mid, ll, rr, res));
if (rr>mid) tmp=max(tmp,ask(x*+, mid+, r, ll, rr, res));
return tmp;
}
void init()
{
memset(mx,,sizeof(mx));
memset(lmx,,sizeof(lmx));
memset(rmx,,sizeof(rmx));
}
}tree; void solve(int st,int ed,int l,int r)
{
if (st>ed) return ;
if (l>=r)
{
for (int i=st;i<=ed;i++) ans[q[i].id]=l;
return ;
}
int mid=(l+r)>>, n1=, n2=;
for (int i=st;i<=ed;i++)
{
if (q[i].id==)
{
if (q[i].y>mid)
{
tree.add(, , n, q[i].x, );
q2[n2++]=q[i];
}
else
q1[n1++]=q[i];
}
else
{
int res=;
int tmp=tree.ask(, , n, q[i].x, q[i].y, res);
if (tmp>=q[i].z)
q2[n2++]=q[i];
else
q1[n1++]=q[i];
}
}
for (int i=;i<n1;i++) q[st+i]=q1[i];
for (int i=;i<n2;i++) q[st+n1+i]=q2[i];
solve(st,st+n1-,l,mid);
for (int i=st;i<=ed;i++)
if (q[i].id== && q[i].y>mid) tree.add(, , n, q[i].x, );
solve(st+n1,ed,mid+,r);
} int main()
{
scanf("%d",&n);
tot=;
for (int i=;i<=n;i++)
{
scanf("%d",&a[i]);
q[tot++]=query(,i,a[i],,);
}
scanf("%d",&m);
int x,y,z;
for (int i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
q[tot++]=query(i,x,y,z,);
}
tree.init();
tree.build(,,n);
solve(,tot-,,inf);
for (int i=;i<=m;i++) printf("%d\n",ans[i]);
return ;
}
Sign on Fence
CF 484E - Sign on Fence的更多相关文章
- (困难) CF 484E Sign on Fence,整体二分+线段树
Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence o ...
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence
http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...
- AC日记——Sign on Fence Codeforces 484e
E. Sign on Fence time limit per test 4 seconds memory limit per test 256 megabytes input standard in ...
- 【CF484E】Sign on Fence(主席树)
[CF484E]Sign on Fence(主席树) 题面 懒得贴CF了,你们自己都找得到 洛谷 题解 这不就是[TJOI&HEOI 排序]那题的套路吗... 二分一个答案,把大于答案的都变成 ...
- Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树
E. Sign on Fence Bizon the Champion has recently finished painting his wood fence. The fence consi ...
- CF484E Sign on Fence && [国家集训队]middle
CF484E Sign on Fence #include<bits/stdc++.h> #define RG register #define IL inline #define _ 1 ...
- Sign on Fence CodeForces - 484E
http://codeforces.com/problemset/problem/484/E 题意: 给定一个长度为n的数列,有m次询问,询问形如l r k 要你在区间[l,r]内选一个长度为k的区间 ...
- cf B. Color the Fence
http://codeforces.com/contest/349/problem/B 贪心 #include <cstdio> #include <cstring> #inc ...
随机推荐
- 2016huasacm暑假集训训练三 G - 还是畅通工程
题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/G 这题和上一道题差不多,还更简单点,直接用prim算法就行,直接贴AC代码: im ...
- 第一章-第十一题(请问 “软件” 和 “软件工程” 这些词汇是如何出现的 - 何时、何地、何人)--By 侯伟婷
从邹欣老师的<构建执法:现代软件工程>一书中,我们得到有关这些名词的起源的信息是软件工程的概念是1968年第一次提出的[1].而在一篇专访Margaret Hamilton的报道中,我们通 ...
- Unity3D的四种坐标系
[Unity3D的四种坐标系] 1.World Space(世界坐标):我们在场景中添加物体(如:Cube),他们都是以世界坐标显示在场景中的.transform.position可以获得该位置坐标. ...
- iOS版本更新的App提交审核发布流程
http://www.2cto.com/kf/201502/378698.html 版本更新的App和新App的发布提交流程略有不同,新的App需要在开发者账号里准备发布证书,添加App的id,关联描 ...
- servlet中service() 和doGet() 、doPost() 学习笔记
Sevlet接口定义如下: 与Sevlet接口相关的结构图: service() 方法是 Servlet 的核心.每当一个客户请求一个HttpServlet 对象,该对象的service() 方法就要 ...
- C#测试运行时间
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); //开始监视代码运行时间 ...
- openfire 用户密码加密解密
1.openfire采用的加密方法 Blowfish.java /** * $RCSfile$ * $Revision: 3657 $ * $Date: 2002-09-09 08:31:31 -07 ...
- (java oracle)以bean和array为参数的存储过程及dao部分代码
一.数据库部分 1.创建bean对象 CREATE OR REPLACE TYPE "QUARTZJOBBEAN" as object ( -- Author : Duwc -- ...
- ios10 safari 的坑!
| 导语 ios10 的safari,又给前端开发者挖坑了..测试验证此问题只出现在ios10 safari中.想早点知道结论的,可以直接看最后一个结论~因为,解决过程不重要! 个人原创,未经允许,禁 ...
- 浅析py-faster-rcnn中不同版本caffe的安装及其对应不同版本cudnn的解决方案
浅析py-faster-rcnn中不同版本caffe的安装及其对应不同版本cudnn的解决方案 本文是截止目前为止最强攻略,按照本文方法基本可以无压力应对caffe和Ross B. Girshick的 ...