CodeForces #187.div2.problem E

E. Sereja and Subsequences
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 modulo1000000007 (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).

Sample test(s)
input
1
42
output
42
input
3
1 2 2
output
13
input
5
1 2 3 4 5
output
719
 
题意:给出一组数列a[1],a[2],...,a[n],找出所有不下降的且不相同的序列,比如(x<=y)a[x],a[x+1],...,a[y]满足a[x]<=a[x+1]<=...<=a[y],对于所有的序列,求出比他小或者相等的序列的个数。其中小或者等的定义如题所述。例如样例2,(1,2,2),满足题意的子序列有(1)-(2)-(1,2)-(2,2)-(1,2,2),则答案有(1)-(1),(2)-(1,1),(1,2),(1,1),(1,2),(2,1),(2,2)-(1,1,1),(1,1,2),(1,2,2),(1,2,1),为13。
 
 
    首先,对于以数x结尾的序列,它的总数为(sum[1]+sum[2]+...+sum[x])*x+x,其中sum[i]表示以i结尾时的总数。比如序列(1,2,3),满足的总数就为1*2*3(这是指子序列有3个元素时的总数)。至于为什么后面还有加上x,因为这要考虑到后续的问题,比如对于y=x+1,除了1*2*...*y(既1~y)的全部选上外,还有(2~y),(3~y)...(x~y),所以根据公式,可以知道x*y(既子序列为(x,y))也就计算进去了。这里对于sum[1]+sum[2]+...+sum[x],考虑到x最大有10^6,所以可以用树状数组求和。其中更新操作为:
update(x,(getSum(x)*x+x)%mod - ((getSum(x)-getSum(x-1)+mod)%mod+mod)%mod);中间过程的+mod是为了防止出现负数,其中getSum(x)*x+x即为上述公式,但是这里重复计算了之前加到x上的个数,故减掉。
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define SIZE 1000001
#define mod 1000000007

using namespace std;

typedef __int64 Int;

Int sum[SIZE];

void update(int x,int add)
{
    for(int i=x; i<SIZE; i+=(i&(-i)))
        sum[i] = (sum[i] + add) % mod;
}

Int getSum(int x)
{
    Int ret = 0;
    for(int i=x; i>0; i-=(i&(-i)))
        ret = (ret + sum[i]) % mod;
    return ret;
}

int main()
{
    int n,tem;
    cin >> n;
    for(int i=1; i<=n; i++)
    {
        cin >> tem;
        Int all = getSum(tem);
        int add = ((all+1)*tem%mod - (all-getSum(tem-1)+mod)%mod + mod)%mod;
        update(tem,add);
    }
    cout << getSum(SIZE-1)%mod << endl;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值