Fenwick tree rises longest sequence optimization

Rising violence longest sub-sequence comparison of the wording is the n- , in fact, we find that the maximum value is smaller than the longest current sequence of rising front;

Fenwick tree can optimize it;

Reverse decline in demand is the longest sequence;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
typedef double dd;
typedef long long ll;
ll n;
ll a[maxn];
ll id[maxn];

ll f[maxn],g[maxn];

ll b1[maxn],b2[maxn];

int len;

ll query_front(int x)
{
    ll ans=0;
    for(;x;x-=x&(-x)) ans=max(b1[x],ans);
    return ans;
}

ll query_back(int x)
{
    ll ans=0;
    for(;x;x-=x&(-x)) ans=max(b2[x],ans);
    return ans;
}

void  add_front(int x,ll y)
{
    for(;x<=len;x+=x&(-x)) b1[x]=max(b1[x],y);
}

void add_back(int x,ll y)
{
    for(;x<=len;x+=x&(-x)) b2[x]=max(b2[x],y);
}

dd ans;

int qw[maxn];

int main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        id[i]=a[i];
    }
    sort(id+1,id+n+1);
    len=unique(id+1,id+n+1)-id-1;
    for(int i=1;i<=n;i++) qw[i]=lower_bound(id+1,id+len+1,a[i])-id;
    for(int i=1;i<=n;i++)
    {
        f[i]=query_front(qw[i]-1)+a[i];
        g[n-i+1]=query_back(qw[n-i+1]-1)+a[n-i+1];
        add_front(qw[i],f[i]);
        add_back(qw[n-i+1],g[n-i+1]);
    }
    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/WHFF521/p/11730074.html