Gym - 101733D_Triangle Construction

Statements

Little Andrey wants to work in IT. He already knows that he should exercise in mathematics and algorithms.

Andrey likes to play with a set of wooden sticks of various lengths. Experimentally he found that three sticks can form a triangle, if the length of the longest of them is strictly less than the sum of two others.

A sequence of n sticks is located on the table. Andrey chooses some interval with indices from l to r and looks for three sticks which can form a triangle.

Help Andrew, for each of the q intervals find three sticks such that he can make a triangle.

Input

The first input line contains two integers n and q (1 ≤ n, q ≤ 300 000), number of sticks and number of intervals. The second line contains n integers li (1 ≤ li ≤ 1018), the array of sticks length in the order they lie on the table.

Each of the next q lines contains two integers l and r (1 ≤ l ≤ r ≤ n), the interval boundaries.

Output

For each interval print any three different indices i j k (l ≤ i, j, k ≤ r) such that sticks with lengths li lj lk make a triangle. If there are no such indices, output the only number  - 1.

Example
Input
5 3
1 3 1 3 1
1 3
2 4
1 5
Output
-1
2 3 4
1 3 5
Input
9 3
3 4 5 3 4 7 3 4 8
1 3
4 6
7 9
Output
1 2 3
-1
-1
Input
5 2
1 2 3 4 5
1 1
2 3
Output
-1
-1

题意:

指定的区间内是否有构成三角形的边,有输出索引,没有-1;

bug:

输入斐波那契数列,GG了

加个特判,r-l>88, (88是看题解知道的,真的是大佬才想得到,服)


代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<map>
typedef long long ll;
using namespace std;
int n,q;
int l,r;
struct pair1{
ll p1;
int p2;
} a[300005],b[300005];
int c,d; 
int e[3];


bool cmp(pair1 a,pair1 b)
{
return a.p1<b.p1;
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i].p1);
a[i].p2=i;
}
while(q--)
{
scanf("%d %d",&l,&r);
if(r-l<2)
{
printf("-1\n");
continue;

if(r-l>88)
{
r=l+88;
}
for(int j=0;j<=r-l;j++)
{
b[j].p1=a[l+j].p1;
b[j].p2=a[l+j].p2;
}
sort(b,b+r-l+1,cmp);
c=0;
for(int j=0;j<=r-l-2;j++)
{
if(b[j].p1+b[j+1].p1>b[j+2].p1){
c=1;
d=j;
break;
}
}
if(c)
{
printf("%d %d %d\n",b[d].p2,b[d+1].p2,b[d+2].p2);
}
else
{
printf("-1\n");
}
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值