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
1
42
42
3
1 2 2
13
5
1 2 3 4 5
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)的更多相关文章
- Codeforces 597C. Subsequences (树状数组+dp)
题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...
- codeforces 597C (树状数组+DP)
题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k ...
- CodeForces 828E DNA Evolution(树状数组)题解
题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- Codeforces 635D Factory Repairs【树状数组】
又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- codeforces E. DNA Evolution(树状数组)
题目链接:http://codeforces.com/contest/828/problem/E 题解:就是开4个数组举一个例子. A[mod][res][i]表示到i位置膜mod余数是res的‘A’ ...
- Codeforces 567D - One-Dimensional Battle Ships - [树状数组+二分]
题目链接:https://codeforces.com/problemset/problem/567/D 题意: 在一个 $1 \times n$ 的网格上,初始摆放着 $k$ 只船,每只船的长度均为 ...
- codeforces#1167F. Scalar Queries(树状数组+求贡献)
题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...
随机推荐
- Sublime Text3介绍和插件安装——基于Python开发
Subime编辑器是一款轻量级的代码编辑器,是收费的,但是可以无限期使用.官网下载地址:https://www.sublimetext.com. Sublime Text3支持语言开发种类多样,几乎可 ...
- 学习day02
day021.结构标记 ***** 做布局 1.<header>元素 <header></header> ==> <div id="heade ...
- animate-queue和step-animate
Step-animate: 分为3部分:{配置},{step:function(){...},duration:1000} <div id="warpper" style=& ...
- 在SuperMap iDesktop中如何快速追加记录行?
SuperMap iDesktop 产品中,普通数据集右键查看属性表,无法编辑行.是因为要直接在属性表中添加行,只能是纯属性数据集才可用. 除了直接打开数据集,增加几何对象,还有什么办法可以快速追加记 ...
- Android Intent通讯实例
//1.拨打电话 // 给移动客服10086拨打电话 Uri uri = Uri.parse("tel:10086"); Intent intent = new Intent(In ...
- 『cURL』curl: (6) Could not resolve host无法解析主机地址
最近在学数据挖掘时,获取数据有两种途径: 开放数据,可以直接使用和存储的数据: 网络数据,通过爬虫或云市场api(付费或免费)获取数据 我通过教程,在阿里云购买一个天气数据api,尝试使用cURL获取 ...
- thinkphp5.1验证器场景验证中传参的方法。
一个场景:用户保存自己的昵称,如果已经有其他用户用了这个昵称则不允许保存,但是要排除当前用户自己,因为如果用户未作修改,新昵称和老昵称一样,是可以保存的. 因为昵称定义了唯一规则: 'name' =& ...
- 使用Let's Encrypt生成免费SSL证书操作记录
最近要做微信小程序,要求接口必须备案且是https,个人小站就直接准备使用免费的SSL证书,网上搜了一圈,发现Let's Encrypt是浏览器支持比较好的. 流程: 1. 首先去服务器上安装了Let ...
- 关于Xcode10的那些事
前言 这里主要介绍一下Xcode10 版本主要更新的内容. 随着iOS12的发布,Xcode10已经可以从Mac App Store下载. Xcode10包含了iOS12.watchOS 5.macO ...
- In action "Setting JDBC driver jar location unix [Set a variable]" (screen "Select a Database [Configurable banner form]"), property "Script":
java.lang.Exception: JDBC Driver Jar not found. Looking for: /u01/oracle/GG_Director/ERROR: Unresolv ...