E. Sign on Fence
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  1. The width of the sign should be exactly w meters.
  2. 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.

Input

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 lr and w (1 ≤ l ≤ r ≤ n,1 ≤ w ≤ r - l + 1) — the segment of the fence and the width of the sign respectively.

Output

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.

Sample test(s)
input
5
1 2 2 3 3
3
2 5 3
2 5 2
1 5 5
output
2
3
1
Note

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的更多相关文章

  1. (困难) CF 484E Sign on Fence,整体二分+线段树

    Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence o ...

  2. Codeforces 484E Sign on Fence(是持久的段树+二分法)

    题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...

  3. 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的数 维护 ...

  4. 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 ...

  5. 【CF484E】Sign on Fence(主席树)

    [CF484E]Sign on Fence(主席树) 题面 懒得贴CF了,你们自己都找得到 洛谷 题解 这不就是[TJOI&HEOI 排序]那题的套路吗... 二分一个答案,把大于答案的都变成 ...

  6. 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 ...

  7. CF484E Sign on Fence && [国家集训队]middle

    CF484E Sign on Fence #include<bits/stdc++.h> #define RG register #define IL inline #define _ 1 ...

  8. Sign on Fence CodeForces - 484E

    http://codeforces.com/problemset/problem/484/E 题意: 给定一个长度为n的数列,有m次询问,询问形如l r k 要你在区间[l,r]内选一个长度为k的区间 ...

  9. cf B. Color the Fence

    http://codeforces.com/contest/349/problem/B 贪心 #include <cstdio> #include <cstring> #inc ...

随机推荐

  1. HTML5的全新语义化元素

    1.<section> <section>元素用来定义文档或应用程序中的区域(或节).例如,可以用它组织你的个人信息,一个<section>用于联系信息,另一个用于 ...

  2. 返回数据方法DeaCacheCommand,由CRL自动实现

    越来越多的人学起了前端,或许部分的初衷仅是它简单易上手以及好找工作,毕竟几年前只会个html和css就能有工作,悄悄告诉泥萌,这也是博主一年前的初衷 还好numpy, scikit-learn都提供了 ...

  3. 在MVC架构中使用CodeSmith生成NHibernate映射对象和实体类

    第一步:找到生成模板,如下图 第二步:配置数据库连接(如下图),然后右击第一步找到的模板,点击Excute 第三步:执行操做(如下图) 第四步: 找到之前配置生成的文件夹,找到如下文件(图中标记的文件 ...

  4. PHP 实现“贴吧神兽”验证码

    最早看到 “贴吧神兽” 验证码是在百度贴吧,吧主防止挖坟贴,放出了究极神兽验证码 例如: 地址:http://tieba.baidu.com/p/3320323440 可以用 PHP + JavaSc ...

  5. Angular2 起步(1)

    1.安装 nodejs(最新稳定版)https://nodejs.org/en/ 安装 typescript npm install -g typescript 安装 angular-cli(关于CL ...

  6. Elong App 性能测试分享

    个人简介: 测试老鸟,曾做过6年的测试以及2年的大数据开发:曾就职于伟景行.高德(大数据开发):钟情于钻研开源测试框架:目前挂单于艺龙. 有对本主题感兴趣的同学,可以加我Q私信(305285925): ...

  7. Matlab中常用机器学习函数

    更多内容请参考http://cn.mathworks.com/help/stats/index.html?s_cid=doc_ftr. Naive Bayes(朴素贝叶斯) Factor = Naiv ...

  8. Linux 基础命令-CURL 表单上传文件

    CURL -F, --form <name=content> (HTTP) This lets curl emulate a filled-in form in which a user ...

  9. 什么是ValueStack

    转载自:http://www.cnblogs.com/zyw-205520/archive/2012/09/12/2681346.html Strut2的Action类通过属性可以获得所有相关的值,如 ...

  10. java.util.concurrent.atomic 包详解

    Atomic包的作用: 方便程序员在多线程环境下,无锁的进行原子操作 Atomic包核心: Atomic包里的类基本都是使用Unsafe实现的包装类,核心操作是CAS原子操作 关于CAS compar ...