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. Android 网页打开app(或者打开指定页面)并且接收参数

    网页打开app 现实描述场景: 1.短信通知中通知内容,比如信息中一个咨询详情,流程步骤,信息中的地址打开的是一个网页,网页打开就指定app或者app中的指定页面 html代码 <html> ...

  2. 测者的测试技术手册:智能化测试框架EvoSuite的一个坑以及填坑方法

    问题 最近在不断地学习和探索EvoSuite框架的时候,在生产JUnit单元测试框架后,出现如下问题: Exception: Caused by: org.evosuite.runtime.TooMa ...

  3. rabbitmq之基本原理及搭建单机环境

    1.RabbitMQ基本原理 1.MQ全称Message Queue,是一种分布式应用程序的通信方法,是消费-生产者模型的典型代表,producer向消息队列中不断写入消息,而另一端consumer则 ...

  4. ios定义数组和字典快捷方式

    //标准写法 NSNumber * number = [NSNumber numberWithInt:1]; NSArray * array = [NSArray arrayWithObjects:@ ...

  5. composer在update时提示file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages: error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO

    在开发的时候,需要把依赖的服务更新到最新,然后 手动composer update一下,提示如下: failed) Update failed (The "e "https://a ...

  6. 「插件」Runner更新Pro版,帮助设计师远离996

    三年多前Runner团队在德国汉堡的骇客松上第一次发布了Sketch插件Runner的beta版本.从那以后,这个团队的目标一直很清晰: 创造一个加速设计工作流的工具. 他们只给Runner添加真正能 ...

  7. .net prams关键字

    先举个例子: 代码如下: class Program { static void Main(string[] args) { Console.WriteLine(Sum(1)); Console.Wr ...

  8. 如何查看linux中文件打开情况

    前言 我们都知道,在linux下,“一切皆文件”,因此有时候查看文件的打开情况,就显得格外重要,而这里有一个命令能够在这件事上很好的帮助我们-它就是lsof. linux下有哪些文件 在介绍lsof命 ...

  9. tensorflow的基本认识

    版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/10741013.html 作者:窗户 ...

  10. bilibili用户信息全栈爬取