Revenge of Segment Tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1541    Accepted Submission(s): 552

Problem Description
In
computer science, a segment tree is a tree data structure for storing
intervals, or segments. It allows querying which of the stored segments
contain a given point. It is, in principle, a static structure; that is,
its content cannot be modified once the structure is built. A similar
data structure is the interval tree.
A segment tree for a set I of n
intervals uses O(n log n) storage and can be built in O(n log n) time.
Segment trees support searching for all the intervals that contain a
query point in O(log n + k), k being the number of retrieved intervals
or segments.
---Wikipedia

Today, Segment Tree takes revenge on
you. As Segment Tree can answer the sum query of a interval sequence
easily, your task is calculating the sum of the sum of all continuous
sub-sequences of a given number sequence.

 
Input
The first line contains a single integer T, indicating the number of test cases.

Each
test case begins with an integer N, indicating the length of the
sequence. Then N integer Ai follows, indicating the sequence.

[Technical Specification]
1. 1 <= T <= 10
2. 1 <= N <= 447 000
3. 0 <= Ai <= 1 000 000 000

 
Output
For each test case, output the answer mod 1 000 000 007.
 
Sample Input
2
1
2
3
1 2 3
 
Sample Output
2
20

Hint

For the second test case, all continuous sub-sequences are [1], [2], [3], [1, 2], [2, 3] and [1, 2, 3]. So the sum of the sum of the sub-sequences is 1 + 2 + 3 + 3 + 5 + 6 = 20.
Huge input, faster I/O method is recommended. And as N is rather big, too straightforward algorithm (for example, O(N^2)) will lead Time Limit Exceeded.
And one more little helpful hint, be careful about the overflow of int.

 
Source
 
题意:求一个序列所有的连续子序列之和。
题解:假设序列为 1 2 3
那么合法序列有:
1 第一项
1 2 第二项
2
1 2 3 第三项
2 3
3
dp[i]代表第i项 那么我们可以看出 dp[i] = dp[i-1]+i*a[i]
最终答案累加即可。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long LL;
const int mod = ;
const int N = ;
int n;
LL a[N];
LL dp[N];
int main()
{ int tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
dp[] = a[];
for(int i=;i<=n;i++){
dp[i] = (dp[i-] + (i*a[i])%mod)%mod;
}
LL ans = ;
for(int i=;i<=n;i++){
ans = (ans+dp[i])%mod;
}
printf("%lld\n",ans);
}
return ;
}

hdu 5086(递推)的更多相关文章

  1. HDOJ(HDU).2044-2049 递推专题

    HDOJ(HDU).2044-2049 递推专题 点我挑战题目 HDU.2044 题意分析 先考虑递推关系:从1到第n个格子的时候由多少种走法? 如图,当n为下方格子的时候,由于只能向右走,所以有2中 ...

  2. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  3. "红色病毒"问题 HDU 2065 递推+找循环节

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=2065 递推类题目, 可以考虑用数学方法来做, 但是明显也可以有递推思维来理解. 递推的话基本就是状态 ...

  4. Children’s Queue HDU 1297 递推+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...

  5. hdu 2044-2050 递推专题

    总结一下做递推题的经验,一般都开成long long (别看项数少,随便就超了) 一般从第 i 项开始推其与前面项的关系(动态规划也是这样),而不是从第i 项推其与后面的项的关系. hdu2044:h ...

  6. ZOJ 3182 HDU 2842递推

    ZOJ 3182 Nine Interlinks 题目大意:把一些带标号的环套到棍子上,标号为1的可以所以操作,标号i的根子在棍子上时,只有它标号比它小的换都不在棍子上,才能把标号为i+1的环,放在棍 ...

  7. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

  8. hdu 4055 递推

    转自:http://blog.csdn.net/shiqi_614/article/details/7983298 题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果 ...

  9. HDU 3123-GCC(递推)

    GCC Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

随机推荐

  1. Java 虚拟机结构分析

    本博文主要介绍了JVM(Java Virtual Machine)的组成部分以及它们内部的工作机制和原理.需要注意的是,虽然平时我们用的大多是Sun(现已被Oracle收购)JDK提供的JVM,但是J ...

  2. 页面引入外部字体ttf,如何提取所需要的ttf字体或者加载过慢的解决方法-1127更新

    最近几天编写手机端的页面之后,文中需要华文行楷字体,在网上下载后,引入到了自己的前端页面,以为没有什么事了,继续码代码 @font-face { font-family:huawen; src: ur ...

  3. 途牛banner自动轮播

    <!DOCTYPE html>               <!--申明文档类型:html--> <html lang="en">       ...

  4. CSS系列(8) CSS后代选择器和子选择器详解

    一.CSS后代选择器详解 1,  生动介绍基本概念 一个标签嵌B在另一个标签A内部,B就是A的后代. 而且,B的后代也是A的后代,这就叫“子子孙孙无穷尽也”. 比如: <div> < ...

  5. CocosCreator设置启动场景

    刚开始接触CocosCreator,在调试时,如果有多个场景,不知道如何设置将某个指定的场景设置为启动场景,折腾了一圈,找到了设置的地方, 记录一下.   点击项目->项目设置     在预览运 ...

  6. Python+Selenium框架设计篇之-什么是POM

    前面我们介绍了Python中的单元测试框架unittest,以后我们所有的测试类文件,都采用unittest来辅助我们进行debug和脚本开发.搞定了debug机制和确定了unittest来进行创建和 ...

  7. JavaWeb项目中使用ajax上传文件

    1.jsp $("#cxsc").click(function(){ var bankId = $("#bankId").val(); var formdata ...

  8. Python全栈工程师 (exercises)

    # 1:给定一个数,判断他是正数,负数,还是0 a = int(input("请输入一该个整数")) if a == 0: print(a, "是0") eli ...

  9. UVALive 6324 Archery (求射箭覆盖的期望)

    #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> const d ...

  10. Rational Rose 使用技巧

    1.浏览区 2.菜单项 其中Format选项中: 决定各项是否显示,也可以通过右击-option选择 3.常用快捷键: F1:任何时候都可以按F1获得相关帮助,把鼠标放在某条菜单上按F1可以获得这条菜 ...