lightoj 1085【离散化+树状数组】

题意:
求所有的上升子序列种数;
思路:
我想先离散化一下,然后用树状数组维护一下。
最终答案就是sum(n) ?

卧槽,好像是;然后就过了。。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod=1000000007;
const int N=1e5+10;

LL arr[N],n;
LL c[N*4];

void add(LL i,LL v)
{
    while(i<=n)
    {
        c[i]=(c[i]+v)%mod;
        i+=i&(-i);
    }
}

LL Sum(LL i)
{
    LL ans=0;
    while(i>0)
    {
        ans=(ans+c[i])%mod;
        i-=i&(-i);
    }
    return ans%mod;
}

vector<LL>xs;
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);

        xs.clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            xs.push_back(arr[i]);
        }
        sort(xs.begin(),xs.end());
        vector<LL>::iterator e=unique(xs.begin(),xs.end());
        for(int i=1;i<=n;i++)
            arr[i]=lower_bound(xs.begin(),e,arr[i])-xs.begin()+1;

        memset(c,0,sizeof(c));
        LL temp;
        for(int i=1;i<=n;i++)
        {
            temp=(Sum(arr[i]-1)+1%mod);
            add(arr[i],temp);
        }
        printf("Case %d: %lld
",cas++,Sum(n));
    }
    return 0;
}


原文地址:https://www.cnblogs.com/keyboarder-zsq/p/6777533.html