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 = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); 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=;
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=;i<=n;i++){
scanf("%d",&num[i]);
}
for(int i=;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=;
for(int i=;i<=maxm;i++){
ans+=dp[i];
ans%=mod;
}
printf("%lld\n",ans); return ;
}

CodeForces - 314C Sereja and Subsequences (树状数组+dp)的更多相关文章

  1. Codeforces 597C. Subsequences (树状数组+dp)

    题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...

  2. codeforces 597C (树状数组+DP)

    题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k ...

  3. CodeForces 828E DNA Evolution(树状数组)题解

    题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...

  4. Codeforces 909C Python Indentation:树状数组优化dp

    题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...

  5. Codeforces 635D Factory Repairs【树状数组】

    又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...

  6. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  7. codeforces E. DNA Evolution(树状数组)

    题目链接:http://codeforces.com/contest/828/problem/E 题解:就是开4个数组举一个例子. A[mod][res][i]表示到i位置膜mod余数是res的‘A’ ...

  8. Codeforces 567D - One-Dimensional Battle Ships - [树状数组+二分]

    题目链接:https://codeforces.com/problemset/problem/567/D 题意: 在一个 $1 \times n$ 的网格上,初始摆放着 $k$ 只船,每只船的长度均为 ...

  9. codeforces#1167F. Scalar Queries(树状数组+求贡献)

    题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...

随机推荐

  1. JS 的继承

    1:原生链:prototype 儿子能够继承父亲的属性,也可以觉得遗传基因不好自己改属性,(但是不能改变老爸的属性). 看例子:             function farther(){     ...

  2. android studio gradle 更新方法。

    Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrapper gradle-wrapper.properties   (只要在打开项目的时候选OK,这个文件就 ...

  3. rocketmq简单消息发送

    有以下3种方式发送RocketMQ消息 可靠同步发送 reliable synchronous 可靠异步发送 reliable asynchronous 单向发送 one-way transmissi ...

  4. ClickOnce一项Winform部署

    先建一个Winform 控制台程序 建好后从工具箱里拖出来个 文本框 在属性中把TEXT改了 鼠标放到项目上点击右键——>属性 如下图所示,有两个发布位置. 发布位置可以选择本地文件夹,也可以选 ...

  5. python3 员工信息表

    这是最后一条NLP了......来吧 十二,动机和情绪总不会错,只是行为没有效果而已 动机在潜意识里,总是正面的.潜意识从来不会伤害自己,只会误会的以为某行为可以满足该动机,而又不知道有其他做法的可能 ...

  6. 《Python 数据库 GUI CGI编程》

    本文地址:http://www.cnblogs.com/aiweixiao/p/8390417.html 原文地址 点击关注微信公众号 wenyuqinghuai 1.写在前边 上一次,我们介绍了Py ...

  7. c++ 指针做为参数和返回值

    指针参数 返回值是指针 一.指针作参数形式的函数 //计算x的平方 x*x void square(int *x) { int a=*x; *x=a*a; } 二.指针作返回值的函数 int *squ ...

  8. 在混合开发框架模式中,简化客户端对Web API的频繁调用

    在混合开发框架模式中,有时候我们在处理树形节点的时候,需要很多关联的处理,可能需要结合用户配置信息,属性字典,以及表的字段分类等信息来展示一个结构树,那么在处理的时候就可能会频繁的对这些接口API进行 ...

  9. HTTPS中间人攻击实践(原理·实践)

      前言 很早以前看过HTTPS的介绍,并了解过TLS的相关细节,也相信使用HTTPS是相对安全可靠的.直到前段时间在验证https代理通道连接时,搭建了MITM环境,才发现事实并不是我想的那样.由于 ...

  10. FAST LOW-RANK APPROXIMATION FOR COVARIANCE MATRICES

    目录 Nystorm method 低秩逼近 矩阵乘法的逼近 Belabbas M A, Wolfe P J. Fast Low-Rank Approximation for Covariance M ...