CodeForces - 314C Sereja and Subsequences (树状数组+dp)

Sereja has a sequence that consists of n positive integers, a1, a2, ..., an.

First Sereja took a piece of squared paper and wrote all distinct non-empty non-decreasing subsequences of sequence a. Then for each sequence written on the squared paper, Sereja wrote on a piece of lines paper all sequences that do not exceed it.

A sequence of positive integers x = x1, x2, ..., xr doesn't exceed a sequence of positive integers y = y1, y2, ..., yr, if the following inequation holds: x1 ≤ y1, x2 ≤ y2, ..., xr ≤ yr.

Now Sereja wonders, how many sequences are written on the lines piece of paper. Help Sereja, find the required quantity modulo 1000000007 (109 + 7).

Input

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106).

Output

In the single line print the answer to the problem modulo 1000000007 (109 + 7).

Examples

Input
1
42
Output
42
Input
3
1 2 2
Output
13
Input
5
1 2 3 4 5
Output
719


题意:
原题意是这样的:
看第二组样例:
3
1 2 2
首先,写出所有非空的,非严格递增的,不同的子序列:
1
2
1 2
2 2
1 2 2
然后写出小于这些子序列的数组,问数组个数
~ 1
1

~2
1
2

~ 1 2
1 1
1 2

~2 2
1 1
1 2
2 1
2 2

~1 2 2
1 1 1
1 1 2
1 2 1
1 2 2

这样一共写出了13个数组。
显而易见的,每个子序列可以写出来的数组个数,其实就是子序列的数字之积。
思路:
dp[num[i]]表示子序列以num[i]为结尾的答案。
然后就按照输入顺序进行更新。
dp[num[i]]=(dp[1]到dp[num[i]]的和)*num[i]+num[i];
前半部分表示接在其他数字后面,用树状数组优化,后半部分表示自己单独出现
当然还要去重,就是1 2 2,第一个2会接在1后面,第二个2也接在1后面,就会重复。
用一个pre记录之前的那个dp[2],正常更新dp[2]再减去pre[2]就行了。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int maxm = 1000086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);

int num[maxn];
ll dp[maxm];
ll a[maxm];
ll pre[maxm];
int lowbit(int x){
    return x&(-x);
}

void update(int pos,ll num){
    while (pos<maxm){
        a[pos]+=num;
        a[pos]%=mod;
        pos+=lowbit(pos);
    }
}

ll query(int pos){
    ll ans=0;
    while (pos){
        ans+=a[pos];
        ans%=mod;
        pos-=lowbit(pos);
    }
    return ans;
}

int main()
{
//    ios::sync_with_stdio(false);
//    freopen("in.txt","r",stdin);

    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&num[i]);
    }
    for(int i=1;i<=n;i++){
        ll ans=query(num[i]);
        ll tmp=ans*num[i]+num[i];
        dp[num[i]]+=ans*num[i]+num[i];
        dp[num[i]]%=mod;
        dp[num[i]]-=pre[num[i]];
        tmp=((tmp-pre[num[i]])+mod)%mod;
        dp[num[i]]=(dp[num[i]]+mod)%mod;
        pre[num[i]]=dp[num[i]];
        update(num[i],tmp);
//        debug(dp,num[i]);
    }
    ll ans=0;
    for(int i=1;i<=maxm;i++){
        ans+=dp[i];
        ans%=mod;
    }
    printf("%lld\n",ans);

    return 0;
}
View Code

 

posted @ 2019-04-16 17:12  断腿三郎  阅读(208)  评论(0编辑  收藏  举报