http://codeforces.com/contest/484/problem/E

题意:

给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值

第i棵线段树表示>=i的数

维护最长连续子区间

把数从大到小插入主席树

对于每个询问,二分x

在第x棵线段树中查,若最长连续子区间>=w,到代表更大的线段树中查

没有建第n+1棵线段树,导致前面节点的siz不对,WA了一次

#include<cstdio>
#include<iostream>
#include<algorithm> using namespace std; #define N 100001 pair<int,int>a[N]; int tot;
int root[N+],lc[N*],rc[N*]; struct node
{
int siz;
int mx,lmax,rmax; node operator + (node p)
{
node c;
c.siz=siz+p.siz;
c.lmax=lmax;
if(lmax==siz) c.lmax+=p.lmax;
c.rmax=p.rmax;
if(p.rmax==p.siz) c.rmax+=rmax;
c.mx=max(mx,p.mx);
c.mx=max(c.mx,rmax+p.lmax);
return c;
} }e[N*]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void build(int &k,int l,int r)
{
e[k=++tot].siz=r-l+;;
if(l==r) return;
int mid=l+r>>;
build(lc[k],l,mid);
build(rc[k],mid+,r);
} void insert(int pre,int &k,int l,int r,int pos)
{
k=++tot;
if(l==r)
{
e[k].siz=;
e[k].mx=e[k].lmax=e[k].rmax=;
return;
}
int mid=l+r>>;
if(pos<=mid)
{
rc[k]=rc[pre];
insert(lc[pre],lc[k],l,mid,pos);
}
else
{
lc[k]=lc[pre];
insert(rc[pre],rc[k],mid+,r,pos);
}
e[k]=e[lc[k]]+e[rc[k]];
} node query(int k,int l,int r,int opl,int opr)
{
if(l>=opl && r<=opr) return e[k];
int mid=l+r>>;
if(opr<=mid) return query(lc[k],l,mid,opl,opr);
if(opl>mid) return query(rc[k],mid+,r,opl,opr);
return query(lc[k],l,mid,opl,opr)+query(rc[k],mid+,r,opl,opr);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("my.out","w",stdout);
int n;
read(n);
for(int i=;i<=n;++i)
{
read(a[i].first);
a[i].second=i;
}
sort(a+,a+n+);
build(root[n+],,n);
for(int i=n;i;--i) insert(root[i+],root[i],,n,a[i].second);
int m;
read(m);
int s,t,w;
int l,r,mid;
int ans;
while(m--)
{
read(s); read(t); read(w);
l=,r=n; ans=;
while(l<=r)
{
mid=l+r>>;
if(query(root[mid],,n,s,t).mx>=w) ans=mid,l=mid+;
else r=mid-;
}
cout<<a[ans].first<<'\n';
}
}
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.

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.

CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence的更多相关文章

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

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

  2. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding

    http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...

  3. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

    http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...

  4. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation

    http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...

  5. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik

    http://codeforces.com/contest/351/problem/B 题意: 给出一个n的排列 第一个人任选两个相邻数交换位置 第二个人有一半的概率交换相邻的第一个数>第二个数 ...

  6. CF&&CC百套计划1 Codeforces Round #449 C. Willem, Chtholly and Seniorious (Old Driver Tree)

    http://codeforces.com/problemset/problem/896/C 题意: 对于一个随机序列,执行以下操作: 区间赋值 区间加 区间求第k小 区间求k次幂的和 对于随机序列, ...

  7. CF&&CC百套计划1 Codeforces Round #449 B. Ithea Plays With Chtholly

    http://codeforces.com/contest/896/problem/B 题意: 交互题 n张卡片填m个1到c之间的数,1<=n*ceil(c/2)<=m 最后填出一个单调非 ...

  8. CF&&CC百套计划1 Codeforces Round #449 A. Nephren gives a riddle

    http://codeforces.com/contest/896/problem/A 第i个字符串嵌套第i-1个字符串 求第n个字符串的第k个字母 dfs #include<map> # ...

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

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

随机推荐

  1. Maven构建项目速度太慢的解决办法

    问题描述 通过idea新建maven项目,参数设置好后,idea自动构建maven项目时,速度很慢. 参数设置如图: 执行时间如下图: Total time为8:49,花了将近十分钟时间. 连续尝试了 ...

  2. ReactJS实用技巧(1):JSX与HTML的那些不同

    在项目中使用ReactJS也已经有大半年了,收获很多也踩过不少坑.不想把这个系列写成抄书似的罗列,旨在总结些常用的技巧及常见的坑,以帮助初心者快速入门,想系统学习的同学还是多阅读文档. JSX本质上与 ...

  3. 百度地图API的网页使用

    请看图示(以及参考官方文档): 图片尺寸:1710x822

  4. linux中使sqlplus能够上下翻页

    安装包链接:https://pan.baidu.com/s/1WsQTeEQClM88aEqIvNi2ag 提取码:s241  rlwrap-0.37-1.el6.x86_64.rpm 和 rlwra ...

  5. 用Beyond Compare比较文本时,忽略不重要文本的方法

    Beyond Compare是一款好用的文本比较工具,可以比较纯文本文件.源代码和HTML,Word文档.Adobe和pdf文件.在使用Beyond Compare比较文本文件时,有些不重要的文本差异 ...

  6. selenium+ python自动化--断言assertpy

    前言: 在对登录验证时,不知道为何原因用unittest的断言不成功,就在网上发现这个assertpy,因此做个笔记 准备: pip install assertypy 例子: from assert ...

  7. CentOS 6.8 安装Tomcat7

    一.下载Tomcat到服务器上 将Tomcat包下载到devleoper(没有此目录创建一个)目录下: 二.解压安装包 下载好之后,直接解压,使用命令: .tar.gz # 是否使用sudo权限执行根 ...

  8. kali linux 安装Nessus

    Nessus 介绍: Nessus 是目前全世界最多人使用的系统漏洞扫描与分析软件.总共有超过75,000个机构使用Nessus 作为扫描该机构电脑系统的软件. 下载Nessus,我的是64为,我选择 ...

  9. 详解JavaScript中的Event Loop(事件循环)机制

    前言 我们都知道,javascript从诞生之日起就是一门单线程的非阻塞的脚本语言.这是由其最初的用途来决定的:与浏览器交互. 单线程意味着,javascript代码在执行的任何时候,都只有一个主线程 ...

  10. 2-Thirteenth Scrum Meeting-10151213

    任务安排 成员 今日完成 明日任务 闫昊 获取视频播放进度  用本地数据库记录课程结构和学习进度 唐彬  阅读IOS代码+阅读上届网络核心代码  请假(编译……) 史烨轩 下载service开发    ...