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$ ...
随机推荐
- 时空地图TimeGIS.com生成正交曲线网格
数值模拟中对数学物理方程的求解过程中经常需要生成网格,这里提供了一种方便的方法,只需要简单地勾画出区域的轮廓, 就可以生成相应的正交曲线网格,详情请访问 www.TimeGIS.com
- pthread小结
参考1 https://computing.llnl.gov/tutorials/pthreads/ 参考2 http://man7.org/linux/man-pages/man7/pthreads ...
- DataPipeline CTO陈肃:从ETL到ELT,AI时代数据集成的问题与解决方案
引言:2018年7月25日,DataPipeline CTO陈肃在第一期公开课上作了题为<从ETL到ELT,AI时代数据集成的问题与解决方案>的分享,本文根据陈肃分享内容整理而成. 大家好 ...
- java class反编译工具----JD-GUI
下载地址 http://jd.benow.ca/
- C# 获取当前服务器运行程序的根目录,获取当前运行程序物理路径
C# 获取当前服务器运行程序的根目录,获取当前运行程序物理路径 string tmpRootDir = AppDomain.CurrentDomain.BaseDirectory;//获得当前服务器程 ...
- Linux通过NFS实现文件共享
在项目生产环境我们经常需要实现文件共享,传统的常见方案是通过NFS,实现服务器之间共享某一块磁盘,通过网络传输将分散的文件集中存储在一块指定的共享磁盘,实现基本的文件共享.实现这种方案,分服务端和客户 ...
- js饼状图(带百分比)功能实现,新人必懂
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 基于android的天气预报的设计与实现
目录 应用开发技术及开发平台介绍 应用需求分析 应用功能设计及其描述 应用UI展示 ①开发技术: 本系统是采用面向对象的软件开发方法,基于Android studio开发平台,以Android作为本系 ...
- kvm虚拟机管理基础
部署 KVM 虚拟机 a.kvm 安装 环境:centos7,cpu 支持虚拟化,关闭 selinux,关闭 firewalld yum install libvirt virt-install qe ...
- Wsus Content内容误删处理
问题:在wsus content文件夹下误删除文件,需要重新下载文件解决方法:打开cmdcd C:\Program Files\Update Services\Tools\.\wsusutil.exe ...