题目描述
Little Sub has learned a new word ’Triple’, which usually means a group with three elements in it.
Now he comes up with an interesting problem for you.
Define
Given an positive integer sequence A and some queries. In each query, you have to tell if there exists
a triple T = (x, y, z) such that:
1.x, y, z are unique integers.
2.l≤x, y, z≤r.
3.Ax≤Ay≤Az .
4.F(Ax, Ay, Az) > 1.
输入
The first line contains two positive integers n, q(1 ≤ n, q ≤ 200000).
The second line contains n positive integers, indicating the integer sequence.
The next q lines describe each query by giving two integer l, r(1≤l≤r≤n).
All given integers will not exceed 231-1.
输出
For each query, print YES if it exists any legal triple or NO otherwise.
样例输入
4 2
1 2 3 4
1 3
2 4
样例输出
NO
YES
思路
由题意可推出只要该区间内存在三条线可组成一个三角形,该条件成立,但如果直接模拟做会T,因此引用斐波那契数列剪枝,如果区间长度大于46(int范围内斐波那契数列的最长序列长度),则该条件直接成立
代码实现
#include<cstdio>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
const int N=2e5+5;
typedef long long ll;
int n,q;
ll a[N];
set<ll> v;
int main()
{
scanf("%d%d",&n,&q);
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
}
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
if(r-l+1>46) printf("YES\n");
else if (r-l+1<=2)
{
printf("NO\n");
}
else
{
v.clear();
bool flag=false;
for(int i=l; i<=r; i++) v.insert(a[i]);
set<ll>:: iterator it;
int a,c,b;
a=b=c=-1;
for(it=v.begin(); it!=v.end(); it++)
{
if(a==-1) a=*it;
else if(b==-1) b=*it;
else
{
if(c!=-1)
{
a=b;
b=c;
}
c=*it;
if(a+b>c)
{
flag=true;
break;
}
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
}
return 0;
}