2019杭电多校第二场hdu6601-Keen On Everything But Triangle(主席树)

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

主席树模板题,直接上代码
参考代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<string>
#include<cstring>
#include<time.h>
#include<stdlib.h>

using namespace std;
typedef long long ll;
typedef pair<int, int>P;

const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int MAX_N = 100000+5;
const int S=(1<<18)+MAX_N*50;

struct node{
    int k,id;
};

int n,q;
node data[MAX_N+1];
int ii[MAX_N+1];
int tree[MAX_N+1];
int tot;
int kat[S],lc[S],rc[S];

bool cmp(node A,node B){
    return A.k<B.k;
}

int init(int l=1,int r=n){
    int t=++tot;
    kat[t]=0;lc[t]=rc[t]=0;
    if(l!=r){
        lc[t]=init(l,(l+r)/2);
        rc[t]=init((l+r)/2+1,r);
    }
    return t;
}

void build(int k,int pos){
    int last=tree[k-1];
    tree[k]=++tot;
    kat[tot]=kat[last]+1;
    int l=1,r=n;
    while(lc[last]||rc[last]){
        if(pos<=(l+r)/2){
            lc[tot]=tot+1;
            rc[tot]=rc[last];
            kat[++tot]=kat[lc[last]]+1;
            last=lc[last];
            r=(l+r)/2;
        }
        else{
            lc[tot]=lc[last];
            rc[tot]=tot+1;
            kat[++tot]=kat[rc[last]]+1;
            last=rc[last];
            l=(l+r)/2+1;
        }
    }
    lc[tot]=rc[tot]=0;
}

int query(int k,int a,int b,int l=1,int r=n){
    if(l==r)return l;
    int ans=kat[lc[b]]-kat[lc[a]];
    if(ans>=k)return query(k,lc[a],lc[b],l,(l+r)/2);
    return query(k-ans,rc[a],rc[b],(l+r)/2+1,r);
}

ll get(int l,int r,int k){
    return data[query(r-l+2-k,tree[l-1],tree[r])].k;
}

void solve(int l,int r){
    if(r-l<2){
        printf("-1\n");
        return ;
    }
    ll a,b=get(l,r,1),c=get(l,r,2);
    for(int i=3;i<=r-l+1;i++){
        a=b;b=c;c=get(l,r,i);
        if(a<b+c){
            printf("%lld\n",a+b+c);
            return ;
        }
    }
    printf("-1\n");
}

int main(){
    while(scanf("%d%d",&n,&q)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&data[i].k);
            data[i].id=i;
        }
        sort(data+1,data+n+1,cmp);
        for(int i=1;i<=n;i++)ii[data[i].id]=i;
        tot=0;
        tree[0]=init();
        for(int i=1;i<=n;i++){
            build(i,ii[i]);
        }
        while(q--){
            int l,r;
            scanf("%d%d",&l,&r);
            solve(l,r);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值