Problem Description

N sticks are arranged in a row, and their lengths are a1,a2,...,aN.

There are Q querys. For i-th of them, you can only use sticks between li-th to ri-th. Please output the maximum circumference of all the triangles that you can make with these sticks, or print −1 denoting no triangles you can make.

Input

There are multiple test cases.

Each case starts with a line containing two positive integers N,Q(N,Q≤1e5).

The second line contains N integers, the i-th integer ai(1≤ai≤1e9) of them showing the length of the i-th stick.

Then follow Q lines. i-th of them contains two integers li,ri(1≤li≤ri≤N), meaning that you can only use sticks between li-th to ri-th.

It is guaranteed that the sum of Ns and the sum of Qs in all test cases are both no larger than 4×1e5.

Output

For each test case, output Q lines, each containing an integer denoting the maximum circumference.

Sample Input

5 3
2 5 6 5 2
1 3
2 4
2 5

Sample Output

13
16
16

Solution:

可以发现,无法组成三角形的最劣条件便是斐波那契数列,而在本题数列最多到44项(ai<=1e9)

于是我们便可以用主席树来维护区间第K大,然后暴力询问就OK了

Code:

#include<cstdio>
#include<ctype.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
const int N=1e5+1;
int n,q,cnt,a[N],b[N],rt[N];
struct SegTree{
int tot,ls[N*40],rs[N*40],sz[N*40];
void clear(){
tot=0;
memset(ls,0,sizeof(ls));
memset(rs,0,sizeof(rs));
memset(rt,0,sizeof(sz));
}
void build(int &q,int l,int r){
q=++tot;sz[q]=0;
if(l==r) return ;
int mid=l+r>>1;
build(ls[q],l,mid);
build(rs[q],mid+1,r);
}
void ins(int &q,int lst,int l,int r,int x){
q=++tot;
ls[q]=ls[lst],rs[q]=rs[lst];
sz[q]=sz[lst]+1;
if(l==r) return ;
int mid=l+r>>1;
if(mid>=x) ins(ls[q],ls[lst],l,mid,x);
else ins(rs[q],rs[lst],mid+1,r,x);
}
int query(int q,int p,int l,int r,int k){
if(l==r) return b[l];
int v=sz[ls[q]]-sz[ls[p]];
int mid=l+r>>1;
if(v>=k) return query(ls[q],ls[p],l,mid,k);
else return query(rs[q],rs[p],mid+1,r,k-v);
}
}T;
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
void solve(){T.clear();
for(int i=1;i<=n;i++)
a[i]=read(),b[i]=a[i];
sort(b+1,b+n+1);
cnt=unique(b+1,b+n+1)-b-1;
T.build(rt[0],1,cnt);
for(int i=1;i<=n;i++){
int v=lower_bound(b+1,b+cnt+1,a[i])-b;
T.ins(rt[i],rt[i-1],1,cnt,v);
}
for(int i=1;i<=q;i++){
int l=read(),r=read(),u=r-l+1,flag=0;
if(u<=2){puts("-1");continue;}
int a=T.query(rt[r],rt[l-1],1,cnt,u);
int b=T.query(rt[r],rt[l-1],1,cnt,--u);
while(u){
int v=T.query(rt[r],rt[l-1],1,cnt,--u);
if(v+b>a){
printf("%lld\n",a+b+v);
flag=1;break;
}a=b,b=v;
}if(!flag) puts("-1");
}
}
signed main(){
while(~scanf("%lld%lld",&n,&q)) solve();
return 0;
}

