HDU6404 lower_bound【线段树】

昨天敲了一下午没结局 一直想用lower_bound函数 现在想想lower_bound得排好序才能用...

题意:

给你一组数 替换其中一个数 问递增子序列长度

不懂怎么表达 看样例

1 2 3 4 4

input:1 5(5 2 3 4 4 )

output:1(5)

input:5 5(1 2 3 4 5 )

output:5(1 2 3 4 5)

input:2 3(1 3 3 4 4)

output:3(1 3 4)

思路:

d1[i]为a[1]到a[i]的最长递增子序列长度

d2[i]为a[i]到a[n]的最长递增子序列长度

分段 [1,p-1] , p, [p+1,n] 

然后线段树查询1到p-1的最大值Maxx下标cur ans+=d1[cur] 如果q>Maxx ans++

Maxx = max(q,Maxx);

然后查p+1到n第一个大于Maxx的数的下标cur2 ans+=d2[cur2]

代码:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5+5;

int a[MAXN],d1[MAXN],d2[MAXN];
int tree[(MAXN)<<2],vis[(MAXN)<<2];
int ans,cur;

//更新节点信息
void PushUp(int rt) {
	tree[rt] = max(tree[rt<<1], tree[rt<<1|1]);
}

//建树
void build(int l, int r,int rt) {
    if(l == r) {
        tree[rt] = a[l];
        return ;
    }
    int m = (l + r) >> 1;
    build(l, m, rt << 1);
    build(m+1,r,rt << 1|1);
    PushUp(rt);
}

//求区间第一个大于k的数的下标
void query(int l, int r, int rt, int L, int R, int k) {
    if(l == r) {
        cur = min(cur,l);
        return ;
    }
    int m = (l + r)>>1;
    if(l >= L && r <= R) {
        if(tree[rt<<1] > k) query(l, m, rt<<1, L, R, k);
        else if(tree[rt<<1|1] > k) query(m+1, r, rt<<1|1, L ,R, k);
        return;
    }
    if(L <= m && tree[rt<<1] > k) query(l, m, rt<<1, L, R, k);
    if(R > m && tree[rt<<1|1] > k) query(m+1,r,rt << 1|1, L, R, k);
}

//求区间最大值的下标
void query2(int l, int r, int rt, int L, int R) {
    if(l == r) {
        if(tree[rt] > a[cur]) cur = l;
        return;
    }
    int m = (l + r) >> 1;
    if(l >= L && r <= R) {
        if(tree[rt<<1] >= tree[rt<<1|1]) query2(l, m, rt<<1, L, R);
        else query2(m+1, r, rt << 1|1, L, R);
        return ;
    }
    if(L <= m) query2(l,m,rt<<1,L,R);
    if(R > m) query2(m+1,r,rt<<1|1,L,R);
}

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        int n,m,p,q,Max = -1;
        scanf("%d%d",&n,&m);
        memset(d1,0,sizeof(d1));
        memset(d2,0,sizeof(d2));
        for(int i = 1; i <= n; i++) {
            scanf("%d",&a[i]);
            if(a[i] > Max) d1[i] = d1[i-1] + 1, Max = a[i];
            else d1[i] = d1[i-1];
            // 1 2 3 4 4 3 2 1 d1 = {1,2,3,4,4,4,4,4}
        }
        memset(tree,0,sizeof(tree));
        build(1,n,1);
        d2[n] = 1;
        for(int i = n-1; i; i--) {
            cur = n + 1;
            query(1,n,1,i+1,n,a[i]);
            if(cur > n) cur = 0;
            d2[i] = d2[cur] + 1;
        }
        while(m--) {
            scanf("%d%d",&p,&q);
            ans = cur = 0;
            if(p!=1) query2(1,n,1,1,p-1);
            ans += d1[cur];
            if(q > a[cur]) ans++;
            else q = a[cur];
            cur = n+1;
            if(p!=n) query(1, n, 1, p+1, n, q);
            if(cur <= n) ans += d2[cur];
            printf("%d\n",ans);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值