1 条题解

  • 0
    @ 2021-6-15 14:34:41

    C++ :

    #include <cstdio>
    #include <map>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int MOD=1000000007;
    int bef[100001];
    int seq[100000],n,x[100001];
    struct L{
    	L *lc,*rc;
    	int fr,to;
    	int cc;
    }lines[200100],*li,*root;
    L* newL(int fr,int to){
    	li->fr=fr;
    	li->to=to;
    	li->lc=li->rc=NULL;
    	li->cc=0;
    	return li++;
    }
    void build(L*r){
    	if(r->fr==r->to)return;
    	int mid=(r->fr+r->to)/2;
    	r->lc=newL(r->fr,mid);
    	r->rc=newL(mid+1,r->to);
    	build(r->rc);
    	build(r->lc);
    }
    void set(L*r,int index,int value){
    	if(r->fr==r->to){
    		r->cc=value%MOD;
    		return;
    	}
    	int mid=(r->fr+r->to)/2;
    	if(index<=mid)set(r->lc,index,value);
    	else set(r->rc,index,value);
    	r->cc=(r->lc->cc+r->rc->cc)%MOD;
    }
    int get(L*r,int fr,int to){
    	if(r->fr==fr&&r->to==to)return r->cc;
    	int mid=(r->fr+r->to)/2;
    	if(to<=mid)return get(r->lc,fr,to);
    	else if(fr>mid)return get(r->rc,fr,to);
    	else return (get(r->lc,fr,mid)+get(r->rc,mid+1,to))%MOD;
    }
    int main(){
    	//freopen("subsequence.in","r",stdin);
    	//freopen("subsequence.out","w",stdout);
    	scanf("%d",&n);
    	for(int i=0;i<n;i++){
    		scanf("%d",seq+i);
    		x[i]=seq[i];
    	}
    	x[n]=-1000000000;
    	sort(x,x+n+1);
    	memset(bef,0,sizeof(int)*(n+1));
    	li=lines;
    	root=newL(0,n);
    	build(root);
    	int ans=0;
    	for(int i=0;i<n;i++){
    		int ind=lower_bound(x,x+n+1,seq[i])-x;
    		ans=(ans+MOD-bef[ind])%MOD;
    		int tmp=get(root,0,ind-1);
    		set(root,ind,tmp+1);
    		ans=(ans+tmp)%MOD;
    		bef[ind]=tmp;
    	}
    	printf("%d\n",ans);
    }
    
    
    • 1

    信息

    ID
    1017
    时间
    2000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者