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
    评论
Subject: Application for the Position of Food Science Professor Dear Hiring Manager, I am writing to express my interest in the position of Food Science Professor at your esteemed university. With a PhD in Food Science and over five years of teaching experience, I am confident in my ability to make a valuable contribution to your institution. My passion for teaching and research in the field of food science has been the driving force behind my career. I have worked as a lecturer at XYZ University, where I developed and taught courses on food processing, food microbiology, and food safety, among others. Additionally, my research focuses on the development of novel food products and the improvement of existing ones. As a professor, I aim to inspire and motivate my students to pursue their passions and reach their full potential. I believe that teaching should be interactive and engaging, and I use a variety of teaching methods to suit different learning styles. Furthermore, I am committed to fostering a collaborative and inclusive learning environment where students feel comfortable asking questions and sharing their ideas. I am also excited about the opportunity to contribute to your university's research endeavors. I bring a strong publication record and have secured several grants for my research projects. I am keen to collaborate with other faculty members and engage in interdisciplinary research that addresses important global challenges. Thank you for considering my application. I have attached my resume and academic transcripts for your review. I look forward to the opportunity to discuss my qualifications and experience in further detail. Sincerely, [Your Name]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wuliwuliii

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

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

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

打赏作者

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

抵扣说明:

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

余额充值