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. java并发编程(二)线程中断

    参考:http://blog.csdn.net/ns_code/article/details/17091267 使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thre ...

  2. 二、jquery选择器

    在jquery库中,可以通过选择器实现DOM元素快捷选择这一重要的核心功能. 1.选择器的优势 (1)代码更简单 由于在jquery库中,封装了大量可以通过选择器直接调用的方法或函数,使编写代码更加简 ...

  3. fnc.tld学习编写

    使用 el 的过程中,需要使用到后端代码处理逻辑,这个时候我们就需要自定义 方法. 如我们后端代码定义如下: package com.rhythmk.common; public class FncH ...

  4. 字符串转数字_atoi_stringstream

    一.#include <cstdlib> 字符串转换到整型数,函数原型:int atoi(const char *nptr) 注意事项:有符号整型,能转换的最大字符串是:"214 ...

  5. Slave作为其它Slave的Master时使用

    主从配置需要注意的点 (1)主从服务器操作系统版本和位数一致: (2) Master和Slave数据库的版本要一致: (3) Master和Slave数据库中的数据要一致: (4) Master开启二 ...

  6. 【整理】强化学习与MDP

    [入门,来自wiki] 强化学习是机器学习中的一个领域,强调如何基于环境而行动,以取得最大化的预期利益.其灵感来源于心理学中的行为主义理论,即有机体如何在环境给予的奖励或惩罚的刺激下,逐步形成对刺激的 ...

  7. final 评论 I

    新蜂团队:俄罗斯方块界面设计给人眼前一亮的感觉,很喜欢他们界面的颜色搭配.功能上实现了俄罗斯方块的基本功能,并且没有bug.最重要的是游戏有自己的创新点在里面,很喜欢游戏的瞄准功能.总的来说项目完成的 ...

  8. PHP preg_replace使用例子

    将 qwer://xxx/545/0 替换为 qwer://trading_system_xxx/0/545 $str = '<a href="qwer://xxx/545/0&quo ...

  9. 安装VS 2015完成后,VS2012 打开报错

    安装VS 2015完成后,VS2012 打开报错 打开VS2012Web项目,弹出错误提示: asp.net 4.0 has not been registered on the web server ...

  10. 源码阅读 etherum-block.py

    def calc_difficulty(parent, timestamp): config = parent.config offset = parent.difficulty // config[ ...