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
  1. 5
    1 2 2 3 3
    3
    2 5 3
    2 5 2
    1 5 5
output
  1. 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个位置存在有值,很明显的线段树的区间合并,区间查询了

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #pragma comment(linker, "/STACK:102400000,102400000")
  4. #define ls i<<1
  5. #define rs ls | 1
  6. #define mid ((ll+rr)>>1)
  7. #define pii pair<int,int>
  8. #define MP make_pair
  9. typedef long long LL;
  10. const long long INF = 1e18;
  11. const double Pi = acos(-1.0);
  12. const int N = 1e5+, M = 1e6, mod = 1e9+, inf = 2e9;
  13. int n,root[N],m,l[N*],r[N*],rmx[N*],lmx[N*],mx[N*],sz,v[N*];
  14. pair<int ,int > a[N];
  15. void push_up(int i,int ll,int rr) {
  16. lmx[i] = lmx[l[i]];
  17. if(lmx[i] == mid - ll + ) lmx[i] += lmx[r[i]];
  18. rmx[i] = rmx[r[i]];
  19. if(rmx[i] == rr - mid) rmx[i] += rmx[l[i]];
  20. mx[i] = max(lmx[r[i]]+rmx[l[i]],max(mx[l[i]],mx[r[i]]));
  21. }
  22. void update(int x,int &y,int ll,int rr,int k,int c) {
  23. v[y = ++sz] = v[x] + ;
  24. l[y] = l[x];
  25. r[y] = r[x];
  26. if(ll == rr) {
  27. mx[y] = lmx[y] = rmx[y] = c;
  28. l[y] = ; r[y] = ;
  29. return ;
  30. }
  31. if(k <= mid) update(l[x],l[y],ll,mid,k,c);
  32. else update(r[x],r[y],mid+,rr,k,c);
  33. push_up(y,ll,rr);
  34. }
  35. int query(int i,int ll,int rr,int s,int t) {
  36. if(s > t) return ;
  37. if(s == ll && rr == t) return mx[i];
  38. int ret = ;
  39. if(t <= mid) ret = query(l[i],ll,mid,s,t);
  40. else if(s > mid) ret = query(r[i],mid+,rr,s,t);
  41. else {
  42. ret = max(query(l[i],ll,mid,s,mid),query(r[i],mid+,rr,mid+,t));
  43. int lx = min(rmx[l[i]],mid - s + );
  44. int rx = min(lmx[r[i]],t - mid);
  45. ret = max(ret, lx + rx);
  46. }
  47. return ret;
  48. }
  49. int main() {
  50. scanf("%d",&n);
  51. for(int i = ; i <= n; ++i) scanf("%d",&a[i].first),a[i].second = i;
  52. sort(a+,a+n+);
  53. for(int i = n; i >= ; --i) update(root[i+],root[i],,n,a[i].second,);
  54. scanf("%d",&m);
  55. for(int i = ; i <= m; ++i) {
  56. int x,y,w;
  57. scanf("%d%d%d",&x,&y,&w);
  58. int l = , r = n, ans = n;
  59. while(l <= r) {
  60. int md = (l+r)>>;
  61. int ss = query(root[md],,n,x,y);
  62. if(ss >= w) l = md+,ans=md;
  63. else r = md - ;
  64. }
  65. printf("%d\n",a[ans].first);
  66. }
  67. return ;
  68. }

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. sruts2 自定义类型转换器

    1.1.1    Struts2中自定义类型转换器:(了解) 类型转换的过程是双向的过程: JSP---->Action参数提交:String---Date. Action---->JSP ...

  2. android studio Error:java.lang.OutOfMemoryError: GC overhead limit exceeded

    android studio Error:java.lang.OutOfMemoryError: GC overhead limit exceeded 在app下的build.gradle中找到and ...

  3. poj4052

    题意:求一个文章(长度5.1e6)里面出现了多少个指定的模式串.重复出现只记一次.而且如果两个模式串都出现的情况下,一个是另一个的子串,则该子串不算出现过. 分析:AC自动机. 由于子串不算所以加一些 ...

  4. perl 判断数组相等的三种方法

    1.数组相等,数组成员相同,位置也相同 一般的如果判断@array1 等于 @array2 a.数组长度相同 $#array1=$#array2, 比较数组长度,不能使用length函数,length ...

  5. 上传文件报错System.Net.ProtocolViolationException: 必须先将 ContentLength 字节写入请求流,然后再调用 [Begin]GetResponse。

    在上传文件的时候报错. 错误: System.Net.ProtocolViolationException: 必须先将 ContentLength 字节写入请求流,然后再调用 [Begin]GetRe ...

  6. 【python】An Introduction to Interactive Programming in Python(week two)

    This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.e ...

  7. 地图API文档

    目录 腾讯地图API 2 1.API概览... 2 1.1 WebService API(官网注明是beta版本,可能不稳定,慎用):... 2 1.2 URL API:... 2 1.3 静态图AP ...

  8. -[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000000000e3

    网络数据解析出现-[__NSCFNumber length]: unrecognized selector sent to instance 0xb0000000000000e3这样的错误,具体 re ...

  9. IOS 去掉导航栏(UINavigationBar)下方的横线

    这是导航栏的问题,将下边的代码放在  viewWillAppear  方法中就可以实现效果: - (void)viewWillAppear:(BOOL)animated{ // Called when ...

  10. 解决客户端访问https报错

    现象: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure at com.sun.net.ssl. ...