2019hdu多校 Keen On Everything But Triangle的更多相关文章

  1. 2019杭电多校第二场hdu6601 Keen On Everything But Triangle

    Keen On Everything But Triangle 题目传送门 解题思路 利用主席树求区间第k小,先求区间内最大的值,再求第二大,第三大--直到找到连续的三个数可以构成一个三角形.因为对于 ...

  2. HDU - 6601 Keen On Everything But Triangle 主席树

    Keen On Everything But Triangle 感觉最近多校好多主席树的亚子,但是本人菜得很,还没学过主席树,看着队友写题就只能划水,\(WA\)了还不能帮忙\(debug\),所以深 ...

  3. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  4. HDU6579 2019HDU多校训练赛第一场1002 (线性基)

    HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...

  5. 2019HDU多校第一场1001 BLANK (DP)(HDU6578)

    2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...

  6. [2019杭电多校第二场][hdu6601]Keen On Everything But Triangle

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6601 题意是说用给定区间内的数字组成周长最大的三角形. 大致做法就是求区间第1大,第2大和第3大然后判 ...

  7. hdu多校第二场1011 (hdu6601) Keen On Everything But Triangle 主席树

    题意: 给定一个数列,每次询问一个区间,问这个区间中的值可组成的周长最大的三角形的周长. 题解: 定理1:给定一些值,这些值中组成边长最大的三角形的三条边的大小排名一定是连续的. 证明:假如第k大,第 ...

  8. 杭电多校HDU 6601 Keen On Everything But Triangle(主席树)题解

    题意: 有\(n\)根长度不一的棍子,q次询问,求\([L,R]\)区间的棍子所能组成的周长最长的三角形.棍长\(\in [1, 1e9]\),n\(\in [1, 1e5]\). 思路: 由于不构成 ...

  9. [2019HDU多校第一场][HDU 6590][M. Code]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6590 题目大意(来自队友):二维平面上有\(n\)个点,每个点要么是黑色要么是白色,问能否找到一条直线 ...

随机推荐

  1. Vue --》this.$set()的神奇用法

    作为一名开发者,我们都知道: data中数据,都是响应式.也就是说,如果操作data中的数据,视图会实时更新: 但在实际开发中,遇到过一个坑:若data中数据类型为对象,方法methods中改变对象的 ...

  2. 微信小程序request请求封装,验签

    1/ 公共文件util添加 request请求 //简单封装请求 function request(params, path, isShowLoading = true, goBack = false ...

  3. mysql——多表——子查询——示例

    子查询: 子查询是将一个查询语句嵌套在另外一个查询语句中,内层查询语句的查询结果,可以作为外来层查询语句提供查询条件. 因此在特定条件下,一个查询语句的条件,需要另外一个查询语句来获取. 前期准备表: ...

  4. spark 常用设置

    1.spark.hadoop.validateOutputSpecs 若设置为true,saveAsHadoopFile会验证输出目录是否存在.虽然设为false可直接覆盖文件路径

  5. Java 读取application.properties配置文件中配置

    实际开发中若需要读取配置文件application.properties中的配置,代码如下.例:读取配置文件中name属性配置值: 代码如下: import org.springframework.c ...

  6. Hive开发中使用变量的两种方法

    在使用hive开发数据分析代码时,经常会遇到需要改变运行参数的情况,比如select语句中对日期字段值的设定,可能不同时间想要看不同日期的数据,这就需要能动态改变日期的值.如果开发量较大.参数多的话, ...

  7. Python数据基础类型-列表

    1,列表的创建 list1 = ['hello', 'world', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", &quo ...

  8. Javaweb实训-宠物医院-社区宠物医院的页面样式

    /* CSS Document */      /*        对于CSS来说  每一个元素默认的margin和padding就是0px.但是不同的浏览器会有一个默认的浏览器样式修改默认的marg ...

  9. Django设置 DEBUG=False后静态文件无法加载

    修改setting.py STATIC_URL = '/static/' STATIC_ROOT = 'static' ## 新增行 STATICFILES_DIRS = [ os.path.join ...

  10. tar/gzip/zip文件打包、压缩命令

    一.tar打包备份工具 1.命令功能 tar 将多个文件或目录打包在一起,可用通过调用gzip或zip实现压缩.解压的命令:tar不仅可以多多个文件进行打包,还可以对多个文件打包后进行压缩. 2.语法 ...