2019hdu多校 Keen On Everything But Triangle
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的更多相关文章
- 2019杭电多校第二场hdu6601 Keen On Everything But Triangle
Keen On Everything But Triangle 题目传送门 解题思路 利用主席树求区间第k小,先求区间内最大的值,再求第二大,第三大--直到找到连续的三个数可以构成一个三角形.因为对于 ...
- HDU - 6601 Keen On Everything But Triangle 主席树
Keen On Everything But Triangle 感觉最近多校好多主席树的亚子,但是本人菜得很,还没学过主席树,看着队友写题就只能划水,\(WA\)了还不能帮忙\(debug\),所以深 ...
- HDU6578 2019HDU多校训练赛第一场 1001 (dp)
HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...
- HDU6579 2019HDU多校训练赛第一场1002 (线性基)
HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...
- 2019HDU多校第一场1001 BLANK (DP)(HDU6578)
2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...
- [2019杭电多校第二场][hdu6601]Keen On Everything But Triangle
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6601 题意是说用给定区间内的数字组成周长最大的三角形. 大致做法就是求区间第1大,第2大和第3大然后判 ...
- hdu多校第二场1011 (hdu6601) Keen On Everything But Triangle 主席树
题意: 给定一个数列,每次询问一个区间,问这个区间中的值可组成的周长最大的三角形的周长. 题解: 定理1:给定一些值,这些值中组成边长最大的三角形的三条边的大小排名一定是连续的. 证明:假如第k大,第 ...
- 杭电多校HDU 6601 Keen On Everything But Triangle(主席树)题解
题意: 有\(n\)根长度不一的棍子,q次询问,求\([L,R]\)区间的棍子所能组成的周长最长的三角形.棍长\(\in [1, 1e9]\),n\(\in [1, 1e5]\). 思路: 由于不构成 ...
- [2019HDU多校第一场][HDU 6590][M. Code]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6590 题目大意(来自队友):二维平面上有\(n\)个点,每个点要么是黑色要么是白色,问能否找到一条直线 ...
随机推荐
- TCP listener
调试时未关闭之前开启的TCP SERVER: 应该关闭: TcpListener TcpClient 1.开启TCP listener (1)Start()方法失败 异常位置:该异常的产生位置为 tc ...
- JS中的 map, filter, some, every, forEach, for in, for of 用法总结和区别
JS中的 map, filter, some, every, forEach, for in, for of 用法总结和区别 :https://blog.csdn.net/hyupeng1006/a ...
- Virtual DOM和snabbdom.js
Virtual DOM和snabbdom.js:https://www.jianshu.com/p/1f1ef915e83e
- thread 多线程2
###24.04_多线程(多线程程序实现的方式1)(掌握) * 1.继承Thread * 定义类继承Thread * 重写run方法 * 把新线程要做的事写在run方法中 * 创建线程对象 * 开启新 ...
- 19.AutoMapper 之开放式泛型(Open Generics)
https://www.jianshu.com/p/ce4c7e291408 开放式泛型(Open Generics) AutoMapper可以支持开放式泛型的映射.为开放式泛型创建映射: publi ...
- Jquery复习(二)之stop()易忘点
jQuery stop() 方法 jQuery stop() 方法用于停止动画或效果,在它们完成之前. stop() 方法适用于所有 jQuery 效果函数,包括滑动.淡入淡出和自定义动画. 语法:$ ...
- 095、如何创建Swarm集群?(Swarm02)
参考https://www.cnblogs.com/CloudMan6/p/7862254.html 本节我们将创建三节点的swarm集群(操作系统Ubuntu 16.04 ,Docker 版本均 ...
- 使用consul
eureka官方已经正式宣布:自2.0起不再维护该项目,并在github 项目wiki上放出了一段吓唬人的话: 大意就是:从2.x起,官方不会继续开发了,如果需要使用2.x,风险自负.但其实我觉得问题 ...
- 68. Text Justification (JAVA)
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- byteArray转换为double,int
/*将int转为低字节在前,高字节在后的byte数组 b[0] = 11111111(0xff) & 01100001 b[1] = 11111111(0xff) & (n & ...