Little Sub and Triples(思维)

题目描述
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;
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值