题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=6601

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≤105).

The second line contains N integers, the i-th integer ai(1≤ai≤109) 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×105.

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

Hint

题意

给一个长度为N的数组,Q个询问,(l, r)区间内任三个数能构成的三角形的最大周长

题解:

对于排序好的数组,若想要构成三角形周长最大,肯定从最大的边开始取,且三条边是连续的,也就是先取第一大第二大第三大,若不能构成三角形则取第二大第三大第四大,依次取下去。

未排序的数组可以用主席数查询第K大,对于每次询问最多只要查询四十多次,因为若要构造出不能构成三角形的数组,最优构造策略是斐波那契数列,1,2,3,5,8,11,19,到四十多项就超过1e9了。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int mx = 1e5+5;
typedef long long ll; int a[mx], root[mx], cnt;
vector <int> v;
struct node {
int l, r, sum;
}T[mx*40]; int getid(int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
} void update(int l, int r, int &x, int y, int pos) {
T[++cnt] = T[y]; T[cnt].sum++; x = cnt;
if (l == r) return;
int mid = (l+r) / 2;
if (mid >= pos) update(l, mid, T[x].l, T[y].l, pos);
else update(mid+1, r, T[x].r, T[y].r, pos);
} int query(int l, int r, int x, int y, int k) {
if (l == r) return l;
int mid = (l+r) / 2;
int sum = T[T[y].l].sum - T[T[x].l].sum;
if (sum >= k) return query(l, mid, T[x].l, T[y].l, k);
else return query(mid+1, r, T[x].r, T[y].r, k-sum);
} int main() {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
v.clear(); cnt = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
v.push_back(a[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= n; i++) update(1, n, root[i], root[i-1], getid(a[i]));
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
bool flag = false;
int len = y - x + 1;
for (int j = 1; j <= y-x-1; j++) {
ll a = v[query(1, n, root[x-1], root[y], len-j+1)-1];
ll b = v[query(1, n, root[x-1], root[y], len-(j+1)+1)-1];
ll c = v[query(1, n, root[x-1], root[y], len-(j+2)+1)-1];
if (b+c > a) {
flag = true;
printf("%lld\n", a+b+c);
break;
}
}
if (!flag) puts("-1");
}
}
return 0;
}

hdu-6601 Keen On Everything But Triangle的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 2018 Multi-University Training Contest 1 Triangle Partition 【YY】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6300 Triangle Partition Time Limit: 2000/1000 MS (Java ...

  6. 2019 Multi-University Training Contest 2 - 1011 - Keen On Everything But Triangle - 线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=6601 首先要贪心地想,题目要最长的边长,那么要怎么构造呢?在一段连续的区间里面,一定是拿出最长的三根出来比,这样 ...

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

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

  8. hdu 6601 区间条件极值 - 区间 最大 三角形周长

    题目传送门//res tp hdu 目的 对长度为n的区间,给定q个子区间,求其元素能构成三角形的最大周长.有多组测试. n 1e5 q 1e5 ai [1,1e9] (i∈[1,n]); 数据结构 ...

  9. 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 ...

随机推荐

  1. Java匹马行天下之JavaWeb核心技术——JSP(续一)

      十二.JSP表单处理 我们在浏览网页的时候,经常需要向服务器提交信息,并让后台程序处理.浏览器中使用 GET 和 POST 方法向服务器提交数据. GET 方法 GET方法将请求的编码信息添加在网 ...

  2. 现代c++与模板元编程

    最近在重温<c++程序设计新思维>这本经典著作,感慨颇多.由于成书较早,书中很多元编程的例子使用c++98实现的.而如今c++20即将带着concept,Ranges等新特性一同到来,不得 ...

  3. PID算法 旋转倒立摆与平衡车的区别。此贴后边会更新。

    我做PID算法的背景和经历:本人之前电子信息科学与技术专业,对控制方向颇感兴趣,刚上大学时听到实验室老师说PID算法,那年在暑假集训准备全国电子设计竞赛,我正在练习做一个以前专科的题目,帆板角度控制系 ...

  4. win10应用商店卸载后重装教程

    方法一 先进这个链接   http://go.microsoft.com/fwlink/?LinkId=619547  下载一个记事本文件,并且把它保存到你的“下载” 里面. 管理员身份打开Power ...

  5. 搭建nexus私服

    一.安装 1.从网上下载nexus软件https://www.sonatype.com/download-oss-sonatype  下载Nexus Repository Manager OSS软件包 ...

  6. 数字麦克风PDM信号采集与STM32 I2S接口应用

    数字麦克风采用MEMS技术,将声波信号转换为数字采样信号,由单芯片实现采样量化编码,一般而言数字麦克风的输出有PDM麦克风和PCM麦克风,由于PDM麦克风结构.工艺简单而大量应用,在使用中要注意这二者 ...

  7. [Short-Circuit Constraint Violation]警告解决办法

    今天用Altium Designer16画PCB时候遇到一个问题,进行DRC检测,警告如下: [Short-Circuit Constraint Violation] GrayscaleSensor1 ...

  8. luogu1330_封锁阳光大学 图的遍历

    传送门 解释:(转自洛谷题解) 首先,肯定要明确一点,那就是这个图是不一定联通的.于是,我们就可以将整张图切分成许多分开的连同子图来处理.然而最重要的事情是:如何处理一个连通图? 乍看下去,似乎无从下 ...

  9. win10 我的电脑下面的六个文件夹的隐藏

      第一步   第二步     第三步 修改注册表,要隐藏那个文件夹,ThisPCPolicy 改为 "Hide" 修改我的文档的注册表值,使我的文档文件夹隐藏     <w ...

  10. requestAnimationFrame 兼容方案

    [toc] 编写涉及:css, html, js 在线演示codepen html代码 <div class="roll-box"> <div class=&qu ...