HDU find the nondecreasing subsequences(树状数组)

Problem Description
How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.
 

Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n that is the length of the sequence S, the next line contains n integers {s1, s2, s3, ...., sn}, 1 <= n <= 100000, 0 <= si <= 2^31.
 

Output
For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.
 

Sample Input
3
1 2 3
 

Sample Output
7


 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

typedef long long ll;

ll n;
ll a[100005],b[100005];
ll c[100005],dp[100005];

struct Node
{
    ll value;
    int index;
    bool operator < (const Node&b)const
    {
        return value<b.value;
    }
}node[100005];

ll lowbit(ll i)
{
    return i&(-i);
}

void updata(ll i,ll data)
{
    while(i<=n)
    {
        c[i]+=data;
        c[i]=c[i]%1000000007;
        i+=lowbit(i);
    }
}

ll query(ll i)
{
    ll res=0;
    while(i>0)
    {
        res+=c[i];
        i-=lowbit(i);
    }
    return res%1000000007;
}

int main()
{
    int i,j;
    while(~scanf("%lld",&n))
    {
        memset(dp,0,sizeof(dp));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&node[i].value);
            node[i].index=i;
        }
        sort(node+1,node+n+1);
        b[ node[1].index ]=1;
        for(i=2;i<=n;i++)
        {
            if(node[i].value==node[i-1].value)
                b[ node[i].index ]=b[ node[i-1].index ];
            else
                b[ node[i].index ]=i;
        }
        for(i=1;i<=n;i++)
        {
            dp[i]=query(b[i]);
            updata(b[i],dp[i]+1);
        }
        cout<<query(n)<<endl;
    }
    return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值