Keen On Everything But Triangle【线段树+最大、次大、第三大】【2019多校第二场】

Problem 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


  这道题,我们可以想到,对应一个区间的三角形的边,如果我们可以取的话,也就是代表了这三条边一定是连续的,我们可以反证法来证明,如果有一个不连续的话,那么,我们一定可以把它替换掉。

a、b、c、d四条边,已经按照降序排列好了,如果我们取a、b、d,那么是不是a<b+d,但是呢,c>d我们是不是可以换成a<b+c,同样的,我们可以举例头尾之类的情况。

  有了这个,我们就可以去跑了。我们是不是可以不断的取最大的、次大的,然后如果它们不能构成一个三角形,我们继续取第二大、第三大、第四大…… 以此类推即可。

比赛的线段树Code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, Q;
ll a[maxN];
struct node
{
    int id;
    ll val;
    node(int a=0, ll b=0):id(a), val(b) {}
}tree[maxN<<2];
inline void pushup(int rt, int l, int r)
{
    if(tree[lsn].val >= tree[rsn].val) tree[rt] = tree[lsn];
    else tree[rt] = tree[rsn];
}
inline void buildTree(int rt, int l, int r)
{
    if(l == r)
    {
        scanf("%lld", &tree[rt].val);
        a[l] = tree[rt].val;
        tree[rt].id = l;
        return;
    }
    int mid = HalF;
    buildTree(Lson); buildTree(Rson);
    pushup(myself);
}
inline void update(int rt, int l, int r, int qx,long long val)
{
    if(l == r)
    {
        tree[rt].val = val;
        return;
    }
    int mid = HalF;
    if(qx <= mid) update(Lson, qx, val);
    else update(Rson, qx, val);
    pushup(myself);
}
inline node query(int rt, int l, int r, int ql, int qr)
{
    if(ql <= l && qr >= r) return tree[rt];
    int mid = HalF;
    if(qr <= mid) return query(QL);
    else if(ql > mid) return query(QR);
    else
    {
        node TL = query(QL), TR = query(QR);
        if(TL.val >= TR.val) return TL;
        else return TR;
    }
}
int synp[100004];
int cnt;
int main()
{
    while (/*scanf("%d%d", &N, &Q) != EOF*/cin>>N>>Q)
    {
        buildTree(1, 1, N);
        int l, r;
        while (Q--)
        {
            scanf("%d%d", &l, &r);
            node fir;
            cnt=0;
            node L1,R1,L2,R2;
            long long ans = -1;
            while(1){
                fir = query(1, 1, N, l, r);
                if(fir.val==-INF)break;
                L1 = R1 = L2 = R2 = {0,-INF};
                if(fir.id>l) {
                    L1 = query(1,1,N,l,fir.id-1);
                    if(L1.val>0)
                    update(1,1,N,L1.id,-INF);
                }
                if(fir.id<r){
                    R1 = query(1,1,N,fir.id+1,r);
                    if(R1.val>0)update(1,1,N,R1.id,-INF);
                }
                if(fir.id>l) {
                    L2 = query(1,1,N,l,fir.id-1);
                }
                if(fir.id<r) {
                    R2 = query(1,1,N,fir.id+1,r);
                }
                long long res = -1;
                res = max(res,L1.val+L2.val);
                res = max(res,L1.val+R1.val);
                res = max(res,R1.val+R2.val);
                if(res > fir.val ){
                    if(fir.id>l) {
                        if(L1.val>0)
                        update(1,1,N,L1.id,a[L1.id]);
                    }
                    if(fir.id<r) {
                        if(R1.val>0)
                        update(1,1,N,R1.id,a[R1.id]);
                    }
                    ans = res+fir.val;
                    break;
                }
                else {
                    if(L1.val>0 && fir.id>l) {
                        update(1,1,N,L1.id,a[L1.id]);
                    }
                    if(R1.val>0 && fir.id<r) {
                        update(1,1,N,R1.id,a[R1.id]);
                    }
                    update(1,1,N,fir.id,-INF);
                    synp[cnt++] = fir.id;
                }
            }
            printf("%lld\n",ans);
            for (int i = 0; i < cnt; ++i) {
                update(1,1,N,synp[i],a[synp[i]]);
            }
        }
    }
    return 0;
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值