Revenge of Segment Tree

                                                         Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

                                                                                   Total Submission(s): 420    Accepted Submission(s): 180

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.
 

求一段序列的全部连续子序列的和。
对于序列中的第i个,下标从0開始。在第ai个前有i+1个数(包含它自己),在ai个后有n-i个(包含它自己),所以ai
共出现(i+1)*(n-i)次。



官方题解:

考虑每一个数出如今多少个子序列之中。如果第i个数为Ai。区间为[L,R]。

那么包括Ai的区间满足L⩽i⋂R⩾i。累加(L+1)∗(N−R)∗A[i]就能够了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
long long mod=1000000000+7;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long n;
long long ans=0;
long long a;
scanf("%I64d",&n);
for(int i=0;i<n;i++)
{
scanf("%I64d",&a);
ans=(ans+(((a*(i+1)%mod)*(n-i))%mod))%mod;
}
printf("%I64d\n",ans);
}
return 0;
}


hdu 5086 Revenge of Segment Tree(BestCoder Round #16)的更多相关文章

  1. HDU5086——Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...

  2. [ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  3. HUD 5086 Revenge of Segment Tree(递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5086 题目大意: 给定一个序列,求这个序列的子序列的和,再求所有子序列总和,这些子序列是连续的.去题目给的第二组 ...

  4. HDU5087——Revenge of LIS II(BestCoder Round #16)

    Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...

  5. hdu5086——Revenge of Segment Tree

    Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  6. BestCoder Round #16

    BestCoder Round #16 题目链接 这场挫掉了,3挂2,都是非常sb的错误 23333 QAQ A:每一个数字.左边个数乘上右边个数,就是能够组成的区间个数,然后乘的过程注意取模不然会爆 ...

  7. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  8. HDU 5228 ZCC loves straight flush( BestCoder Round #41)

    题目链接:pid=5228">ZCC loves straight flush pid=5228">题面: pid=5228"> ZCC loves s ...

  9. HDU 5432 Rikka with Tree (BestCoder Round #53 (div.2))

    http://acm.hdu.edu.cn/showproblem.php?pid=5423 题目大意:给你一个树 判断这棵树是否是独特的 一颗树是独特的条件:不存在一颗和它本身不同但相似的树 两颗树 ...

随机推荐

  1. JAVA-JSP内置对象之page范围

    相关资料:<21天学通Java Web开发> page范围1.当前页有效.2.通过使用pageContext的aetAttribute()设置属性,并通过pageContext的getAt ...

  2. php编写TCP服务端和客户端程序

    1.修改php.ini,打开extension=php_sockets.dll 2.服务端程序SocketServer.php <?php //确保在连接客户端时不会超时 set_time_li ...

  3. git——从远程库克隆

    从远程库克隆 阅读: 248434 上次我们讲了先有本地库,后有远程库的时候,如何关联远程库. 现在,假设我们从零开发,那么最好的方式是先创建远程库,然后,从远程库克隆. 首先,登陆GitHub,创建 ...

  4. [转]Java中的POJO类

    简单的Java对象(Plain Old Java Objects)实际就是普通JavaBeans,使用POJO名称是为了避免和EJB混淆起来, 而且简称比较直接. 其中有一些属性及其getter se ...

  5. Extjs4.x 共享组件,写法

    1.项目架构如下: /Application /ext--框架核心文件 /common--共享 /resources--共享资源 /src--共享js(例如,多个模块共享一个store等) /mode ...

  6. PCL几种采样方法

    (1)下采样  Downsampling 一般下采样是通过构造一个三维体素栅格,然后在每个体素内用体素内的所有点的重心近似显示体素中的其他点,这样体素内所有点就用一个重心点来表示,进行下采样的来达到滤 ...

  7. 转:Java中String与byte[]的转换

    原文地址:http://blog.csdn.net/llwan/article/details/7567906 String s = "fs123fdsa";//String变量 ...

  8. Android studio导入framework编译的classes.jar包

    1.在libs文件夹中加入jar包,并将其置顶 注:studio3.1的scope没有Provided选项,都默认选择implementation,studio2.3及以下版本需要将scope设置为P ...

  9. [maven] settings 文件 本地maven仓库

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  10. Fragment过度动画分析一

    Sliding Fragment 介绍:该案例为传统的Fragment增加了个性化的补间动画,其效果是原有fragment向屏幕内做一定的下沉,新的fragment显示在最上层,产生层叠效果的多个fr ...