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.
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.
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.
5 3 1 3 1 3 1 1 3 2 4 1 5
-1 2 3 4 1 3 5
9 3 3 4 5 3 4 7 3 4 8 1 3 4 6 7 9
1 2 3 -1 -1
5 2 1 2 3 4 5 1 1 2 3
-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;
}