【jzoj4920】【降雷皇】【动态规划】【二分答案】【数据结构】

题目大意

求一个序列的最长上升子序列及方案数。

解题思路

首先维护一个数组,t[i]表示长度为i的最长上升子序列末尾最小是多少,这样就可以求出第一个答案。每个长度再用权值线段树维护每个末尾的方案数,转移方案数时求一下比它小的末尾方案数之和。

code

#include
#include
#include
#define LL long long
#define min(a,b) ((a
#define max(a,b) ((a>b)?a:b)
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
int const maxn=1e5,inf=1e9;
int n,type,t[maxn+10],lson[maxn*16+10],rson[maxn*16+10],cntpon=maxn;
LL f[maxn*16+10],mo=123456789;
LL qury(int now,int l,int r,int pos){
    int mid=(l+r)>>1;
    if(l==r)return f[now];
    if(pos<=mid)return (lson[now])?qury(lson[now],l,mid,pos):0;
    else return (f[lson[now]]+((rson[now])?qury(rson[now],mid+1,r,pos):0))%mo;
}
void add(int now,int l,int r,int pos,LL val){
    int mid=(l+r)>>1;
    f[now]=(f[now]+val)%mo;
    if(l==r)return;
    if(pos<=mid){
        if(!lson[now])lson[now]=++cntpon;
        add(lson[now],l,mid,pos,val);
    }else{
        if(!rson[now])rson[now]=++cntpon;
        add(rson[now],mid+1,r,pos,val);
    }
}
int main(){
    //freopen("hamon.in","r",stdin);
    //freopen("hamon.out","w",stdout);
    freopen("d.in","r",stdin);
    freopen("d.out","w",stdout);
    scanf("%d%d",&n,&type);
    int a,l,r,mid,mx=0;
    fo(i,mx+1,n)t[i]=inf;
    fo(i,1,n){
        scanf("%d",&a);
        l=0,r=mx;
        while(l!=r){
            mid=(l+r+1)>>1;
            if(t[mid]>=a)r=mid-1;
            else l=mid;
        }
        if(a1])t[l+1]=a;
        if(l+1>mx)mx=l+1;
        add(l+1,1,maxn,a,max(1,qury(l,1,maxn,a-1)));
    }
    printf("%d\n",mx);
    if(type)printf("%lld\n",qury(mx,1,maxn,maxn));
    return 0;
}

你可能感兴趣的:(动态规划,jzoj,二分,三分,数据结构)