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. hibernate_Restrictions用法

    方法   说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge &g ...

  2. 【emWin】例程八:绘制位图

    实验指导书及代码包下载: 链接:http://pan.baidu.com/s/1bpeMYpp 密码:wgtp 实验现象:

  3. 【emWin】例程五:显示数值

    实验指导书及代码包下载: 链接:http://pan.baidu.com/s/1pLexsAf密码:p0jf 实验现象:

  4. Git系列教程二 基础介绍

    一.存储方式 如果让我们设计一个版本控制系统,最简单的方式就是每做一次更改就生成一个新的文件. 这样的方式太占用空间,所以传统的版本控制系统都是保存一个文件的某个版本的全部内容以及其他版本相对于这个版 ...

  5. mysql延迟查询, 覆盖索引使用例子

    引用自 'mysql高性能' 5.3.6章节 不能使用覆盖索引的情况 :  解决办法 : 

  6. selenium python 安装

    环境为Win64位系统,默认已经安装python2.7到D:\Python27,此次使用的浏览器为chrome 下面是selenium的安装和chromedriver.exe的下载 1.安装selen ...

  7. Ubuntu16.04安装docker

    1.检查内核版本uname -r,如果是16.04则可以跳过这一步,因为docker只能运行在64-bit linux 内核3.10或高于3.10的系统中. 2.更新apt sources sudo  ...

  8. avalon的表单验证

    表单验证 avalon内置了强大的表单验证功能,它需要结合ms-duplex, ms-validate, ms-rules这个三个指令一起使用. ms-duplex负责监控每个表单元素的输入. ms- ...

  9. 【转载】如何破解受保护的excel密码

    [工具] 1.电脑一台(安装有Microsoft Excel) 2.受保护excel一个 [步骤] 1.首先,打开受保护的Excel表格,按"ALT"+"F11" ...

  10. Java语言程序设计(基础篇) 第四章 数学函数、字符和字符串

    第四章 数学函数.字符和字符串 4.2 常用数学函数 方法分三类:三角函数方法(trigonometric method).指数函数方法(exponent method)和服务方法(service m ...