E. 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:

  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.

Examples
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.The optimal position of the sign for the third query.

题意:

  给你n个数,每个数表示一个高度。

  m个询问,每次询问你l,r内连续w个数的最低高度的最大值

  note解释样例很详细

题解:

  主席树的技巧

  按照高度排序,倒着插入每一颗线段树中

  查询的话,二分历史版本线段树的位置,在l,r这段区间内至少存在连续w个位置存在有值,很明显的线段树的区间合并,区间查询了

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e6, mod = 1e9+, inf = 2e9;
int n,root[N],m,l[N*],r[N*],rmx[N*],lmx[N*],mx[N*],sz,v[N*];
pair<int ,int > a[N];
void push_up(int i,int ll,int rr) {
lmx[i] = lmx[l[i]];
if(lmx[i] == mid - ll + ) lmx[i] += lmx[r[i]];
rmx[i] = rmx[r[i]];
if(rmx[i] == rr - mid) rmx[i] += rmx[l[i]];
mx[i] = max(lmx[r[i]]+rmx[l[i]],max(mx[l[i]],mx[r[i]]));
}
void update(int x,int &y,int ll,int rr,int k,int c) {
v[y = ++sz] = v[x] + ;
l[y] = l[x];
r[y] = r[x];
if(ll == rr) {
mx[y] = lmx[y] = rmx[y] = c;
l[y] = ; r[y] = ;
return ;
}
if(k <= mid) update(l[x],l[y],ll,mid,k,c);
else update(r[x],r[y],mid+,rr,k,c);
push_up(y,ll,rr);
}
int query(int i,int ll,int rr,int s,int t) {
if(s > t) return ;
if(s == ll && rr == t) return mx[i];
int ret = ;
if(t <= mid) ret = query(l[i],ll,mid,s,t);
else if(s > mid) ret = query(r[i],mid+,rr,s,t);
else {
ret = max(query(l[i],ll,mid,s,mid),query(r[i],mid+,rr,mid+,t));
int lx = min(rmx[l[i]],mid - s + );
int rx = min(lmx[r[i]],t - mid);
ret = max(ret, lx + rx);
}
return ret;
}
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i) scanf("%d",&a[i].first),a[i].second = i;
sort(a+,a+n+);
for(int i = n; i >= ; --i) update(root[i+],root[i],,n,a[i].second,);
scanf("%d",&m);
for(int i = ; i <= m; ++i) {
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
int l = , r = n, ans = n;
while(l <= r) {
int md = (l+r)>>;
int ss = query(root[md],,n,x,y);
if(ss >= w) l = md+,ans=md;
else r = md - ;
}
printf("%d\n",a[ans].first);
}
return ;
}

Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树的更多相关文章

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

  2. Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)

    链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...

  3. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

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

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

  5. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  6. Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数

    B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...

  7. Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心

    A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...

  8. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  9. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

随机推荐

  1. php调用c/c++的一种方式

    php调用c/c++有很多方式,最常用的是通过tcp或者http去调用,通过发送请求去调用c/c++编写的cgi/fastcgi来实现,另外php还有一种直接执行外部应用程序的方式,这种方式会影响到系 ...

  2. Nginx反向代理到Tomcat服务器

    在实际生产中,Tomcat服务器一般不单独使用在项目中,对于静态资源的响应Nginx表现的比较好,另外由于nginx是专门用于反向代理的服务器,所以很容易实现将java的请求转发到后端交给tomcat ...

  3. Androd核心基础01

    Androd核心基础01包含的主要内容如下 Android版本简介 Android体系结构 JVM和DVM的区别 常见adb命令操作 Android工程目录结构 点击事件的四种形式 电话拨号器Demo ...

  4. Java for LintCode 颠倒整数

    将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 解题思路: JAVA实现如下: public int reverseInteger(int n) { Boole ...

  5. Django环境搭建

    1.安装好Python 2.7.10 2.下载解压Django Django-1.9.2.tar.gz cmd cd到解压缩目录(***) python setup.py install 3.检测是否 ...

  6. FIX_前缀后缀_未提交

    问题 B: FIX 时间限制: 1 Sec  内存限制: 64 MB提交: 38  解决: 11[提交][状态][讨论版] 题目描述 如果单词 X 由单词 Y 的前若干个字母构成,我们称 X 是 Y ...

  7. poj 1102.LC-Display 解题报告

    题目链接:http://poj.org/problem?id=1102 题目意思:就是根据给出的格式 s 和 数字 n,输出数值 n 的 LCD 显示.数值 n 的每个数字要占据 s + 2 列 和 ...

  8. C# 串口操作系列(2) -- 入门篇,为什么我的串口程序在关闭串口时候会死锁 ?

    第一篇文章我相信很多人不看都能做的出来,但是,用过微软SerialPort类的人,都遇到过这个尴尬,关闭串口的时候会让软件死锁.天哪,我可不是武断,算了.不要太绝对了.99.9%的人吧,都遇到过这个问 ...

  9. Android 录音

    想要实现wav格式的编码时我们也就不能再使用MediaRecorder,而只能使用AudioRecord进行处理

  10. poj2492(种类并查集/各种解法)

    题目链接: http://poj.org/problem?id=2492 题意: 有t组测试数据, 对于每组数据,第一行n, m分别表示昆虫的数目和接下来m行x, y, x, y表示教授判断x, y为 ...