zoukankan      html  css  js  c++  java
  • Taotao Picks Apples

    st表+二分

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    const int maxn = 100100;
    int n,m,t;
    int a[maxn];
    int st[maxn][20];
    int lg[maxn];
    int cnt[maxn];
    int cnt2[maxn];
    inline int getmax(int l,int r){
        if(l>r)return 0;
        if(r>n)return 2e9;
        int len=lg[r-l+1];
        return std::max(st[l][len],st[r-(1<<len)+1][len]);
    }
    int main(){
        std::ios::sync_with_stdio(false),std::cin.tie(0);
        std::cin >> t;
        lg[0]=-1;
        for(int i=1;i<maxn;++i)lg[i]=lg[i>>1]+1;
        while(t--){
            std::cin >> n >> m;
            memset(st,0,sizeof st);
            memset(cnt,0,sizeof cnt);
            memset(cnt2,0,sizeof cnt2);
            for(int i=1;i<=n;++i)std::cin >> a[i],st[i][0]=a[i];
            for(int i=1;i<20;++i)
                for(int j=1;j+(1<<i-1)<=n;++j)
                    st[j][i]=std::max(st[j][i-1],st[j+(1<<i-1)][i-1]);
            cnt[1]=1;
            for(int i=2,now=a[1];i<=n;++i){
                cnt[i]=cnt[i-1];
                if(a[i] > now) ++cnt[i],now=a[i];
            }
            cnt2[n]=1;
            for(int i=n-1;i;--i){
                if(getmax(i+1,n) <= a[i]){
                    cnt2[i]=1;
                }else{
                    int l=i+1,r=n;
                    while(l!=r){
                        int mid=l+r>>1;
                        if(getmax(i+1,mid) <= a[i])l=mid+1;
                        else r=mid;
                    }
                    cnt2[i]=cnt2[l]+1;
                }
            }
            for(int i=1,p,q;i<=m;++i){
                std::cin >> p >> q;
                if(getmax(1,p-1) >= q){
                    int k = getmax(1,p-1);
                    int l=p+1,r=n+1;
                    while(l!=r){
                        int mid=l+r>>1;
                        if(getmax(p+1,mid) <= k)l=mid+1;
                        else r=mid;
                    }
                    std::cout << cnt[p-1]+cnt2[l] << '
    ';
                }else{
                    if(getmax(p+1,n) <= q){
                        std::cout << cnt[p-1]+1 << '
    ';
                    }else{
                        int l=p+1,r=n;
                        while(l!=r){
                            int mid=l+r>>1;
                            if(getmax(p+1,mid) <= q)l=mid+1;
                            else r=mid;
                        }
                        std::cout << cnt[p-1]+1+cnt2[l] << '
    ';
                    }
                }
            }
        }
    }
  • 相关阅读:
    Luogu3952 NOIP2017D1T2 时间复杂度
    Luogu4933 大师
    Luogu1966 火柴排队
    Luogu2881 排名的牛Ranking the Cows
    Luogu1439 最长公共子序列(LCS)
    Liferay7 BPM门户开发之20: 理解Asset Framework
    提高Liferay7的启动和运行速度
    liferay中jsonws的认证方法
    让Liferay的Service Builder连接其他数据库
    Liferay表结构介绍(四):Portlet相关表
  • 原文地址:https://www.cnblogs.com/skip1978/p/10332740.html
Copyright © 2011-2022 走看看